Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Error Handling

Posted on 2014-02-07
8
Medium Priority
?
335 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 1800 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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
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

 
LVL 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

730 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