Solved

PHP Error Handling

Posted on 2014-02-07
8
329 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 58

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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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 58

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 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