Solved

PHP Error Handling

Posted on 2014-02-07
8
319 Views
Last Modified: 2014-02-11
I am having an issue with handling php errors within my program.

I have a form and i use the follow to call an update :

if ($("#updateClientform").valid())
{
				$.post($("#updateClientform").attr("action"),$("#updateClientform").serialize(),function(){
							
	if(('<?php echo $actionType?>') =="Add"){
		var alertText = "The Client has been succesfully Inserted !"
	}
	else if(('<?php echo $actionType?>') =="Edit"){
		var alertText = "The Client has been succesfully Updated !"
	}
	showAlert("success",alertText,"fieldset");
	window.scrollTo(0,0); //Scroll to top of screen after update
	});
}

Open in new window


the form is set up as following :

<form action='UpdateClient.php' id='updateClientform' method="post" class="form-horizontal" role="form">

Open in new window



This works well and without issue. It shows a bootstrap alert to say that it has happened.

The problem i have is that when there is an issue i don't know how to report it back to the calling page. I am guessing this is something very simple !!

This is the a summary of the PHP i am using :

<?php

	try
	{
		SQL and Binding....
		$result->execute();
	}
	
	catch (PDOException $e)
	{
		$output = 'Unable to update Client';
		include 'error.php';
		exit();	
	}	
	
?>

Open in new window



If there is an error i can only see by looking at a preview of the page. What i'd like is for the calling page to know that there was an error and then display the appropriate message

Thanks in advance
0
Comment
Question by:maddisoncr
  • 4
  • 2
  • 2
8 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 450 total points
Comment Utility
One way to do this is to pick up the error and return a json string with a status code and message something like this
<?php
...
$result = array();

if (query_successful)
{
    $result['status'] = 1;
}
else {
    $result['status'] = 0;
    $result['msg'] = 'Your message here';
}
die(json_encode($result));
...
?>

Open in new window


Then in your JQuery code in the $.post call back

if ($("#updateClientform").valid())
{
  $.post($("#updateClientform").attr("action"),$("#updateClientform").serialize(),function(resp){ // ADD RESPONSE PARAMETER TO PICK UP RETURNED STATUS
    if(('<?php echo $actionType?>') =="Add"){
      var alertText = "The Client has been succesfully Inserted !"
    }
    else if(('<?php echo $actionType?>') =="Edit"){
      var alertText = "The Client has been succesfully Updated !"
    }
    showAlert("success",alertText,"fieldset");
    window.scrollTo(0,0); //Scroll to top of screen after update
    // CODE TO DISPLAY ERROR
    if (resp.status == 0) {
       // Display error here
       // Modify depending on how you want it displayed
       $('#error-field').html(resp.msg);
    }
  },'json'); // ADD THIS SO JQUERY PARSES THE RETURNED JSON STRING
}

Open in new window


You can expand depending on your requirements.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
Comment Utility
Here is the simple example showing how to get a result back from an AJAX call and display that result on a web page.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

This is a function that can help produce an appropriate output message and consequence.
http://php.net/manual/en/function.trigger-error.php
0
 

Author Comment

by:maddisoncr
Comment Utility
Thank you very much. I will have a look at all of these approaches over the weekend
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Good luck with it, and please post back if you have any questions, ~Ray
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:maddisoncr
Comment Utility
Thanks again Ray, all up and running :-)
0
 

Author Comment

by:maddisoncr
Comment Utility
Julian, thank you.. nearly forgot ;-)
0
 

Author Closing Comment

by:maddisoncr
Comment Utility
Perfect
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome - thanks for the points
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now