user1410070
user1410070

Reputation: 33

Passing variables to a PHP file through jQuery

Really not familiar with jQuery. Is there anyway I can pass form data to a PHP file using jQuery?

FORM:

<div id="dialog-form" title="Fill in your details!">
  <form>
<fieldset>
  <label for="name">Name</label>
  <input type="text" name="name" id="name"/>
  <label for="email">Email</label>
  <input type="text" name="email" id="email" value=""/>
  <label for="phone">Phone</label>
  <input type="phone" name="phone" id="phone" value=""/>
</fieldset>
  </form>

It's a pop-up dialog with jQuery and gets submitted with:

$("#dialog-form").dialog({
  autoOpen: false,
  height: 450,
  width: 350,
  modal: true,
  buttons: {
    "Sumbit": function() {
    //VALIDATES FORM INFO, IF CORRECT
      if (Valid) {
        $.ajax({
          url: 'process-form.php',
          success: function (response) {
          //response is value returned from php
            $("#dialog-success").dialog({
              modal: true,
              buttons: {
                Ok: function() {
                  $(this).dialog("close");
                }
              }
            });
          }
        });
        $(this).dialog("close");
      }
    }
  }
});

What I want to do is to send the form data that the user enters into process-form.php, where it will be processed and sent as an email (which I can do). Just not to sure on the jQuery side of things. Is it even possible?

Upvotes: 3

Views: 489

Answers (5)

user1299518
user1299518

Reputation:

Whats the point for form if you're sending with ajax? On the problem now, get the inputs by:

var fields = [];
$("#dialog-form form fieldset > input").each(function() {
   fields.push( $(this)[0].value );
});
...
$.ajax({
   url: 'process-form.php',
   data:fields
...

Upvotes: 0

nealio82
nealio82

Reputation: 2633

You're on the right lines with $.ajax, but you need to actually pass the data with the submission, which you haven't done so far. You're best off setting the 'type' as well.

$( "#dialog-form" ).dialog({
        autoOpen: false,
        height: 450,
        width: 350,
        modal: true,
        buttons: {
            "Sumbit": function() {
                //VALIDATES FORM INFO, IF CORRECT
                if (Valid ) {
                    $.ajax({
                       url: 'process-form.php',
                       type: "post",
                       data: {
                           name: $('[name=name]').val(),
                           email: $('[name=email]').val(),
                           phone: $('[name=phone]').val(),
                       },
                       success: function (response) { //response is value returned from php
                            $( "#dialog-success" ).dialog({
                                modal: true,
                                buttons: {
                                    Ok: function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            });
                       }
                    });
                    $( this ).dialog( "close" );
                }

These variables should now be available in your PHP script as $_POST['name'], $_POST['email'] and $_POST['phone']

Upvotes: 0

Jamie Dixon
Jamie Dixon

Reputation: 54021

Given your current code the easiest way is to serialize the form into the data property:

[...]
url: 'process-form.php',
data: $('#dialog-form').serialize()

Upvotes: 1

Manse
Manse

Reputation: 38147

You can use the .serialize() function

$('yourform').serialize();

Docs for .serialize() here

You would use it like this :

$.ajax({
    url: 'process-form.php',
    data: $('form').serialize(),  // **** added this line ****
    success: function (response) { //response is value returned from php
        $("#dialog-success").dialog({
            modal: true,
            buttons: {
                Ok: function () {
                    $(this).dialog("close");
                }
            }
        });
    }
});

Upvotes: 3

jeffjenx
jeffjenx

Reputation: 17487

Yes, you can use the jQuery .post() method, which is detailed here

$.post( "process-form.php", $( "#dialog-form" ).serialize( ) );

Upvotes: 2

Related Questions