根据选择选项(AJAX?)自动更改PHP变量


Automatically change PHP variable based on select option (AJAX?)

现在,我有默认为当前d/m/y的变量,然后将其弹出到mySQL查询中,该查询显示表WHERE date='$dday'AND month='$dmonth'AND year='$dyear'中的所有数据。

$ddate = date("d");
$dmonth = date("m");
$dyear = date("Y");

相反,我希望有一个选择框,可以根据选择的选项更改变量。因此,Day select的默认选项将是当前日期,但如果我将其更改为第12天,例如,我希望在select选项更改时更改变量,然后自动重新查询数据库。我假设这将通过AJAX来完成。

我所说的可能吗?如果查询的自动化增加了很大的复杂性,我只需要更改变量并根据提交按钮进行更新就可以了。

我保证,如果有人问到低于我简单水平的问题,我会暂停提问,开始回答一些问题。

是的,这是可能的。jQuery甚至让它变得简单。

  1. 创建您的<select>元素并给它一个ID,以便它可以在jQuery中使用。

  2. 在jQuery中添加一个事件侦听器,该侦听器在<select>更改时激发,如$("#date").change()

  3. 在更改事件的事件处理程序中,获取<select>的当前值,然后使用jQuery的AJAX $.post()函数将该数据发送到PHP文件。

  4. 在该PHP文件中,清除数据以防止MySQL注入,然后在数据库中查询新数据。

  5. 使用PHP的echo函数发回数据。

  6. 在jQuery $.post()回调函数(第三个参数)中,接收回显的数据并将其放入变量中。

  7. 使用jQuery用数据更新HTML。

您建议的两种解决方案都可以工作。您可以使用AJAX将选择框中的值发送到php脚本,也可以提交表单并通过$_POST$_GET访问它们,具体取决于您的表单方法。

下面是一个例子,我让你来做查询:

<?php 
//Some pseudo data kinda as your receive it from your query
$datafromSql = array(
array('id'=>1,'date'=>1,'month'=>1,'year'=>2012,'theData'=>'This is some data when the user select 1/1/2012'),
array('id'=>2,'date'=>2,'month'=>2,'year'=>2012,'theData'=>'This is some data when the user select 2/2/2012'),
array('id'=>3,'date'=>3,'month'=>3,'year'=>2012,'theData'=>'This is some data when the user select 3/3/2012'),
);
//Super simple API to access the data
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
    header('Content-Type: text/html');
    //pseudo code, really you would just format your query result   
    $return=array();
    foreach($datafromSql as $row){
        //Select all from array which match select choice
        if($row['date']==$_POST['day'] || $row['month']==$_POST['month'] || $row['year']==$_POST['year']){
            $return[]=$row['theData'].'<br />';
        }
    }
    //output, with a fancy horizontal rule
    echo implode('<hr />',$return);
    die;
}?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
function update(){
    $.post('./<?php echo basename(__FILE__)?>',
       {
        day:  $("#day").val(),
        month: $("#month").val(),
        year:  $("#year").val()
       },
    function(data) {
            $('#result').replaceWith('<div id="result"><h1>The Result:</h1>'+ data +'</div>');
    });
}
$(document).ready(function(){
    update();
});
</script>
</head>
<body>
<form id="dateform" >
 <p>Select Date: 
  <select size="1" name="day" id="day" onChange="update()">
  <?php foreach(range(1,31) as $i){echo '<option value="'.$i.'">'.$i.'</option>';} ?>
  </select>
  <select size="1" name="month" id="month" onChange="update()">
  <?php foreach(range(1,12) as $i){echo '<option value="'.$i.'">'.$i.'</option>';} ?>
  </select>
  <select size="1" name="year" id="year" onChange="update()">
  <?php foreach(range(2008,2012) as $i){echo '<option value="'.$i.'">'.$i.'</option>';} ?>
  </select>
 </p>
</form>
<p id='result'></p>
</body>
</html>