简体   繁体   中英

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

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.

You cant do that ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM