1

我想将 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'];

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

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

1 回答 1

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 事件,而不是当前更改的。
再次检查上面的代码。

于 2013-09-11T08:36:48.633 回答