Jump to content

How do I use a JavaScript variable as a parameter in a php function?


rwells2003

Recommended Posts

Hi all,

 

I'm trying to create a webpage that allows users to create an account with a username, password, and security question.  My database stores this information, and when a new user signs up, I want to make sure they don't pick a user name that already exists, because I need to keep them unique for relational data purposes.  So, I wrote a function in php that fills an array with the usernames that are already there, and then compares them in a loop to the username that the user has entered in the html form.  I was hoping not use the method of sending JavaScript data to php via an href, because I also have JavaScript validation that occurs on the form submit, and I think linking somewhere before that to check the username would screw up the rest of the validation. 

 

So what I tried was to store the following string in a php variable:    "<script language=javascript>document.write(document.create_account_form.txtUsername.value);</script>";

 

When I use echo to print the variable that holds this string in php, it indeed prints out the result I want, which is the literal value of txtUsername.  However, when I pass this string variable as a parameter to my username comparison function in php, I get a nonsensical result, because php doesn't realize that the angle brackets and the script identifier and so forth are to be interpreted in a certain way.  So, it just thinks it's a plain string, instead of fetching the txtUsername.value from JavaScript. 

 

Any suggestions on how I can get this JavaScript data into a literal string that php can recognize? 

Link to comment
Share on other sites

I wrote a function in php that fills an array with the usernames that are already there

 

^^^ why not just test if the entered username is already in the database directly in the query. If the query returns a matching row, the username is already taken. No need to spend the time and memory to retrieve everything and loop through it using some slow parsed, tokenized, interpreted php code.

 

As to your main question, javascript executes in the browser, long after any php code on the page has completed its execution. Php is a server side scripting language. The ONLY way you can get a value from the browser to the server is by making a HTTP request and including that value as part of the request.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.