-3

I have a select box with value

<select onchange="javascript:get_id_val(this)" id="variation_select_61_10" name="variation[10]" class="wpsc_select_variation">
    <option value="0" disabled="disabled">-- Please Select --</option>
    <option value="13">L</option>
    <option value="12">M</option>
    <option value="11">S</option>
</select>

I am using java script to get the value of select box

function get_id_val(val){
    alert(val.value);   
}

How can I get he value of alert which is option value in php variable for use in mysql query?

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
Vikas Gautam
  • 997
  • 7
  • 23

4 Answers4

1

You do it with ajax make sure you have included jquery

function get_id_val(val){
$.ajax( {
    type: 'POST',
    url: your.php,
    data: "&val=" + val, 
    success: function(data) {
        alert("data")
    }
} );
}
// in your php file
echo     $_POST['val'];
//mysql_query("....");

Hope this makes sense

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
1

if it for only getting the value of the selected option then simply go through with this in the javascript

var Value = $("#variation_select_61_10").val();
alert(Value);

no need to pass any value in the onchange function

kumar
  • 1,796
  • 2
  • 15
  • 37
0

on change function you can assign the value of select box and then submit the form at the php you can retrieve that value in post data

  function get_id_val(val){
      document.getElementById('variation_select_61_10').value = val.value;
     document.getElementById('form-id').submit();

    }

on php

$value = $_POST['variation'];
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
0

You should use Ajax,

index.php

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function get_id_val(val){   
    var option = val.options[val.selectedIndex];
    $.ajax({
        type: "POST", 
        url: "sql.php",
        data: "value="+option.value,
        success: function(message){ 
            $("#result").html(message);
        }       
    });

}
</script>
<select onchange="javascript:get_id_val(this)" id="variation_select_61_10" name="variation[10]" class="wpsc_select_variation">
    <option value="0" disabled="disabled">-- Please Select --</option>
    <option value="13">L</option>
    <option value="12">M</option>
    <option value="11">S</option>
</select>

<div  id="result">
*/result shows here /*
</div>

sql.php

<?php 
$value = $_POST['value'];
echo " INSERT INTO ..... WHERE value = '$value' ";

?>

I hope this will help you.

6339
  • 475
  • 3
  • 16