Php 使用下拉列表Jquery从sql填充文本字段
您好,第一次这样做,基本上我对如何从数据库中重新填充文本框感到困惑 我目前的问题是,我的数据库中基本上有两个表“USER”和“STATISTICS”。 目前,我的代码正在“User”表中查找“User\u ID”的值,并在下拉列表中填充这些值 我想从那里得到的是,文本字段填充对应于数据库中查找“用户ID”的那些值,例如“得分”、“协助”、“清理工作表”等 我很困惑,我查了很多不同的问题,但找不到我想要的Php 使用下拉列表Jquery从sql填充文本字段,php,mysql,ajax,Php,Mysql,Ajax,您好,第一次这样做,基本上我对如何从数据库中重新填充文本框感到困惑 我目前的问题是,我的数据库中基本上有两个表“USER”和“STATISTICS”。 目前,我的代码正在“User”表中查找“User\u ID”的值,并在下拉列表中填充这些值 我想从那里得到的是,文本字段填充对应于数据库中查找“用户ID”的那些值,例如“得分”、“协助”、“清理工作表”等 我很困惑,我查了很多不同的问题,但找不到我想要的 <?php $link = mysql_connect("localhost","ro
<?php
$link = mysql_connect("localhost","root","");
mysql_select_db("f_club",$link);
$sql = "SELECT * FROM user ";
$aResult = mysql_query($sql);
?>
<html>
<body>
<title>forms</title>
<link rel="stylesheet" type="text/css" href="css/global.css" />
</head>
<body>
<div id="container">
<form action="update.php" method="post">
<h1>Enter User Details</h1>
<h2>
<p> <label for="User_ID"> User ID: </label> <select id="User_ID" id="User_ID" name="User_ID" >
<br> <option value="">Select</option></br>
<?php
$sid1 = $_REQUEST['User_ID'];
while($rows=mysql_fetch_array($aResult,MYSQL_ASSOC))
{
$User_ID = $rows['User_ID'];
if($sid1 == $id)
{
$chkselect = 'selected';
}
else
{
$chkselect ='';
}
?>
<option value="<?php echo $id;?>"<?php echo $chkselect;?>>
<?php echo $User_ID;?></option>
<?php }
?>
I had to put this in because everytime I have text field under the User_ID it goes next to it and cuts it off :S
<p><label for="null"> null: </label><input type="text" name="null" /></p>
<p><label for="goal_scored">Goal Scored: </label><input type="text" name="Goal_Scored" /></p>
<p><label for="assist">assist: </label><input type="text" name="assist" /></p>
<p><label for="clean_sheets">clean sheets: </label><input type="text" name="clean_sheets" /></p>
<p><label for="yellow_card">yellow card: </label><input type="text" name="yellow_card" /></p>
<p><label for="red_card">red card: </label><input type="text" name="red_card" /></p>
<p><input type="submit" name="submit" value="Update" /></p></h2>
</form>
</div>
</body>
</html>
形式
输入用户详细信息
用户ID:
挑选
我建议使用带有查询的简单页面路径,比如user.php?id=1
,而不是将时间花在像AJAX这样复杂的事情上
创建一个user.php
文件(与您的类似),如果设置了id
(if-isset($\u GET['id'])
),则使用select*从数据库中选择该用户,其中id=$id
(我当然假设每个用户都有id
)
您仍然可以使用
,但请记住使用
将其关闭。你可能会得到这样的结果:
<form method="get">
<label for="user">Select user:</label>
<select name="id" id="user">
<option value="1">User 1</option>
...
</select>
<submit name="submit" value="Select user" />
</form>
记住,这只是我所说想法的一个例子,很多代码都被省略了。我通常不喜欢在
标记之外编写HTML,但我想它可以工作。尤其是对于较小的东西。嗯……您需要告诉服务器选择了哪个用户。要么做一个AJAX请求并检索数据以直接将其放入文本字段,要么只是将表单发送到服务器并让PHP脚本检索相应的值。感谢您的帮助,我会让您知道我是如何实现它的。这不是我要找的。。。实际上,我改进了一些代码,而不是查看用户表,而是将其更改为统计表。我还是想知道我该怎么做。我已经研究过AJAX onchange,但仍然无法理解它。有什么帮助吗?
<?php
// init
// Use mysqli_ instead, mysql_ is deprecated
$result = mysqli_query($link, "SELECT id, name FROM users");
// Create our select
while ( $row = mysqli_fetch_array($link, $result, MYSQL_ASSOC) ) {?>
<option value="<?php echo $result['id']; ?>"><?php echo $result['name'] ?></option>
<?php}
// More code ommitted
if (isset($_GET['id'])) {
$id = sanitise($_GET['id']); // I recommend creating a function for this,
// but if only you are going to use it, maybe
// don't bother.
$result = mysqli_query($link, "SELECT * FROM users WHERE id = $id");
// now create our form.
if (isset($_POST['submit'])) {
// data to be updated
$data = sanitise($_POST['data']);
// ...
mysqli_query($link, "UPDATE users SET data = $data, ... WHERE id = $id");
// To avoid the 'refresh to send data thing', you might want to do a
// location header trick
header('Location: user.php?id='.$id);
}
}