How to read variable from php form in Javascript

Rodrigo Costa
Rodrigo Costa used Ask the Experts™
on
Hi folks I need to make a variable passed to the current php file to be able to be read from a javascript that is within this php file.

The variable is passed from a form as $stri , I need to acces this variable from the following javascript:

if(variableName.match(/for123/i)){
  alert("matched");
}else{
  alert("not matched");
}                                            

Open in new window


Any help would be much appreciated.
Cheers!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think I get what you are saying...

The form posts to this page with the javascript, you have the variable $stri passed in from the form. In which case the solution would simply be to add a line like this before the code above...

var variableName = '<?=str_replace("'","\\'",$stri);?>';
Rodrigo CostaLecturer

Author

Commented:
Yep that is it exactly. Cheers mate!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial