简体   繁体   English

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

[英]How to store ajax value in php variable?

I want to store ajax value 'when selected' stored in php variable. 我想将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>

I've tried this, but didn't succeeded. 我已经尝试过了,但是没有成功。

$releasenu=$_POST['release_no'];

How should i store the ajax value of release_no in php variable? 我应该如何在PHP变量中存储release_no的ajax值? Is there any other way? 还有其他办法吗?

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; }   
          }   }    }

According to your code, you should use this in your select_list.php script: 根据您的代码,您应该在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

I assume you want to have cascade <select> elements, you should change your ajaxReq() function: 我假设您要具有层叠<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;
            }   
        }
    }
}

Your select_list.php should look something like this: 您的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>';

// Code changed due to bug found //由于发现错误,代码已更改
The ajax request should contain one more parameter ( next column) since the returned <select> should have name and onchange event prepared for next, not the currently changed. 由于返回的<select>应该具有为下一个(而不是当前已更改)准备的名称和onchange事件,因此ajax请求应再包含一个参数( next列)。
Check the code above again. 再次检查上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM