Using a PHP variable inside JavaScript -
I am very new, so be easy. I am using a session in PHP with some variables when I click on a button so I want to add two strings from my PHP session. I'm using JavaScript onclick to try and get it on.
I have trouble accepting my javascript my PHP variable. Do I have any suggestions?
PHP:
$ _ session ['user1'] = $ _POST ['user']; $ _SESSION ['string3'] = $ _POST ['string1']; $ _SESSION ['string4'] = $ _POST ['string2']; Echo $ _SESSION ['user1']; $ _SESSION ['string3'] resonate; Copy $ _SESSION ['string4']; $ (Session = $ [session '[user1'])) {echo 'Welcome,' $ _ Session ['user1'] "." $ _ Session ['string3']. "And". $ _ Session [string4 ']; } Else {echo "
JS:
& lt; Script type = "text / javascript" & gt; Function xy () {var x = "& lt ;? php $ _SESSION ['string3'];? & Gt;"; Var y = "& lt ;? php $ _SESSION ['string4'];? & Gt;"; Document.getElementById ("Answer"). InnerHTML = x + y; } & Lt; / Script & gt;
You have to resize the PHP variable there because otherwise it is nothing and the JS variable is empty Ends:
var x = "& lt; php echo $ _SESSION ['string3'];? & Gt;"; Var y = "& lt ;? Php $ e_SESSION ['string4'];? & Gt;"; For example, if $ _ session ['string3']
is "ABC", then PHP will resize the value inside JS and you will get: var X = "abc"; Instead of
:
var x = ""; // There is no output from PHP when there is no buzz
To make things easier, here is your entire task:
script type = "text / javascript" & gt; Function xy () {var x = "& lt ;? php echo $ _SESSION ['string3'] ;? & gt;"; Var y = "& lt ;? php $ e_SESSION ['string4'];? & Gt;"; Document.getElementById ("Answer"). InnerHTML = x + y; } & Lt; / Script & gt;
Comments
Post a Comment