Solved

PHP Error Handling

Posted on 2014-02-07
8
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 450 total points
ID: 39841920
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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 39841975
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
ID: 39842722
Thank you very much. I will have a look at all of these approaches over the weekend
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39842767
Good luck with it, and please post back if you have any questions, ~Ray
0
 

Author Comment

by:maddisoncr
ID: 39850159
Thanks again Ray, all up and running :-)
0
 

Author Comment

by:maddisoncr
ID: 39850163
Julian, thank you.. nearly forgot ;-)
0
 

Author Closing Comment

by:maddisoncr
ID: 39850166
Perfect
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39850336
You are welcome - thanks for the points
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I split a variable 19 46
How does this modal gets closed? 6 42
Ajax success not firing alert 6 45
Phone number mask 4 16
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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 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