1

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>
2
  • 2
    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. Commented Jan 29, 2014 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. Commented Jan 29, 2014 at 4:54

3 Answers 3

3

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

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

2 Comments

so how to to pass that value to variable in php? sorry i dont get it.
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
0

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

5 Comments

Not at all, the OP actually wants to pass option value through the hidden variable to PHP.
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.
The down-vote is not by me, I was simply explaining it to you.
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?
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
0

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...
});

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.