jquery - Passing back error data to .load() -
i'm calling php function delete file, using .load:
gallerystatus2$.load('deletepage.php', {pagename : $(e.target).val()}, function(responsetxt,statustxt,xhr) { if(statustxt=="success") alert("external content loaded successfully!"); if(statustxt=="error") alert("error: "+xhr.status+": "+xhr.statustext); });
/* deletepage.php */
<?php $filename = $_post['pagename'] . 'xml'; // comes in without suffix $filepath = $_server['document_root'] . "/users/user_" . $_session['user']['id'] . "/xmls/" . $_post['filename']; if(!unlink ($filepath)) { echo ("<br />delete of $filepath failed"); } else { echo ("<br />deletepage.php: delete of $filepath succeeded"); } ?>
in case deletepage.php has serious errors. 1 of post values it's looking wasn't passed , actual unlink operation fails. on client, statustxt reports "success" , "external content loaded successfully!"
how can tell client php things did not go well.
thanks
in case, need read 'responsetxt' instead of 'statustxt' (without changing 'deletepage.php':
gallerystatus2$.load('deletepage.php', {pagename : $(e.target).val()}, function(responsetxt,statustxt,xhr) { if(statustxt=="success") { if(responsetxt.indexof('succeeded') >= 0) { alert("external content loaded successfully!"); } else { alert("error: "+xhr.status+": "+xhr.statustext); } } else if(statustxt=="error") alert("error: "+xhr.status+": "+xhr.statustext); } );
2nd option: .js
gallerystatus2$.load('deletepage.php', {pagename : $(e.target).val()}, function(responsetxt,statustxt,xhr) { if(statustxt=="success") { if(responsetxt) { var obj = jquery.parsejson(responsetxt); if(obj && obj.status == 'success') { //success whatever need alert(obj.msg); } else { //fail whatever need alert(obj.msg); } } } else if(statustxt=="error") alert("error: "+xhr.status+": "+xhr.statustext); } );
.php
<?php $filename = $_post['pagename'] . 'xml'; // comes in without suffix $filepath = $_server['document_root'] . "/users/user_" . $_session['user']['id'] . "/xmls/" . $_post['filename']; $response = array('status' => '', 'msg' => ''); if(!unlink ($filepath)) { $response['status'] = 'success'; $response['msg'] = '<br />delete of $filepath failed'; } else { $response['status'] = 'error'; $response['msg'] = '<br />deletepage.php: delete of $filepath succeeded'; } json_encode($response); ?>
Comments
Post a Comment