gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:21:36 25 4
gpt4 key购买 nike

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

如何将每一行结果定向并打印到特定的文本框中。在我的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'")

最佳答案

试试这个。我测试了它并且有效>

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'));

关于php - 如何将MySql数据库结果打印到文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28932294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com