2

Im trying to pass two JavaScript variables to PHP variables.

function myFunction(){
  var one = 'one';
  var two = 'two';
}

After doing my research, I have pretty much come to the conclusion I am going to have to use AJAX, something like

window.location.href = "myphpfile.php?one=" + one;
window.location.href = "myphpfile.php?two=" + two;

But Im having trouble picking up the variable in PHP using

$_GET['one']
$_GET['two']

Im also confused as to whether I am calling the php script twice. Is there a working way to pass two variables? I have found several ways to pass one but none to pass two.

3
  • 1
    window.location.href is a browser redirection, not an AJAX call. Since you have tagged jquery-ajax, start reading here. Commented Jan 6, 2013 at 15:19
  • 3
    Your call would be something like $.get('myphpfile.php?one=one&two=two'...), but there's more to it than that... Commented Jan 6, 2013 at 15:19
  • Can you be a little more specific as to how you're having trouble picking up a variable? Any errors you can tell us? perhaps to an isset($_GET['one']) and tell us what happens. Commented Jan 6, 2013 at 15:22

3 Answers 3

5

The following will send the two variables in a single request

window.location.href = "myphpfile.php?one=" + one + "&two=" + two;

but window.location.href is not an AJAX request. Calling the above code will cause the browser location to change to myphpfile.php.

See this page for how to get started with AJAX, or alternatively you could consider using jQuery.

Sign up to request clarification or add additional context in comments.

2 Comments

This will correctly call the PHP script only once, but it is not an AJAX call. As @MichaelBerkowski recommended, have a look at jQuery.ajax.
Thanks, Im a bit of a beginner with JQuery so I will have to wait until I have found my way around it. I would prefer not to have the page redirect but I can work with it. Needed a fast solution and I got one, thanks heaps!
2

If you just want to "fire and forget" those variables, you don't need AJAX. Common way (not elegant, but working) is using hidden frame:

<iframe id="MyFrame" style="display: none;"></iframe>

Then in your code:

function myFunction(){
    var one = 'one';
    var two = 'two';
    document.getElementById("MyFrame").src = "myphpfile.php?one=" + encodeURIComponent(one) + "&two=" + encodeURIComponent(two);
}

This will cause PHP to receive the request, however if you want to read and parse the result, you better use "real" AJAX.

Comments

1

if you only want to send variables to another page then use:

           window.location.href = "myphpfile.php?one=" + one + "&two=" + two;

if you want ajax request:

            $.ajax({
               url: "myphpfile.php",
               method: 'get',
               data : {one:"one",two:"two"},
              success:function() {
              alert("hi to all");
              });

3 Comments

You have not added the parameters one and two into the Ajax call.
Will this ajax request run the PHP file or just save the date to be collected when the script is called? I absolutely know nothing about AJAX and read that I needed to use it to get the data from client side to server side.
ajax will take data from php page then in success option render data on the current page.

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.