views:

55

answers:

3

I am using php/ajax to submit a form without page refresh. Here are my files-

coupon.js

jQuery(document).ready(function(){
        jQuery(".appnitro").submit( function(e) {
$.ajax({
            url     : "sms.php",
            type    : "post",
            dataType: "json",
            data    : $(this).serialize(),
            success : function( data ) {
                        for(var id in data) {
                            jQuery('#' + id).html( data[id] );
                        }
                      }

        });
//return false or
e.preventDefault();

    });

});

sms.php

    <?php
    //process form
$res = "Message successfully delivered";
    $arr = array( 'mess' => $res );
    echo json_encode( $arr );//end sms processing
    unset ($_POST);
    ?>

and here is code for my html page -

<form id="smsform" class="appnitro" action="sms.php" method="post">
...
</form>
<div id="mess" style="background:green;"></div>

Now instead of submitting form through ajax without page refreshing what is happening is that page gets redirected to

baseurl/sms.php and the only thing visible on page is

{"mess":"Message successfully delivered"}

My guess is that php script is not returning back successfully to the jquery and hence the echo in last part of sms.php is getting displayed. How should i make the php script return successfully? ANY IDEAS HOW TO DEBUG THIS.I HAVE TRIED USING return false at the end of coupon.js but no results.

When i click on submit firebug gives following results -

POST http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php

404 Not Found 1.29s   `jquery.min.js (line 130)`

Response

Firebug needs to POST to the server to get this information for url:
http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php

This second POST can interfere with some sites. If you want to send the POST again, open a new tab in Firefox, use URL 'about:config', set boolean value 'extensions.firebug.allowDoublePost' to true
This value is reset every time you restart Firefox This problem will disappear when https://bugzilla.mozilla.org/show_bug.cgi?id=430155 is shipped
+1  A: 

If you're being redirected to sms.php instead of doing an ajax call, it probably means that there's something wrong with your jQuery code, probably the event binding itself.

I'm not sure without testing it, but shouldn't that code be:

$(document).ready(function(){
        $(".appnitro").submit( function(e) {
$.ajax({
...

?

Check the javascript console, either in Firefox/Firebug or Chrome-IE/Developer Tools. Does it show any errors in those lines?

Fanis
@Fanis : please see my edited question for firebug results
ayush
@ayush, are you getting a 404 on loading jquery.min.js? If yes then jquery will not be loaded. Look at the Console tab in Firebug. It will show you any javascript errors, including if you're trying to call a jQuery function without jQuery actually being loaded.
Fanis
i think its showing 404 not found on sms.php. When i click on **`jquery.min.js (line 130)`** it shows me line 130 of the jquery script.
ayush
And the Firebug *Console* tab still shows no error?
Fanis
+1  A: 

@Fanis It's irrelevant whether he uses '$' or 'jQuery', they are synonyms.

@Ayush As Fanis says, you should try Firebug if you don't use it already. I've checked the example at my server, works OK, and I don't know what's the problem at Your side.

You can use onsubmit="return false" to disable form submission:

<form id="..." class="..." ... onsubmit="return false">

Also check if javascript is enabled, for example do "alert('something')" at $(document).ready

Edit:

// instead of  
url: "sms.php"
// try
url: "/~kunal17/devbuzzr/wp-content/themes/street/sms.php"
// although I don't really know if it will help
Michał Kluczka
i added alerts to both places after document ready and after form submission and they work fine.But magically now when i click on submit nothing is happening. no page redirection and no message display.
ayush
Check in Firebug if there was Ajax request performed (if there was, it will be visible in Firebug Console tab)
Michał Kluczka
Thanks for the clarification, Michal. Javascript, even jQuery, isn't really my strong suit, and having only seen the shorthand version used I assumed it was the only one.
Fanis
please check the edited question for firebug results
ayush
+1  A: 

Fanis and Michal Kluczka are probably right about the issue with event binding , I tried your code myself as well, and it works for me.

Put an alert('X') as the first statements in your jQuery(document).ready() and jQuery(".appnitro").submit() functions and see if both are displayed (first one upon document load, second one upon form submission).

One more thing: I suggest you include a

header('Content-Type: application/json');

into your sms.php file before printing your JSON data to protect against cross-site-scripting (XSS) attacks. See also Don’t serve JSON as text/html for details.

Archimedix
please check the edited ques for firebug results.
ayush
Have you included the `alert()` calls ? Do any messages appear when you load the page or when you submit the form ? This would definitely help you track the source of the error. As I've said before, your code **does** work for me. The Firebug `Not Found` error you reported seems quite strange to me as opening the URL directly apparently works, as it does when you submit the form (you are still seeing the JSON data upon submission, aren't you ?).
Archimedix