0

I am confused as to why an alert is not firing a json response. The response is coming into firebug. This was working ok when I was using php4.4.7 but then upgraded to php5.3.5 and now producing this error. Or probably my error. Can someone check my code and see where I am going wrong? If you need any more code, please let me know. many thanks

http://jsfiddle.net/QQtVv/

code here as per request:

function test(com,grid)
{
    if (com=='Delete')
        {
           if($('.trSelected',grid).length>0){
           if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
            var items = $('.trSelected',grid);
            var itemlist ='';
            for(i=0;i<items.length;i++){
                itemlist+= items[i].id.substr(3)+",";
            }
            $.ajax({
               type: "POST",
               dataType: "json",
               url: "fileinrptdelete.php",
               data: "items="+itemlist,
               success: function(data){
                   alert("You have successfully deleted:"+"\n\n"+"Customer: "+data.customer+"\n"+"name: "+data.ref+"\n"+"boxref: "+data.boxref);
               $("#flex1").flexReload();
               }
             });
            }
            } else {
                alert('You have to select a row to delete.'); 
            } 
               }


    }

// this is from the file fileinrptdelete.php

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{\n";
$json .= "name: '".$ref."',\n";
$json .= "company: '".$customer."',\n";
$json .= "boxref: '".$boxref."',\n";
$json .= "total: $total\n";
$json .= "}\n";
echo $json;
1
  • 3
    Live links are a great adjunct to a question, but always post the relevant code in the question as well. Two reasons. 1. People shouldn't have to follow a link to help you. 2. StackOverflow is meant to be a resource not just for you now, but for others having a similar issue in the future. External links can get moved, modified, deleted, etc. By making sure the relevant code is in the question, we ensure that the question (and its answers) remain useful for a reasonable period of time. Jul 13, 2011 at 10:11

2 Answers 2

3

The JSON is invalid (and served with the wrong content-type, it should be application/json).

Don't hand craft it. Use a library.

You are also trying to read data using the variable names you use in PHP and not using the names you apply to the keys in the JSON.

3
  • I am now getting an alert but the contents are undefined. Is the echo $json; the correct call in php5?
    – bollo
    Jul 13, 2011 at 10:23
  • I thought I was reading the json keys: $json .= "\"name\": \"".$ref."\",\n"; and in the success: alert("You have successfully deleted:"+"\n\n"+"Customer: "+data.company+"\n"+"name: "+data.ref+"\n"+"boxref: "+data.boxref); If this is not correct, could you please paste a sample for viewing. Thanks
    – bollo
    Jul 13, 2011 at 10:59
  • Ok I now see what I did. I was calling ref instead of name.
    – bollo
    Jul 13, 2011 at 11:01
1

a valid JSON attributes and values must be enclosed in double quotes "name" : "value" other than that it looks okay to me.

2
  • They are enclosed in double quotes? $json .= "File: '".$custref."',\n";
    – bollo
    Jul 13, 2011 at 10:25
  • no those are php quotes, not json quotes. you need it like $json .= "\"File\": \"".$custref."\",\n";
    – TheBrain
    Jul 13, 2011 at 10:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.