如何在php变量中存储ajax值?

时间:2013-09-11 07:30:07

标签: php ajax jquery

我想存储ajax值''选中'存储在php变量中。

 <select name="client_name" onchange="ajaxReq('product_name', this.value);">
    <option>- - -</option>
    <option value="SANY">SANY</option>
    <option value="MBFC">MBFC</option>
</select>
<span id="slo_product_name"> </span>
<span id="slo_release_no"> </span>
<script type="text/javascript">
var ar_cols = ["client_name","product_name","release_no",null]; var preid = "slo_";
</script>

我试过这个,但没有成功。

$releasenu=$_POST['release_no'];

我应该如何在php变量中存储release_no的ajax值?还有其他办法吗?

function ajaxReq(col, wval) {
  removeLists(col);           
  if(wval!='- - -' && wval!='') {
      var request =  get_XmlHttp();
      var php_file = 'select_list.php';
      var  data_send = 'col='+col+'&wval='+wval;
      request.open("POST", php_file, true);
      document.getElementById(preid+col).innerHTML = 'Loadding...';   
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      request.send(data_send);
      request.onreadystatechange = function() {
  if (request.readyState==4) {
      document.getElementById(preid+col).innerHTML = request.responseText; }   
          }   }    }

1 个答案:

答案 0 :(得分:1)

根据您的代码,您应该在select_list.php脚本中使用它:

$col  = $_POST['col'];
$wval = $_POST['wval'];

// now you can use those variables to save to DB, or get some data out of DB

我假设您想要级联<select>元素,您应该更改ajaxReq()函数:

function ajaxReq(col, wval) {
    // first remove all selects that are after this one
    var remove = false;
    var next   = null; // we also need to trap next select so we can fill it
    for (i in ar_cols) {
        if (ar_cols[i] == col) {
            remove = true; // from now on, remove lists
        } else if (remove) { // if ok to remove
            if (!next) {
                next = ar_cols[i]; // now we found next column to fill
            }
            if (ar_cols[i]) { // remove only non null
                removeLists(ar_cols[i]);
            }
        }
    }      
    if(wval!='- - -' && wval!='' && next) { // also if there is column after to fill
        var request   = get_XmlHttp();
        var php_file  = 'select_list.php';
        var data_send = 'col='+col+'&wval='+wval+'&next='+next; // also add next param
        request.open("POST", php_file, true);
        document.getElementById(preid+col).innerHTML = 'Loadding...';   
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(data_send);
        request.onreadystatechange = function() {
            if (request.readyState==4) {
                // we fill next select
                document.getElementById(preid+next).innerHTML = request.responseText;
            }   
        }
    }
}

您的select_list.php应该是这样的:

$col  = $_POST['col']; // this is just a filter for values in next <select>
$wval = $_POST['wval'];
$next = $_POST['next']; // we need to get this param or we cannot setup next <select>

// using $col and $wval variables get values from DB

echo '<select name="' . $next . '" onchange="ajaxReq(\'' . $next . '\', this.value);">';
foreach ($data as $row) {
    echo '<option ...>...</option>'; // fix this to work for you
}
echo '</select>';

//由于发现错误,代码已更改 ajax请求应该包含一个参数(next列),因为返回的<select>应该具有为下一个准备的名称和onchange事件,而不是当前更改的。 再次检查上面的代码。