3

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?

2
  • 2
    possible duplicate of jQuery POST form data and so many others... :-) Commented May 22, 2012 at 12:50
  • Got it going, thanks guys. ^Had a look through other questions first obviously. If i don't understand the code much, then i can't really tell if that's what i need or not. Commented May 22, 2012 at 13:10

5 Answers 5

3

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");
                }
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

1

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

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

Comments

0

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']

1 Comment

You can pass the whole form using .serialize()
0

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
...

Comments

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.