Reputation: 105
I am trying to send my variables to a php file and I want to prompt before submitting to have the user input his name before submitting. The page won't redirect to the php page as listed below. Any ideas why it is not redirecting? Do I need to include a jquery or some other script source I do not know about
var name = prompt("Enter your name to submit score");
window.location.href = "test.php?time="+sec+"&name="+name+"&moves="+number_moves;
also when testing this on the php page with get function it only seems to work if the name variable isn't first. If i have the name,time,moves i get errors.
Upvotes: 0
Views: 92
Reputation: 2289
As the other guys have mentioned, you need to separate your URI params with an ampersand, not a question mark.
Further, it would be recommended to encode the value that you're receiving from your user, since you never know if they are going to break your code with values you did not expect.
This can be done with the encodeURIComponent
method as such:
var name = window.prompt('Enter your name'),
url = 'test.php' +
'?time=' + sec +
'&name=' + encodeURIComponent(name) +
'&moves=' + number_moves;
location.href = url;
Read more about this function on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
*Fixed the time variable to sec from src
Upvotes: 1
Reputation: 124
You need to declare sec and number_moves as variables. That's the only problem I see when I tested it.
Upvotes: 0
Reputation: 42736
now that you fixed the separators, all the variables passed through the url are considered GET variables so they will be in the $_GET global variable in your php.
To get the variables in the global $_POST variable you would need to use a form with the method set to post or an ajax request that posts them.
HTML
<form id="myform" method="post" action="test.php" method="POST">
<input type="text" name="name" id="nameInput">
<input type="text" name="moves" id="movesInput">
<input type="text" name="time" id="timeInput">
<input type="submit" value="Submit">
</form>
You can still modify the values of the inputs through javascript but you will need to use DOM api methods. There quite a few DOM methods so look through the various references online like the mozilla developers network site
document.getElementById("nameInput").value = "Some Name";
//Though i wouldnt suggest using prompts to get
//data you can still do it
document.getElementById("nameInput").value = prompt("Enter your name");
There are also ways of submitting the form from javascript if needed
document.getElementById("myform").submit();
As for why your page is not changing after changing the href, this is probably due to an error in your javascript which actually makes it so the javascript statement to change the href to not actually execute. You can check for errors by looking in the javascript console.
The console will be in a developer tools window, usually opened by hitting F12 on chrome and IE, firefox and other will have a different shortcut key. If your prompt is showing up, then the error has to be with your string concatenation below it, it will error out if the variables are undefined.
//Will error out because there is no variable someNameVar...
location.href = "test.php?name="+someNameVariableThatDoesntExist;
//in console would see something like
//ReferenceError: someNameVariableThatDoesntExist is not defined
Upvotes: 0
Reputation: 7854
You have to delimit the query string parameters using & instead of ?.
var name = prompt("Enter your name to submit score"); window.location.href = "test.php?time="+sec+"&name="+name+"&moves="+number_moves
IMHO, please consider using the AJAX POST for sending values to server as they are prone to security attacks.
Upvotes: 0
Reputation: 6946
First of all, you're not actually posting them as using this code sends them via the GET method. But that's for precision.
Now your problem is most probably that you should write this :
window.location.href = "test.php?time="+sec+"&name="+name+"&moves="+number_moves;
Note the &
separator between the parameters, where you had a second wrong ?
Upvotes: 1
Reputation: 3504
You only need one question mark. After that the multiple variables should be seperated with the & symbol:
window.location.href = "test.php?time="+sec+"&name="+name+"&moves="+number_moves;
Hope this helps!
Upvotes: 0
Reputation: 68400
For sure you have an error on querystring separator. Replace all ?
except the first by &
window.location.href = "test.php?time="+sec+"&name="+name+"&moves="+number_moves;
Upvotes: 6