简体   繁体   中英

Please help me with jQuery.post. My script not giving an error nor posting form fields

I cant not figure out why this script doesn't work. It has to do with something within the AJAX POST script/function. Right now, when I hit submit on my form, it should pass the form values to ajax_profile.php page. It is not sending / posting form fields data. Please help me figure out the problem. Here is my Javascript code:

<script type="text/javascript">

$(document).ready(function(){

    $('Buddie_profile').submit(function () {

            $.post('ajax_profile.php', $(this).serialize(), function(data) {

                            if(data.success){ 

                                    $("body").showMessage({

                                        'thisMessage':  [data.message],

                                        'autoClose':    true,

                                        'className':    'tooltip',

                                        'delayTime':    8000,

                                        'displayNavigation':    false

                                    });

                                }else{

                                    $("body").showMessage({

                                        'thisMessage':  [data.message],

                                        'autoClose':    true,

                                        'className':    'tooltip',

                                        'delayTime':    8000,

                                        'displayNavigation':    false

                                    });

                                }

                            },'json');

                return false;
            });
});

here is my php file code for ajax_profile.php:

require('include/conn.php');


                                    $success= mysql_query("UPDATE birthbuddie SET 

                    Fname='".$_POST['FirstName']."',

                    Lname='".$_POST['SecondName']."',

                    Surname='".$_POST['Surname']."',

                    Dayofbirth='".$_POST['DayofBirth']."',

                    Monthofbirth ='".$_POST['MonthofBirth']."'

                    Yearofbirth='".$_POST['YearofBirth']."',

                    Ss='".$_POST['ss']."',

                    Mm='".$_POST['mm']."',

                    Hh='".$_POST['hh']."',

                    Countryofbirth='".$_POST['CountryofBirth']."',

                    Cityofbirth='".$_POST['CityofBirth']."',

                    Personalmsg='".$_POST['PersonalMessage']."',

                    WHERE BBId='".$_SESSION['userid']."'");


    if($success){

            $data['success']=true;
            $data['message']='You have updated changes successfully';

        }else{

            $data['success']=false;
            $data['message']='Update was not successful. Try again';
        }

        echo json_encode($data);    

?>

You're not properly serializing the form data. You're not quoting the string values. You're not URI-encoding the form data. The way as you collected the form data may end up in a corrupt JSON object. This should have error'ed in the JavaScript console. You should have noticed this error if you paid attention to the JavaScript console in your browser (press F12 in Firebug/Chrome/IE9).

Assuming that $('#Buddie_profile') refers a HTML <form id="Buddie_profile"> , then you should be serializing its data as follows:

$.post('ajax_profile.php', $(this).serialize(), function(data) {
    // ...
});

Unrelated to the concrete problem, you've there some serious SQL injection hole . If the enduser enters a valid partial SQL query string such as a (partial) DROP or TRUNCATE query in one of the input fields, then it'll get executed as well! Use parameterized queries or at least mysql_real_escape_string() .

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