请帮我处理jQuery.post.我的脚本没有给出错误,也没有发布表单字段

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

本文关键字:布表单 字段 表单 错误 处理 jQuery post 脚本 我的 出错      更新时间:2023-09-26

我不明白为什么这个脚本不起作用。它与AJAX POST脚本/函数中的某些内容有关。现在,当我在表单上点击submit时,它应该将表单值传递到ajax_profile.php页面。它不是发送/过帐表单字段数据。请帮我解决这个问题。这是我的Javascript代码:

<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;
            });
});

以下是我的ajax_profile.php的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);    

?>

您没有正确序列化表单数据。您没有引用字符串值。您不是对表单数据进行URI编码。收集表单数据的方式可能会导致JSON对象损坏。这应该已经在JavaScript控制台中出错了。如果你注意到浏览器中的JavaScript控制台,你应该已经注意到了这个错误(在Firebug/Chrome/IE9中按F12)。

假设$('#Buddie_profile')引用HTML <form id="Buddie_profile">,那么您应该将其数据序列化如下:

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

与具体问题无关,存在一些严重的SQL注入漏洞。如果最终用户在其中一个输入字段中输入了一个有效的部分SQL查询字符串,例如(部分)DROPTRUNCATE查询,那么它也会被执行!使用参数化查询或至少使用mysql_real_escape_string()