如何从使用PHP MYSQL和AJAX创建的HTML表单返回值
所以我制作了一个DynamcHTML表单,它根据数据库中保存的数据填充列表 这是数据库格式如何从使用PHP MYSQL和AJAX创建的HTML表单返回值,php,mysql,list,dynamic,Php,Mysql,List,Dynamic,所以我制作了一个DynamcHTML表单,它根据数据库中保存的数据填充列表 这是数据库格式 CREATE TABLE `electron_subjects`.`subjects` ( `id` INT NOT NULL AUTO_INCREMENT, `subject` VARCHAR(50) NOT NULL, `code` VARCHAR(10) NOT NULL, `semester` INT NOT NULL, `year` INT NOT NU
CREATE TABLE `electron_subjects`.`subjects`
(
`id` INT NOT NULL AUTO_INCREMENT,
`subject` VARCHAR(50) NOT NULL,
`code` VARCHAR(10) NOT NULL,
`semester` INT NOT NULL,
`year` INT NOT NULL,
`lab` BOOL NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
);
这基本上是每个学期的主题列表现在表单运行正常,但我想知道如何返回用户选择的值进行处理..首先,我必须根据主题出现的情况选择学期..在我按submit之后,我想获取代码,或者该主题的行id。如下图所示,我必须获得网络理论的代码或ro_id
我想我需要把value标签放在这一行,因为这是第二个列表,但是怎么做呢
echo "<option>";echo $row["subject"];echo "</option>";
echo”“;echo$行[“主题”];回声“;
这是表格
<?php
require 'db.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Selection Form</title>
</head>
<body>
<form name="selection" action="/display/check.php" method="post"/>
<table>
<tr>
<td>Select Semester</td>
<td>
<select id=semesterdd onChange="change_semester()">
<option>
Select
</option>
<?php
$result = $mysqli->query("SELECT * FROM subjects ORDER BY semester ASC") or die($mysqli->error());
while($row=mysqli_fetch_array($result))
{
?>
<option ><?php echo $row["semester"];?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>
Select Subjects:
</td>
<td>
<div id="subjectid">
<select name='listchoice'>
<option>
Select
</option>
</select>
</div>
</td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
<script type="text/javascript">
function change_semester()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax2.php?semester="+document.getElementById("semesterdd").value,false);
xmlhttp.send(null);
//alert(xmlhttp.responseText);
document.getElementById("subjectid").innerHTML=xmlhttp.responseText;
}
</script>
</body>
选择表格
选择学期
挑选
选择主题:
挑选
函数变化
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open(“GET”、“ajax2.php?sement=“+document.getElementById(“semesterdd”).value,false);
xmlhttp.send(空);
//警报(xmlhttp.responseText);
document.getElementById(“subjectid”).innerHTML=xmlhttp.responseText;
}
这是ajax2.php文件
<?php
require 'db.php';
$semester=$_GET["semester"];
if($semester!="")
{
$result=$mysqli->query("SELECT * FROM subjects WHERE semester=$semester ORDER BY subject ASC") or die($mysqli->error());
echo "<select>";
while($row=mysqli_fetch_array($result))
{
echo "<option value='<?php echo $row['id'] ?>'>";echo $row["subject"];echo "</option>";
}
echo "</select>";
}
?>
您需要为您的第一个表单命名确定我可以添加按钮,但如何返回最终值……我应该将值标记放在哪里以及如何放置?当您提交表单时,它会将您命名的表单元素推送到整个表单中进行处理。一般来说,假设您有一个名为='name'的名称输入put,那么您需要键入if(isset($_POT['submit']){echo$_POST['name'];}它不起作用,因为它正在发送这些变量以进行检查,但检查不会将它们带回来。将check.php中的代码放入表单中,将该代码放在屏幕顶部,并将表单的方法更改为method=''action=“post”。然后,当您在表单页面上提交表单时,表单变量将显示在屏幕顶部它只是显示数组()
<?php
print_r($_POST); // prints all form field values sent through in the $_POST array
if(isset($_POST['submit']))
{
echo $_POST['listchoice'];
}
?>