列出选择和处理表单php的数据库值

时间:2014-06-24 19:16:05

标签: php mysql ajax

我使用以下帖子从我的数据库表创建一个过滤的下拉列表,它工作正常。

Listing database values according to the selected filter in dropdown
在使用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

Jquery的:

function filter(item){
      $.ajax({
        type: "POST",
        url: "filter.php",
        data: {value: item},
        success: function(data){
            $("#results").html(data);
        }
      });
} 

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>

现在我的问题是当我提交表单时,它将值发送到filter.php而不是process.php

请问,我该如何解决这个问题呢? 感谢

根据user1406062的网络控制台请求 web console

我的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;

1 个答案:

答案 0 :(得分:0)

好吧,在你的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

即可