0

I'm trying to use Ajax to send some form data, but on the PHP page it's not echoing. I'm new to Ajax so not sure if I have done something wrong with it.

Here's what I have:

$(function () {

    $('form').on('submit', function (e) {

      e.preventDefault();

      $.ajax({
        type: 'post',
        url: 'two.php',
        data: $('form').serialize(),
        success: function () {
          alert('form was submitted');
        }
      });

    });

  });

One of the form fields has a name="selection" and id="selection" but in two.php all I'm trying to do is simply:

echo $_POST['selection'];

But nothing is set.
Any ideas?

1
  • to see if you have result put this console.log(data); after your alert, in two.php try $data = $_POST; $arrdata = array(); for each($data as $row) { $arrdata[] = $row; } var_dump($arrdata); Commented Jun 13, 2015 at 13:59

1 Answer 1

1

You should be passing your response (from two.php) to your success callback:

    success: function ( response ) {
      alert( 'Submitted data: ' + response );
    }

Which should work provided selection is actually set. (check in your console under network requests to confirm this)

Also, consider adding an error callback:

  error: function( response, errorThrown ){
      alert( 'request failed: ' + errorThrown );
  }

to report back any ajax errors.

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

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.