简体   繁体   中英

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. 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
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 :

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:

 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. 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>

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