我正在尝试将结果从Mysql数据库获取到几个文本框中。这是我打印结果的代码。此页面称为 api2.php
如何将每行结果定向并打印到我的索引中.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);
?>
索引.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å di nummerene du ønsker<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 进入我的索引.php:
<script>
$.getJSON("api2.php", function (data) {
$.each(data, function (key, value) {
$('#l1').val();
$('#l2').val();
});
});
然后我希望它出现在索引中的文本框中.php:
<input type="text" name="l1" id="l1" />
<input type="text" name="l2" id="l2" />
我看到我必须找到正确的值,但我不知道如何。
$('#l1').val(???);
我猜是这样的:$('#l1').val(echo "'$name'")
试试这个。我测试了它,它有效>
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å di nummerene du ønsker<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'));