0

Hello, I have my site set up so that everything is done with AJAX requests to a script depending on what action is taken. If my script returns an error - such as the user is not allowed to take the action they tried to take - it returns an error message.

What I would like is for AJAX to know when there is an error returned, and I know there is an error parameter for the ajax function, but as far as I know that is only for HTTP error codes - so how would I modify my PHP script to return an error that the ajax would know it is an error?

Thank you for any help given!
Key

2
Contributors
10
Replies
14
Views
6 Years
Discussion Span
Last Post by kroche
0

Your could control errors with php!

Example!

with '@' you can control functions errors!

<?php
if(@$con=mysql_connect('localhost','user','pass'))
{
  echo "Connect";
}else{
  echo "Error!";
}
?>

Returned if can connect to database "Connect" else "Error".
This code cannot return any other errors!!!

You want this?

Edited by morteza_ipo: n/a

0

I know that I can return errors with PHP - and I do that, but if I want my errors to show up in a different way than my other messages returned by my script, how could I do that with the ajax function? Ex: make errors red, but other messages not red.

0

I just thought about that for my example- it was a bad one. What if I wanted the error to pop up in a javascript window like Shadowbox? I would have to use the error function in ajax to do that - so I want to know how I would have my php script return an error to ajax so that it would know to use the error function. (I'm using the jQuery ajax function too - that probably would have been pertinent.)

Ex:

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   },
   error: function(msg){
      // Pop up in a shadowbox code
   }
 });

The ajax would have to know that it is an error - so what do I pass with php to let it know that?

0

I think this is good for your problem!

$("body").append("<div id='error'>Error</div>");

For creating a div element with id. And in style : #error { color:red; }

Use jQuery in some.php file with top command.

This is good?

Edited by morteza_ipo: n/a

0

No, your not understanding what I would like to achieve. jQuery's AJAX function has an error parameter that is executed when there is an error during the AJAX request. As far as I know it only catches HTTP error codes, so I wanted to know if there was a way for me to send something from my php script that alerts the AJAX code that an error occurred in the script.

0

try it

I think this is good for you!

this code is for : b.php

<html>
<head>
<style type="text/css">
#error
{
	color:red;
}
#success
{
	color:green;
}
</style>
<script language="javascript" src="jquery.min.js"></script>
<script language="javascript">
$.ajax({
   type: "POST",
   url: "a.php",
   data: "q=test",
   success: function(msg)
	{
    // document.getElementById('div').innerHTML = msg ;
	if(msg == "true")
	{     
		$(document).ready(function() { $("div").append("<div id='success'>Success!</div>"); });
	}else{
    		$(document).ready(function() { $("div").append("<div id='error'>Error</div>"); });
	}
	},
	   error: function(msg){
     	$(document).ready(function() { $("div").append("<div id='error'>Error</div>"); });
   }
 });
</script>
</head>
<body>
<div id="div"></div>
</body>
</html>

this code is for: a.php

<?php
if($_POST['q']=="test")
{
	echo "true";
}else{
	echo "false";
}
?>

This is good?

0

It would work, but it was not what I was hoping to do. What I do now is search the return value for the word "Error" in bold text, and then change the class of the div, but I was looking to see if the error parameter in jQuery's ajax function was a better way to do it.

0

The success: and error: parameter in jQuery is just for returning data!
means when ajax can connect to your file and can return any data , success part will be running and when jQuery cannot connect to your file or cannot returning data or any other errors , the error part will be running!

Edited by morteza_ipo: n/a

0

Okay, so there is no way to do what I was looking to do then. Thank you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.