1

myfile.php

header('Content-type: application/json');

echo json_encode( array( 'error' => 'jkfshdkfj hskjdfh skld hf.' ) );

the above code works.

but when I alter it it stops working:

if( isset( $_GET['customerID'] ) ){

       // do something else error

} else {

   header('Content-type: application/json');
   echo json_encode( array( 'error' => 'jkfshdkfj hskjdfh skld hf.' ) );

}

both outputs are correct:

{"error":"jkfshdkfj hskjdfh skld hf."}

but I get an ajax error:

myfile.phtml

        <?php 
            if( isset( $_GET['custID'] ) )
               echo "var custID = " . htmlentities( $_GET['custID'] ) . ";";                             
            else
               echo "var custID = null;";                             
        ?>

        $.ajax({

            url: 'php/viewCustomer.php',
            type: 'GET',
            data: {customerID: custID},
            dataType: 'json',
            cache: false,
            beforeSend: function(){

                $('#display').append('<div id="loader"> Lodaing ... </div>');

            },
            complete: function(){

                $('#loader').remove();

            },
            success: function( data ){

                if( data.error ) {

                    var errorMessage = "";

                    $.each( data, function( errorIndex, errorValue ){ 

                        errorMessage += errorValue + "\n";

                    });

                    alert( errorMessage );

                } else {

                    //$('div#customer-content table tr td').eq(0).text( '1234' );
                    //$('div#customer-content table tr td').eq(1).text( '1234' );
                    //$('div#customer-content table tr td').eq(2).text( '1234' );
                    //$('div#customer-content table tr td').eq(3).text( '1234' );
                    //$('div#customer-content table tr td').eq(4).text( '1234' );
                    alert( data );

                }                       

            },
            error: function( jqXHR ){

                alert( 'AJAX ERROR' );

            }

        });

    });
16
  • What error do you get? What happens when customerID is set? Commented Mar 7, 2013 at 21:06
  • The error is 'AJAX ERROR' from the error: function( jqXHR ) { alert( 'AJAX ERROR' ); } Commented Mar 7, 2013 at 21:06
  • 1
    You are passing a customerID with the ajax request, so it's going into the if - what output are you expecting? Commented Mar 7, 2013 at 21:07
  • data: {customerID: custID} = where is the custID coming from? Commented Mar 7, 2013 at 21:08
  • Chad, if customerID is set I do nothing at the moment. empty if block. Commented Mar 7, 2013 at 21:08

1 Answer 1

1

From the comments it sounds like you are passing a null customerID and are not expecting it to go into the if condition. But even if the value is null, $_GET['customerID'] is still set. Change the check to empty() instead and it will work as you expect:

if( !empty( $_GET['customerID'] ) ){
    ....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.