Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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.

share|improve this question
1  
window.location.href is a browser redirection, not an AJAX call. Since you have tagged jquery-ajax, start reading here. –  Michael Berkowski Jan 6 '13 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... –  Michael Berkowski Jan 6 '13 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. –  aug Jan 6 '13 at 15:22

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
    
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. –  Mattias Buelens Jan 6 '13 at 15:21
    
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! –  4Skinz Jan 6 '13 at 15:40

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.

share|improve this answer
    
Awesome! This can be used very nicely in this situation. –  4Skinz Jan 6 '13 at 15:47

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");
              });
share|improve this answer
    
You have not added the parameters one and two into the Ajax call. –  PassKit Jan 6 '13 at 15:26
    
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. –  4Skinz Jan 6 '13 at 15:42
    
ajax will take data from php page then in success option render data on the current page. –  sourcecode Jan 6 '13 at 16:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.