Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 about passing variable between php and javascript without loading the page? Let say i have this code

<body>
    <input type='hidden' name='textOption' id='mytext' /><br/>
    <?php
        // Get the value from <input type=hidden ....> from javascript to set as other             variable
        // For example but not logic
        // $variableFromJS = document.getElementById('textOption').value;
    ?>
    <select id="optionValue">
        <option value='none'>--Select--</option>
        <option value="first">First</option>
        <option value="second">Second</option>
        <option value="third">Third</option>
    </select>
    <script type="text/javascript">
    var optionValue = document.getElementById('optionValue');

    optionValue.onchange = function() {
        document.getElementById('textOption').value = optionValue.value;
    }
    </script>
</body>
share|improve this question
1  
You cannot because the basic point is, your PHP code is executed before any HTML or Javascript. If you need to pass some data to PHP without page reload, search on AJAX, it's not very difficult to use. – I Can Has Kittenz Jan 29 '14 at 4:54
1  
Can you clarify what it is you are trying to do here? The concept you are looing for is called AJAX. This is a method for passing information to and from the server, using client side (Javascript) code. – Ben A. Hilleli Jan 29 '14 at 4:54
up vote 3 down vote accepted

Its possible through ajax calling in javascript,

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

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

  var xmlhttp=new XMLHttpRequest();
  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

share|improve this answer
    
so how to to pass that value to variable in php? sorry i dont get it. – Mohd Dhiyaulhaq Jan 29 '14 at 6:28
1  
edit this function as this xmlhttp.open("POST","some_page.php?q="+variable_name,true); and in php function $_REQUEST["q"]; use this code to fetch the value of the variable, use echo to see the value of q – Vikarn Singh Mankotia Jan 29 '14 at 7:38

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

share|improve this answer
1  
Not at all, the OP actually wants to pass option value through the hidden variable to PHP. – I Can Has Kittenz Jan 29 '14 at 5:21
    
I did not get the question correctly so why the votedown? I did have this line Disregard this if I miss the concept of the question on my answer. – HTTP Jan 29 '14 at 5:32
    
The down-vote is not by me, I was simply explaining it to you. – I Can Has Kittenz Jan 29 '14 at 5:37
    
So the problem is that if the select box on change, whatever the value of the select box then it will be the value of the hidden field? – HTTP Jan 29 '14 at 5:46
1  
He is already doing that, he now wants THAT hidden value to be passed to PHP. If you see the commented code inside his PHP block, it says //get the value from <input type=hidden ....> from javascript to set as other variable – I Can Has Kittenz Jan 29 '14 at 5:48

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

$.ajax({
    url: 'www.example.com/delete_post/<?php echo $post_id; ?>';
}).done(function() {
    //do some html manipulation here if u want...
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.