rwells2003 Posted September 26, 2010 Share Posted September 26, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/214481-how-do-i-use-a-javascript-variable-as-a-parameter-in-a-php-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 26, 2010 Share Posted September 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214481-how-do-i-use-a-javascript-variable-as-a-parameter-in-a-php-function/#findComment-1116086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.