0

My form:

          <form class="form-inline signup" action="php/signupForm.php" role="form" id="signupForm">
        <div class="form-group">
          <input type="email" name="email" class="form-control" placeholder="Email address">
        </div>
        <div class="form-group">
          <button type="submit" class="btn btn-theme ladda-button" data-style="expand-left">
<span class="ladda-label" id="notice">Get notified!</span>
</button>
        </div>
      </form>

the end of my php script

$response = array(
    "status" => $status,
    "message" => $message
);

echo json_encode($response);

My page is receiving data like:

{"status":0,"message":"This email is already on list!"}

using JS I need to parse that data and then update text within an element.

 <span id="notice">Get notified!</span>

here's my script which doesn't work, after senging form data to my php script I get a white screen that shows the json strong

    $(document).ready(function() {
      $.ajax({
      dataType: 'json',
        $('#notice').text(data.message);
      });
    });
2
  • 1
    If you are getting a screen of the response then you are doing a regular form submit instead of preventing the form from being submitted (ie using preventDefault in onsubmit event) and then doing the ajax call. With the code listed you are executing your ajax call as soon as your main page loads, not when you submit a form Commented Oct 13, 2015 at 20:10
  • I think your right and I think I've done this before. So to fix do I execute ajax onclick submit? Commented Oct 13, 2015 at 20:15

3 Answers 3

1

You have to handle the response in a callback.

$(document).ready(function() {
    $('form').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
          data: $(this).serialize(),
          url: $(this).attr('action'), // Or the path of the PHP file
          dataType: 'json',
        }).done(function(response) {
          $('#notice').text(response.message);
        });
    });
});

See the related docs here

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

9 Comments

What url would go there, it's coming back to the page from the server?
The url of the PHP script you're calling
In the network tab of your browser debugger, so you see your call being fired ? My guess is that you're not referring to the right php file in the url parameter
see my updated anwser, that details how to handle the call itself.
For the GET vs POST, that will affect how you will use the data in your php script, i suggest that you use a POST for this, and you will be able to manipulate the posted data in your PHP script in the $_POST array
|
1

That ajax call is not well formed, missing success callback and url e.g:

$(document).ready(function () {
    $.ajax({
        url: '/the/url/where/your/data/comes/from/',
        dataType: 'json',
        success: function (data) {
            $('#notice').text(data.message);
        }
    });
});

Comments

1

Your code as is, is just executing at page load and not on submission of a form. You need to attach an onsubmit event, prevent the default action of doing the form submit and do your ajax call in there. Also your ajax call itself was malformed

$("#yourFormID").submit(function(e){
    e.preventDefault();
    $.ajax({
       url:"/urlToServerScript",
       data:{} //any form data the script needs you should be put here,
       dataType:"json" //type of response the server will output
    }).then(function(data){
       $('#notice').text(data.message);
    });
});

7 Comments

but the data is already coming back to the page from the php script. I just need something to handle that returning data. I'm probably misunderstanding you here
@Rhillz, "after senging form data to my php script I get a white screen that shows the json strong", This indicates that the data isn't "coming back" through an AJAX request though, you are doing a regular html form submit that causes the browser to navigate away from the current page to the server response. If you are wanting to stay on the current page and still submit data to a server script this is how you would do it
but it is my sql is updating
i see what your saying
you can do data:$(this).serialize() I think
|

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.