-1

I need to pass javascript variable to php. This the code.this function call from dropdown.

 <script>
   function functionname2(){
       var ab = $("#ma").val(); 

      var va= <?php $ab1=ad;  echo TTable::model()->attr($ab1)?>; 
      if(va==='true')
          {
              var r=confirm("Press a button");
               if (r==true)
                {
                    x="You pressed OK!";
                }
                else
                {
                    x="You pressed Cancel!";
                } 
          }
  }   
   </script>

can any one know how to do this ?

5
  • 1
    AJAX will solve your problem Commented Jun 11, 2013 at 10:33
  • @HamZa it seems he totally misunderstood...seeing this line <?php $ab1=ad; echo TTable::model()->attr($ab1)?>; Commented Jun 11, 2013 at 10:33
  • 1
    php executes before JS so you can't pass js variable to php in same page like this and what is ad in your php tag it seems to me that you are passing php var to js Commented Jun 11, 2013 at 10:34
  • hi nu123, see @Ankit comment... describe your case with some more information...so that others will understand your problem better and tend to help you.... Commented Jun 11, 2013 at 10:35
  • 1
    possible duplicate of Transfer javascript var to php var? Commented Jun 11, 2013 at 10:40

3 Answers 3

4

You can't : PHP generates the HTML code of your page, including javascript of course.

Do you pass an argument to God ? Obviously not.

But you can pray, and that's why AJAX has been created later : using asynchronous calls, you can ask javascript to request a PHP service and to render the results without reloading the whole page.

If that's what you are looking for, then take a look at jQuery and its AJAX API if you want to use it without too much pain.

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

Comments

2

You can't because:

PHP is run on server side (like your apache) while Javascript is run on client side (browser). PHP interpreter runs your statements such as echo '<table>....</table>' and finally such prints/outputs are all sent to the web browser hence it only contains whatever you print/ouput but no php script on client side. Javascript, html whatever on the server side, they are just text in php's point of view.

On client side you will only have whatever php generated but not php script so you can't pass like javascript to php. Now browser interprets that text as special text in it's point view depending upon their tags.

But however you can communicate with other php scripts on server by using javascript techniques such as ajax which dynamically sends the data to the php script on the server and takes the output back.

For example, you can dynamically send a search query to the php via database and take back the data from server and display it back on the browser through the technique ajax.

To further explain what you have to do, what approach you have to make. More information on what you want to achieve is needed.

I hope it helps.

Comments

0

Use a form with an element and submit that to your PHP page:

<form name="phpForm" action="" method="post" id="myForm" onsubmit="DoSubmit();">
    <input type="text" name="confirmation" value="" />
</form>

And set the value of the input before you submit it:

if (r==true)
{
    document.phpForm.confirmation.value = "You pressed OK!";
}
else
{
    document.phpForm.confirmation.value = "You pressed Cancel!";
}

document.getElementById("myform").submit();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.