Solved

PHP Error Handling

Posted on 2014-02-07
8
321 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 55

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 109

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 109

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 55

Expert Comment

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

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Email called spam 5 36
IP 10.0.1.2 / 255.0.0.0 61 56
Extracting store locations from Google maps or site 2 22
Echo Multiple values from multiple records 19 40
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

839 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