0

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.

6
  • 4
    you have multiple ? in the url, you only need the first one, the others should be & Commented Apr 11, 2014 at 12:22
  • query string separator is & not ? Commented Apr 11, 2014 at 12:24
  • Yes that was my bad, that wasn't the issue. It still wont redirect Commented Apr 11, 2014 at 12:30
  • As one of the other answers mentioned, you are performing a GET request. Do you need to perform a POST request? Is your PHP script trying to access the request info using $_POST? Commented Apr 11, 2014 at 12:41
  • Yes I believe i need POST request Commented Apr 11, 2014 at 12:59

7 Answers 7

6

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;
Sign up to request clarification or add additional context in comments.

4 Comments

My mistake, but that wasn't the problem, it still wont redirect.
@blackfilms: what do you mean with "it won't redirect"? Are you getting a 404 (not found) error?
No it just stays on the same page. I need it to go to test.php?name=myname&time=44&moves=2
@blackfilms: do you see any error logged on browser console? The code you provided should work
1

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

4 Comments

I have tried this also and still wont redirect. I asked if it had something with the time variable since its still changing. After the prompt screen it just stays on same page like nothing happened
@blackfilms It has to be a JavaScript error. Probably sec or number_moves are not defined. Look in your JavaScript Console to see if you have any errors.
I tested with alert to see if the variables are defined and they appear on the alert, would that mean they are defined? or should i convert them to another variable in this to be sure?
You should check for JavaScript errors on your site. If you use Chrome, here's an introduction to their Developer Tools: developers.google.com/chrome-developer-tools
1

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 ?

1 Comment

Problem still wont redirect
0

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!

Comments

0

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.

3 Comments

I changed it, but problem is it wont redirect
Did you try using a / at the start of the URL.
AJAX POST is just slightly less prone to security attacks... The important thing is sanitizing info server-side. I think the problem in this case might be very hard to solve anyway, securing client/server exchanges is always a pain in the a**
0

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

Comments

0

You need to declare sec and number_moves as variables. That's the only problem I see when I tested it.

1 Comment

Its working now, it was my mistake number_moves was number_tries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.