I have a question I have been struggling with for a while, I think I might be taking the wrong approach or not understanding the basics. But I am looking for a solution, so if I have it the "wrong way" please let me know.
I am building a login/registration system. The key to the "app" is that you don't input your own username in a textbox. Instead, I have built a jQuery application which helps you choose a username (the reason being that the target market is children).
At the jQuery stage, the app has three stored string variables = wordOne, wordTwo and wordThree.
I have already built a registration functionality (PHP & SQL) but at present it uses a standard text input to grab the username. So I wanted to change this up by passing the wordOne, wordTwo and wordThree to the username div, then use PHP to grab the value and use that to create the username entry (which is posted through a form).
One of the issues I am having is: how do I pass these three JavaScript variables from JS to PHP?
My research has given me the understanding that this must be done using an AJAX request as there needs to be communication between serverside and clientside. This works great for me because I am needing to use AJAX to reload the next section of the site.
So... my major question is how do I instruct a PHP function to process on the success of the AJAX, or what else do I link it to so that it runs when the page loads?
Here is what I am doing at present. In the AJAX call, I have added the variables mentioned above and reassigned their values to the PHP variables one, two and three
function saveUsername(wordOne, wordTwo, wordThree){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("login-register-wrapper").innerHTML=xmlhttp.responseText;
}
}
As a closeup, here is the line that I (hope) assigns the JS variables to new PHP variables, or at least gives me a way to grab them in PHP.
`xmlhttp.open("GET","register-form.php?one=wordOne&two=wordTwo&three=wordThree",true);`
This all works fine. But then I run into an issue. I want to do the following things to these variables. 1) Uppercase the first letter. 2) Concatenate them together into one string. 3) Assign this string to the variable $username
So I wrote this:
$_GET['one'], $_GET['two'] and $_GET['three'];
// Capitalise first letter
$one = ucfirst($one);
$two = ucfirst($two);
$three = ucfirst($three);
$username = $one . $two . $three;
I haven't been able to full test the code but you get the idea. ucfirst for the caps, then concatenate and store in the variable.
The thing I don't understand is where does this script go? where do I call this function? I know if this was JavaScript, I'd put it in here:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
}
But I just can't get my head around where I call my little capital and concatenate script so that it runs as the AJAX page is loaded in.
If you look through the site, which will only take a few seconds, you will see what I am attempting to do. Am I going about this completely the wrong way? I've tried everything, asked on a bunch of forums and no one has the answer, or seems to be able to help me out. What would you do to:
1). Pass JS variables to PHP
2). Run an AJAX script to load a new file into the page asynchronously
3). Run a script which capitalises and concatenates
4). Store that in a variable.
5). Echo that variable on the newly loaded in page.