1

Overview - you click on a button(on a javascript page), that will send a request to a php page to pull up a specific array, then that array should be sent back to the javascript page. When that array is passed into the javascript page, I need to pass it into a function for use. Does anyone know of what to do.

What I am currently doing is below -

<form action="GetTest.php" method="post">
    Something: <input id = "something" input type="submit" name="q" on/>
</form>

When i click on submit, I am redirected to my php page which has the array I need(which is expected), but I don't know what to do from there onwards. Any help on this will be very much appreciated.

1
  • 3
    Look into AJAX with JSON, most likely using the jQuery $.ajax() function. Commented Jul 6, 2012 at 16:10

2 Answers 2

2

You have two methods that can help you:

  1. Using GET or POST values - in your case POST. Inside GetTest.php use $_POST variables to handle the data received from your form.

    Reference: $_POST documentation

  2. Using AJAX functionality. In this case you will be using XMLHTTPRequest or $.ajax (if you are about to use jQuery).

    Reference: Using jQuery and AJAX - NetTuts+, or AJAX Tutorial - W3Schools

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

2 Comments

+1 for linking to learning resources for this rather than giving code. Would definitely suggest using the jquery implementation to minimise cross browser issues
Thanks for the help guys, definitely going the jquery route!!
0

First off, handle the HTML so that you aren't passing it over to the PHP form at all (staying on the page):

Changing:

<form action="GetTest.php" method="post">
    Something: <input id = "something" input type="submit" name="q" on/>
</form>

To:

    Something: <input id = "something" input type="submit" name="q" on/>

Then, look into jQuery's AJAX functionality:

$("#something").click(function() {
    $.ajax({
      url: 'GetTest.php',
      type: 'POST',
      success: function(data){
        alert('success');
      },
      error: function(){
        alert('failure');
      }
    });
});

You can retrieve the POST data from GetTest.php and transfer it directly through Javascript as a result.

Good luck!

1 Comment

I think I am missing something, I just wrote this in my code. But, I still get sent to my php page when i click on my button. What I need that button is to pull the php array while still staying on the javascript page, any idea on this at all

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.