如何将MySql数据库结果打印到文本框中

时间:2015-03-08 21:48:35

标签: php mysql

我正在尝试将Mysql数据库中的结果提取到几个文本框中。 这是我打印结果的代码。 该页面名为api2.php

如何将每行结果指向并打印到特定的texbox中。在我的index.php中

<?php 
include 'db.php';

$return_arr = array();

$fetch = mysql_query("SELECT * FROM user"); 

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
    $row_array['1'] = $row['name'];
    $row_array['2'] = $row['name2'];
    $row_array['3'] = $row['name3'];
    $row_array['4'] = $row['name4'];
    $row_array['5'] = $row['name5'];

    array_push($return_arr,$row_array);
}

echo json_encode($return_arr);

?>

index.php需要从数据库(api.php)加载结果并禁用文本框。数据库的帖子部分没问题

<!DOCTYPE html>
<html lang="en">
  <head><meta http-equiv="Content-Type" content="text/html; charset=euc-kr">

    <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="created" content="tir, 03 mar 2015 17:47:17 GMT">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta http-equiv="refresh" content="" >
    <title>Vin lotteri</title>

    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>
  <div class="container">
<div class="main">
<form id="myForm" action="userInfo.php" method="post">
<form method="get" id="form">

<center>
<h2>Vin lotteri</h2>

<br>
Velg nummer<br>
Skriv inn navnet ditt p&aring di nummerene du &oslashnsker<br>
Husk, kun 5 lodd pr. pers.<br><br>


<!-- javascript/jQuery -->

<script src="script/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="script/my_script.js" type="text/javascript"></script>

<div id="holder"></div>
</head>
<body>

<script>


      $.getJSON( "api.php", function( data ) {
        $.each( data, function( key, val ) {

            $('#holder').append( "<input value='" + val + "' name='" + val + "'/>");


        });
    });
</script>

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
</style>
<table class="tg">
  <tr>
    <th class="tg-031e">1</th>
    <th class="tg-031e"><input type="text" name="l1" id="l1" /></th>
    <th class="tg-031e">2</th>
    <th class="tg-031e"><input type="text" name="l2" id="l2" /></th>
    <th class="tg-031e">3</th>
    <th class="tg-031e"><input type="text" name="l3" id="l3" /></th>
    <th class="tg-031e">4</th>
    <th class="tg-031e"><input type="text" name="l4" id="l4" /></th>
    <th class="tg-031e">5</th>
    <th class="tg-031e"><input type="text" name="l5" id="l5" /></th>
 </tr>
</table>
<br>
<input type="submit" name="submit" id="submit" value="Neste" >
</form>

<span id="result"></span>

</div>
</div>
</body>
</html>

我已经尝试了几天让dta进入我的文本框而没有运气。 正如我现在所看到的,我需要一点帮助:

我从我的数据库中获取这个叫做api2.php:

的文件
    <?php 

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------
  include 'db.php';


$return_arr = array();

$fetch = mysql_query("SELECT * FROM user"); 

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
    $row_array['1'] = $row['name'];
    $row_array['2'] = $row['name2'];

    array_push($return_arr,$row_array);
}

echo json_encode($return_arr);

?>

然后我使用getJSON来获取我的index.php:

<script>
$.getJSON("api2.php", function (data) {
    $.each(data, function (key, value) {    

      $('#l1').val();
      $('#l2').val();
    });
});

然后我希望它出现在index.php中的文本框中:

<input type="text" name="l1" id="l1" />
<input type="text" name="l2" id="l2" />

我看到我必须找到正确的价值,但我不知道如何。

$('#l1').val(???);

我认为它类似于:$('#l1').val(echo "'$name'")

1 个答案:

答案 0 :(得分:1)

试试这个。我测试了它,它的工作原理&gt;

HTML(此html可满足您的需求)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=euc-kr">

    <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="created" content="tir, 03 mar 2015 17:47:17 GMT">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta http-equiv="refresh" content="">
    <title>Vin lotteri</title>

    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
<div class="container">
    <div class="main">
        <form id="myForm" action="userInfo.php" method="post">
            <form method="get" id="form">

                <center>
                    <h2>Vin lotteri</h2>

                    <br>
                    Velg nummer<br>
                    Skriv inn navnet ditt p&aring di nummerene du &oslashnsker<br>
                    Husk, kun 5 lodd pr. pers.<br><br>


                    <!-- javascript/jQuery -->

                    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

                    <script src="script/my_script.js" type="text/javascript"></script>

                    <div id="holder"></div>
                    </head>
                    <body>

                    <style type="text/css">
                        .tg {
                            border-collapse: collapse;
                            border-spacing: 0;
                        }

                        .tg td {
                            font-family: Arial, sans-serif;
                            font-size: 14px;
                            padding: 10px 5px;
                            border-style: solid;
                            border-width: 1px;
                            overflow: hidden;
                            word-break: normal;
                        }

                        .tg th {
                            font-family: Arial, sans-serif;
                            font-size: 14px;
                            font-weight: normal;
                            padding: 10px 5px;
                            border-style: solid;
                            border-width: 1px;
                            overflow: hidden;
                            word-break: normal;
                        }
                    </style>
                    <table class="tg">
                        <tr>
                            <th class="tg-031e">1</th>
                            <th class="tg-031e"><input type="text" name="l1" id="l1"/></th>
                            <th class="tg-031e">2</th>
                            <th class="tg-031e"><input type="text" name="l2" id="l2"/></th>
                            <th class="tg-031e">3</th>
                            <th class="tg-031e"><input type="text" name="l3" id="l3"/></th>
                            <th class="tg-031e">4</th>
                            <th class="tg-031e"><input type="text" name="l4" id="l4"/></th>
                            <th class="tg-031e">5</th>
                            <th class="tg-031e"><input type="text" name="l5" id="l5"/></th>
                        </tr>
                    </table>
                    <br>
                    <input type="submit" name="submit" id="submit" value="Neste">
                </center>
            </form>

            <span id="result"></span>

    </div>
</div>
<script>
    $.getJSON("api.php", function (data) {
        $.each(data, function (key, value) {    
            $('#l'+(key+1)).val(value);


        });
    });
</script>
</body>
</html>

注意:请勿使用 <center></center>已弃用。如果你愿意的话,Ypou可以自己删除它们。

JSON(这只是一个虚拟示例)此代码用于证明它有效。

<?php
header('Content-Type: application/json');
echo json_encode(array('jose','leandro','pedro'));