0

I have a page where users fill out $_GET data for some options. I'd like to pass these $_GET variables using AJAX to a .php script. But my issue is how do I pass those $_GET variables they filled out so far, without refreshing the page?

Here is my code so far.

$.ajax({
type: "GET",
url: "serverside script to process on data",
data:{name:youwant}, // Here is where I want to take what the user has filled out so 
                     // far, and place it here all without refreshing the page
success: function(data){
     alert("return here if success")

}
})
Necro.
  • 987
  • 6
  • 17
  • 29

2 Answers2

1

I don't know your code, but you can have a form, but instead of submit it, you put a onsubmit method to a javascript function. In that function you gather all variables and pass it through ajax.

Example: <form name="form1" method="get" onSubmit="return send()">

<script>
function send() {
 $.ajax(...);

return false;

}
</script>

You can use seralize function to send in $.ajax data field

Nuno Costa
  • 423
  • 5
  • 11
1

First of all, drop this task into small ones:

1) Get/process variables in JavaScript

2) Send them to PHP

3) Parse/handle the ones

4) Depending on result send respond back to JavaScript

5) Handle that respond and display a message to user

Take a look at this example, Let's assume that jquery.js is loaded. Assume that we want to send the values of the inputs we have - email and password.

<script type="text/javascript">

  $("#Send").click(function(){

     $.ajax({

         type : "GET", 
         //Look carefully:
         data : {
           // it'll be PHP vars       // This is JS vars
           email                :   $("#email").val(),
           password             :   $("#password").val()
         },

         success : function(respondFromPHP){

              alert(respondFromPHP);
         }
      });


  });

</script>

<input type="text" id="email" />
<input type="password" id="password" />

<br />
<button id="Send">Send to php</button>

In your php script, just handle vars you get, like this:

<?php

print_r($_GET); // will print smth like Array("email" => "foo", "password" => "bar")

// Then create function so that you can simplify handling of the vars.
// Like this:
function validate_password($password){}
function validate_email($email){}
Yang
  • 8,580
  • 8
  • 33
  • 58