0

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?

Alexander
  • 105,104
  • 32
  • 201
  • 196
pretty me
  • 509
  • 2
  • 5
  • 11

4 Answers4

2

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);

                 }
            });
Ninju
  • 2,522
  • 2
  • 15
  • 21
2

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')}
}
Asit
  • 458
  • 4
  • 14
0

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?

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243
0

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.

Pablo Jomer
  • 9,870
  • 11
  • 54
  • 102