douying6206
douying6206
2013-09-11 07:30

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

已采纳

I want to store ajax value 'when selected' stored in php variable.

 <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? 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; }   
          }   }    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsdsm2016 dsdsm2016 8年前

    According to your code, you should use this in your select_list.php script:

    $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:

    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:

    $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.
    Check the code above again.

    点赞 评论 复制链接分享

为你推荐