pretty me
pretty me

Reputation: 559

How to return an error status on an ajax call in PHP?

I have this script code and it works perfectly,

CODE

<script>
    function ajob(){
        var a3=a.value
        var a4=b.value
        var a5=c.value
        if(a3!='' && a4!=''){
            $.ajax({
                type:"get",
                url:"addj.php?content=" + a3 + "," + a4 + "," + a5
            });
         lod();
        }else{alert('Fill all fields')}
    }
</script>

<form>
    <table>
        <tr><td>Job Name:</td>
            <td><input id="a" name="jname" type="text"></td>
        </tr>
        <tr><td>Job Description:</td>
            <td><input id="b" name="jd" style="margin: 2px 0 2px 0;" type="text"></td>
        </tr>
        <tr><td>Status:</td>
            <td>
                <select id="c" name="jstat" style="width:100%; height: 26px" >
                    <option>Active</option>
                    <option>Not Active</option>
                </select>
            </td>
        </tr>
    </table>
</form>

what i want to know is how can can i know if there is an error in my addj.php file? Here is my addj.php

addj.php

require 'con.php';
$pieces = explode(",", $_GET['content']);
$jname=$pieces[0];
$jd=$pieces[1];
$jstat=$pieces[2];
$query=mysqli_query($con,"INSERT INTO job(job_name,job_desc,status) values('$jname','$jd','$jstat') ");

How do I return a failed status from my PHP code and how can I handle that error in my java script code?

Upvotes: 0

Views: 133

Answers (4)

Pablo Jomer
Pablo Jomer

Reputation: 10378

You can use this code to catch any ajax errors.

$.ajax({
  type:"get",
  url:"addj.php?content=" + a3 + "," + a4 + "," + a5
}).fail(function(error){
  alert(error);
});

Hope it helps.

Upvotes: 0

Asit
Asit

Reputation: 468

Use success or error callback function and output data to console.

function ajob(){
    var a3=a.value
    var a4=b.value
    var a5=c.value
    if(a3!='' && a4!=''){
        $.ajax({
            type:"get",
            url:"addj.php?content=" + a3 + "," + a4 + "," + a5,
            success: function(dataReturn, textStatus, jqXHR) {
                console.log(dataReturn);
            },
            error: function(jqXHR,textStatus,errorThrown){
                console.log(textStatus,errorThrown);
            }
        });
     lod();
    }else{alert('Fill all fields')}
}

Upvotes: 2

Ninju
Ninju

Reputation: 2530

Use this

            $.ajax({
                type:"get",
                url:"addj.php?content=" + a3 + "," + a4 + "," + a5,
                success: function(data) { alert("succsess") },
                error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status + " : " + xhr.responseText);

                 }
            });

Upvotes: 2

Techie
Techie

Reputation: 45124

You can use the console of the browser. Try following link that has step by step.

Why the Ajax script is not running on IIS 7.5 Win 2008 R2 server?

Upvotes: 0

Related Questions