列出用于选择和处理表单php的数据库值
我已经使用下面的帖子从我的数据库表中创建了一个过滤下拉列表,它工作正常列出用于选择和处理表单php的数据库值,php,mysql,ajax,Php,Mysql,Ajax,我已经使用下面的帖子从我的数据库表中创建了一个过滤下拉列表,它工作正常 在我使用ajax编辑之前,我的process.php工作得非常完美,没有任何问题,即使在我做了更改之后,使用select选项进行过滤也工作得很好 上述帖子中的代码,答案已被接受。 html <select name="filter" onchange="filter(this.value)"> <option>FILTER:</option> <option value="
在我使用ajax编辑之前,我的process.php工作得非常完美,没有任何问题,即使在我做了更改之后,使用select选项进行过滤也工作得很好 上述帖子中的代码,答案已被接受。
html
<select name="filter" onchange="filter(this.value)">
<option>FILTER:</option>
<option value="alphabetical">ASC</option>
<option value="date">Date</option>
</select>
<div id="results"></div>// store the results here
filter.php:
include "connection.php"; //database connection
$fieldname = $_POST['value'];
if($fieldname == "alphabetical"){
// if you choose first option
$query1 = mysqli_query("SELECT * FROM table ORDER BY name ASC");
// echo the results
}else{
// if you choose second option
$query1 = mysqli_query("SELECT * FROM table ORDER BY date ASC");
// echo the results
}
我的表格是这样的
<form action="process.php" method="post">
<label>Code</label><input type="text" name="code" />
<label>Course</label>
<div id="results"></div>
<input type="submit" value="Submit" />
</form>
在JavaScript函数中,您有硬编码的URL:
function filter(item){
$.ajax({
type: "POST",
**url: "filter.php",**
data: {value: item},
success: function(data){
$("#results").html(data);
}
});
}
您没有使用表单操作URL。。。只需将filtert.php更改为process.php如果您使用的是web开发者控制台,即Chrome、Safari或FireFox中提供的控制台,是否可以包含网络选项卡的快照?我已经用快照更新了我的问题谢谢添加快照。查看网络选项卡,我可以看到状态302,这意味着重定向请求。您能在
process.php
中显示代码吗?谢谢您的回复。我已经添加了process.php中的代码。如果没有ajax,它可以正常工作。我建议您避免在类中使用$\u POST
、$\u GET
和其他特定于服务器的变量。这使得单元测试更容易。无论如何,您可以将所有头(…)
命令更改为die(…)
?这将向您展示在重定向之前已经执行了哪些部分。我已经解决了这个问题,并不是Ajax导致了错误。这是来自my process.php的重定向之一。如果你检查我的代码片段,你会发现我有filter.php和process.php两个单独的函数。
include("../include/session.php");
class UserProcess
{
/* Class constructor */
function UserProcess(){
global $session;
if(!$session->isUser()){
header("Location: ../index.php");
return;
}
/* Student submit forms to register course */
if(isset($_POST['subreg'])){
$this->procReg();
}
else if(isset($_POST['subrem'])){
$this->procRem();
}
else{
header("Location: ../index.php");
}
}
function procReg(){
global $session, $form;
$_POST = $session->cleanInput($_POST);
$retval = $session->RegU($_POST['courseid'], $_POST['user']);
/* Add Successful */
if($retval == 0){
$_SESSION['addsuccess'] = true;
header("Location: ".$session->referrer);
}
/* Error found with form */
else if($retval == 1){
$_SESSION['value_array'] = $_POST;
$_SESSION['error_array'] = $form->getErrorArray();
header("Location: ".$session->referrer);
}
/* Add failed */
else if($retval == 2){
$_SESSION['addsuccess'] = false;
header("Location: ".$session->referrer);
}
}
$userprocess = new UserProcess;
function filter(item){
$.ajax({
type: "POST",
**url: "filter.php",**
data: {value: item},
success: function(data){
$("#results").html(data);
}
});