2 Replies - 569 Views - Last Post: 29 March 2013 - 08:02 AM Rate Topic: -----

#1 danbywinby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-January 13

PHP form error checking?

Posted 29 March 2013 - 07:16 AM

Hi

I have a PHP application form on my website and i have tried implementing error checking into it. This is a snippet of what i have so far in the PHP file which sends the data from the form to a set email address:

This bit is from where it collects the variables from the form:

if(isset($_POST['forum-name']) && $_POST['forum-name']!== ""&& $_POST['forum-name']!== "Registered Forum Name")
{
	$ForumName = $_POST['forum-name'];
}
else
{
	$error = "ERROR: You have not entered a forum name in your application.Please try again";
}


I have managed to make the error appear in the URL of the page which it sends the person to once the form is sent:

header('Location: ../apply.php?='.$error.'');


I also have this code on the page which it sends the person to once the form is sent:

<?
if(isset($_GET["error"]))
{
	echo "<script>alert('".$_GET["error"]."')</script>";
}
?>


I thought this was all working perfectly before but i have realized that now whenever somebody submits an alert always pops up because "error" is always in the URL. How can i fix this so that the alert box only appears when there is an error with a form that somebody has submitted, also i want a different message to popup depending on the error.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: PHP form error checking?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: PHP form error checking?

Posted 29 March 2013 - 07:45 AM

// initialize $error
$error = "";
// set it to some text if there is an error

if ($error == "") {
    header('Location: ../apply.php');
} else {
    header('Location: ../apply.php?error=' . $error);
}

but it is a now a poor practice to use alerts and you should (probably, I haven't checked) encode the $error (because of the spaces, etc.), probably with rawurlencode().

This post has been edited by andrewsw: 29 March 2013 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: PHP form error checking?

Posted 29 March 2013 - 08:02 AM

Rather than passing an error-sentence in the url, pass a simple code word or number, then check this code word/number in the related page; this is simpler, neater, and avoids any concerns about encoding the sentence.

You need to be aware, though, that someone could easily modify the url, so it should not be relied upon to confirm whether the form-submission succeeded.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1