0

I was wondering if I can return an error callback back to my jquery from my php page that I created, which will then display an alert based upon the actions that happen in my php page. I tried creating a header with a 404 error but that didn't seem to work.

Sample JQuery Code:

$(document).ready(function()
    {
        var messageid= '12233122abdbc';
        var url = 'https://mail.google.com/mail/u/0/#all/' + messageid;
        var encodedurl = encodeURIComponent(url);
        var emailSubject = 'Testing123';
        var fromName = 'email@emailtest.com';

        var dataValues = "subject=" + emailSubject + "&url=" + encodedurl + "&from=" + fromName + "&messageID=" + messageid;
        $('#myForm').submit(function(){
            $.ajax({
                type: 'GET',
                data: dataValues,
                url: 'http://somepage.php',
                success: function(){
                     alert('It Was Sent')
                }
                error: function() {
                    alert('ERROR - MessageID Duplicate')
                }
            });
            return false;
        });
    });

Sample PHP Code aka somepage.php:

<?php
include_once('test1.php');
include_once('test2.php');


if(isset($_GET['subject']))
{
   $subject=$_GET['subject'];
}
else
{
   $subject="";
}

if(isset($_GET['url']))
{
   $url=$_GET['url'];
}
else
{
   $url="";
}
if(isset($_GET['from']))
{
    $from=$_GET['from'];
}
else
{
    $from="";
}

if(isset($_GET['messageID']))
{
    $messageID = $_GET['messageID'];
}
else
{
    $messageID="";
}



    $stoqbq = new test2($from, $messageID);
    $msgID = new stoqbq->getMessageID();
    if($msgID = $messageID)
    {
        header("HTTP/1.0 404 Not Found");
        exit;
    }
    else
    {
        $userID = $stoqbq->getUser();
        $stoqb = new test1($subject,$url,$messageID,$userID);
        $stoqb->sendtoquickbase();
    }

     ?>

-EDIT-

If you get the invalid label message when using json this is what I did to fix this problem:

Server Side PHP Code Part-

if($msgID == $messageID)
{
    $response["success"] = "Error: Message Already In Quickbase";
    echo $_GET['callback'].'('.json_encode($response).')';
}
else
{
    $userID = $stoqbq->getUser();
    $stoqb = new SendToQuickbase($subject,$url,$messageID,$userID);
    $stoqb->sendtoquickbase();
    $response["success"] = "Success: Sent To Quickbase";
    echo $_GET['callback'].'('.json_encode($response).')';
}

Client Side JQuery Part-

$('#myForm').submit(function(){
            $.ajax({
                type: 'GET',
                data: dataValues,
                cache: false,
                contentType: "application/json",
                dataType: "json",
                url: "http://somepage.php?&callback=?",
                success: function(response){
                    alert(response.success);
                }
            });
            return false;
        });
Novazero
  • 553
  • 6
  • 24

2 Answers2

2

You can a return a JSON response from your PHP with a success boolean.

if($msgID = $messageID)
{
    echo json_encode(array('success' => false));
}
else
{
    $userID = $stoqbq->getUser();
    $stoqb = new test1($subject,$url,$messageID,$userID);
    $stoqb->sendtoquickbase();
    echo json_encode(array('success' => true));
}

and in your Javascript:

 $.ajax({
            type: 'GET',
            dataType: 'json',
            data: dataValues,
            url: 'http://somepage.php',
            success: function(response){
                 if(response.success) {
                   alert('Success');
                 }
                 else {
                   alert('Failure');
                 }
            }
        });
Jordan Brown
  • 13,603
  • 6
  • 30
  • 29
  • hmm this looks like an easier idea will take a look at this also :) – Novazero Jun 16 '11 at 18:35
  • Hmm its not working even though it makes sense on the solution you came up with any idea why the alerts don't pop up for me? – Novazero Jun 16 '11 at 20:42
  • Use Firebug or a Google Code Inspector to look at what the server is returning on your AJAX request. Every time an AJAX request is fired off, you should be able to inspect the response. In Firebug, it will pop up in your console. IF you're using Chrome, it will show up under the "Network" tab of the code inspector. – Jordan Brown Jun 16 '11 at 21:45
  • Also, try adding this to your PHP code: header('Content-type: text/json'); – Jordan Brown Jun 16 '11 at 21:49
  • I get a response is null this is shown in firefox firebug if(response.success) – Novazero Jun 16 '11 at 22:46
  • I figured out the problem and fixed the issue thanks for your solution it worked :) – Novazero Jun 17 '11 at 17:49
1

There is an accepted q/a with the same thing: How to get the jQuery $.ajax error response text?

Basically you need to grab the response message from the error callback function:

$('#myForm').submit(function(){
    $.ajax({
        type: 'GET',
        data: dataValues,
        url: 'http://somepage.php',
        success: function(){
             alert('It Was Sent')
        }
        error: function(xhr, status, error) {
      alert(xhr.responseText);
    }
    });
    return false;
});
Community
  • 1
  • 1
Romeo M.
  • 3,218
  • 8
  • 35
  • 40