Ajax提交返回错误但更新数据库正常

时间:2017-07-12 05:35:07

标签: javascript php jquery html ajax

<script>
        function addprescription() {
            var Case_Histroy=$('#Case_Histroy').val();
            var Medication=$('#Medication').val();
            var Note=$('#Note').val();
            var pname="<?php echo($patient->getUsername()); ?>";
            var dname="<?php echo($doctor->getUsername()); ?>";
            var id="<?php echo($id); ?>";
            frmData={Case_Histroy:Case_Histroy,Medication:Medication,Note:Note,pname:pname,dname:dname,id:id}
            console.log( frmData);
            $.ajax({
                    type: "POST",
                    url: "loadfiles/AddAppointmentSubmit.php",
                    data: frmData,
                    success: function (msg) {
                        alert(msg);
                        $("#alert").html(msg)
                    }
                    ,
                error : function () {
                alert("failure");
            }
        });
        }
</script>

我有提交表单的功能。但是ajax功能会将其警告为失败。但数据库似乎已更新。当我点击按钮。我无法在控制台中找到原因。

这是php文件

<?php
echo "I'm in";
include "../../Adaptor/mysql_crud.php";
include ("Prescription.php");
$prescription=new Prescription();
if(isset($_POST)){
    $Note=htmlspecialchars($_POST['Note']);
    $Case_Histroy=htmlspecialchars($_POST['Case_Histroy']);
    $medication = htmlspecialchars($_POST['Medication']);
    $pname=$_POST['pname'];
    $danme=$_POST['dname'];
    $id=$_POST['id'];
    $prescription->insert($pname,$danme,$Case_Histroy,$medication,$Note,$id);
    ?>
    <div class="alert alert-success" id="alert"><strong><?php echo "Submitted succesfully"; ?></strong></div>
<?php
}
?>

2 个答案:

答案 0 :(得分:0)

尝试向else添加if语句:

插入($ PNAME,$ danme,$ Case_Histroy,$用药,$注意,$ ID);     ?&GT;     的 } ?&GT;

此外,没有必要将php放在<div>的中间,你可以在开头使用echo,因为你没有向它引入任何变量:

echo '<div class="alert alert-success" id="alert"><strong>Submitted successfully</strong></div>';

答案 1 :(得分:0)

最后我得到了问题的答案!实际问题是启动了AJAX请求的按钮也重新加载了中断AJAX内部工作的页面。因此,将提醒错误消息。

我尝试了这段代码。

<script>
    $(function() {
        $("#button_Add_p").click(function(e){
            e.preventDefault();
            var Case_Histroy=$('#Case_Histroy').val();
            var Medication=$('#Medication').val();
            var Note=$('#Note').val();
            var pname="<?php echo($patient->getUsername()); ?>";
            var dname="<?php echo($doctor->getUsername()); ?>";
            var id="<?php echo($id); ?>";
            frmData={Case_Histroy:Case_Histroy,Medication:Medication,Note:Note,pname:pname,dname:dname,id:id}
            console.log( frmData);
            $.ajax({
                type: "POST",
                dataType: 'html',
                url: "loadfiles/AddAppointmentSubmit.php",
                data: frmData,
                success: function (msg) {
                    alert(msg);
                    $("#alert").html(msg)
                }
                ,
                error : function () {
                    alert("failure");
                }
            });
        });
    });
</script>