简体   繁体   中英

Issues with Jquery/PHP ajax form submission

I am using the following code to post form data to a php file..

jQuery("#form-submit").click(function(data){ 

                var url = "save-data.php";

                jQuery.ajax({
                    type: "POST",
                    data: jQuery('#my-form').serialize(),
                    url: url,
                    cache : "false",
                    success: function(data){
                        jQuery("#dl-message").html(data);
                        jQuery("#dl-message").css('background-color', '#0C3');
                        jQuery('#dl-message').slideToggle('slow', function() {
                            jQuery('#dl-message').delay(2000).slideToggle('slow', function() {
                                //Animation Complete
                            });
                        });

                    },
                    error: function ( jqXHR, textStatus, errorThrown){
                        jQuery("#dl-message").html('There was an error saving your form: ' + errorThrown);
                        jQuery("#dl-message").css('background-color', '#F33');
                        jQuery('#dl-message').slideToggle('slow', function() {
                            jQuery('#dl-message').delay(2000).slideToggle('slow', function() {
                                //Animation Complete
                            });
                        });
                    }
                });

                return false;
            });

and then in my php file I am simply looking for $_POST['field-name'] to see if the contents of the form were posted. The ajax call returns successful, however no data from the form seems to be posted to the PHP file. When I call...

$name = $_POST['name'];
echo "Your name is: " . $name;

I get nothing.... Does anyone see anything wrong with my ajax call at all?

Thanks so much for your time...

So it seems the post was empty at the first place (btw you can include your last three posts inside the question by editing it). Are you sure your form's id is 'my-form'?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM