1

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; }   
          }   }    }
4
  • 2
    where is your ajax function Commented Sep 11, 2013 at 7:32
  • 1
    Can we see ajaxReq function Commented Sep 11, 2013 at 7:32
  • PHP script runs on servers side and Ajax runs on client side so you can not assign ajax value to php variable, yes you can Manipulate DOM after Ajax call. Commented Sep 11, 2013 at 7:32
  • You are sending 'col='+col+'&wval='+wval; to your PHP script. The index release_no was not passed at all. Commented Sep 11, 2013 at 7:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.