Skip to content Skip to sidebar Skip to footer

Pass Variable Between Php And Javascript Without Load Page

I'am new in javascript. This question is for improve my understanding about javascript. Passing variable maybe easy if page is loaded using get or post or request function. How abo

Solution 1:

Its possible through ajax calling in javascript,

<scripttype="text/javascript">var optionValue = document.getElementById('optionValue');

optionValue.onchange = function() {
    document.getElementById('textOption').value = optionValue.value;

  var xmlhttp=newXMLHttpRequest();
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
      document.getElementById("textOption").value = xmlhttp.responseText;
     }
  }
    xmlhttp.open("GET","some_page.php",true);
    xmlhttp.send();
}
</script>

true -> async is false or true.

IN your some_page.php fetch this values and perform your actions accordingly

If you want to learn more ajax in javascript you can refer javascript ajax w3schools

Solution 2:

Your question was not clear but base from what I understand, you want to get whatever the value of hidden field and it will be the selected value of the select box. Am I right?

So you can do,

//in your html
<input type = "text" value = "first" id = "mytext">


var hidden_field = $('#mytext').val();
$('#optionValue option[value="first"]').attr('selected', true);

Disregard this if I miss the concept of the question

Solution 3:

You can use jquery ajax inline with ur php script...

$.ajax({
    url: 'www.example.com/delete_post/<?phpecho$post_id; ?>';
}).done(function() {
    //do some html manipulation here if u want...
});

Post a Comment for "Pass Variable Between Php And Javascript Without Load Page"