繁体   English   中英

Ajax提交返回错误,但可以更新数据库

[英]Ajax submit returning Error but updating the database fine

<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
}
?>

尝试在您的if添加else语句:

insert($ pname,$ danme,$ Case_Histroy,$ medication,$ Note,$ id); ?> }?>

同样,也没有必要将php放在<div>的中间,因为您不会在其中引入任何变量,所以您只能在开始时使用echo

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

最后我得到了问题的答案! 实际的问题是触发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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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