user2967835
user2967835

Reputation: 31

Set the value of JavaScript variable into a PHP variable

I want to set the value of a JavaScript variable into a PHP variable. How can that be done?

Kindly give me some suggestions.

My code is :

document.getElementById("dummy").value=  <?php echo ("document.getElementById('txtplan').value"); ?> //document.formn.txtplan.options[document.formn.txt'+fld+'.selectedIndex].value //eval(''); $_POST['txtplan']
    alert(document.getElementById("dummy").value);  


<?php
    $planname = $_POST["dummy"];
    $plandetails = mysql_query("SELECT * FROM membershipplan where planname='$planname'");
    while($row1 = mysql_fetch_array($plandetails))
    {?>
    document.getElementById("txtduration").value=<?php echo $row1['planduration']; ?>
    document.getElementById("txtplancontact").value=<?php echo $row1['plannoofcontacts']; ?>
    document.getElementById("txtplanamount").value=<?php echo $row1['planamount']; ?>

    <?php
    }
?>

});

am not do with this means kindly give me the alternative way for the above process

Upvotes: 2

Views: 3463

Answers (3)

JaiSat
JaiSat

Reputation: 2386

The answer is simple. Impossible. Because,

  • Java script is a client side scripting. Client machine can handle this script, If it is possible to assign javascript value to PHP variable that leads to some sort of vulnerability.
  • So this can't be done.

If you want to accomplish something by doing this kind of assignment, Definitly there would be a way of doing it. Post another question what do you want to do, you will be showered with answers.

Upvotes: 1

R R
R R

Reputation: 2956

You cant do that i.e you cant assign a javascript variable to a php variable (unless you are using cookie or ajax) but u can assign a php variable to a javascript variable using

var a=<?php echo $a ?> ;

but be careful with that as the php variables will be executed in the server.

Upvotes: 1

Roko C. Buljan
Roko C. Buljan

Reputation: 206669

JS -> PHP = impossible (only if you send that info to PHP using POST or GET)  
PHP -> JS = possible   var text = <?php echo( $text ); ?> ;

The only reason for that is that the PHP code is executed on your server.

Upvotes: 2

Related Questions