简体   繁体   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>

I have a function to submit the form. 我有提交表格的功能。 But ajax function alerts its as failure. 但是ajax函数会警告其失败。 But data base seems to be updated. 但是数据库似乎正在更新。 when I click the button. 当我单击按钮时。 I couldn't find the reason for the cause in the console. 我在控制台中找不到原因的原因。

this is the php file 这是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
}
?>

Try adding an else statement to your if : 尝试在您的if添加else语句:

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

Also, it's not necessary to stick the php in the middle of the <div> you can just use echo at the beginning since you're not introducing any variables to it: 同样,也没有必要将php放在<div>的中间,因为您不会在其中引入任何变量,所以您只能在开始时使用echo

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

Finally I got the answer for the Problem! 最后我得到了问题的答案! Actual problem is button that fired up the AJAX request also reloaded the page interrupting AJAX inner workings. 实际的问题是触发AJAX请求的按钮也重新加载了页面,从而中断了AJAX内部工作。 So the error message will be alerted. 因此错误消息将被警告。

I tried this code. 我尝试了这段代码。

<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