Jump to content

error handling


ngreenwood6

Recommended Posts

I am trying to get my errors to post to the same page. there is an email, firstname, and lastname field. ex. if the user doesnt enter their email on that same page put the error. my code looks like this:

 

if (!$get_email)
{
echo ("You are missing some information!");
}

 

How can I incorporate what I am trying to do into my code? Any help is appreciated as always.

Link to comment
Share on other sites

That works but it didnt solve my issue. I am trying to get the error to post to the same page where you are putting in the data. (ex. if you forget to put in your email address, it displays "you are missing some information" next to the form input on the same page) When i submit the form and forget an email address now it just opens a new page and says "you are missing some information" on a new blank page. I dont want a new blank page i want it displayed on the form page where you are trying to submit it.

Link to comment
Share on other sites

You mean you want the error messages displayed on the page your form is on? Example....

 

form.php

<?php session_start(); ?>
<form action="process.php" method="post">
 <input type="text" name="name"><?php echo (isset($_SESSION['name_error'])) ? "You must supply a name" : ""; ?><br />
 <input type="text" name="email"><?php echo (isset($_SESSION['email_error'])) ? "You must supply an email address" : ""; ?><br />
 <input type="submit" name="submit">
</form>

 

process.php

<?php

 $err = false;
 session_start();
 if (isset($_POST['submit'])) {
   if (empty($_POST['name'])) {
     $_SESSION['name_error'] = true;
     $err = true;
   }
   if (empty($_POST['email'])) {
     $_SESSION['email_error'] = true;
     $err = true;
   }
   if ($err) {
     header("location: form.php");
   } else {
     unset($_SESSION);
     // process form
   }
 }

?>

Link to comment
Share on other sites

Yeah, sure.  You need to attempt to handle the form on the same page that it is displayed.  For example:

 

<?php
if (isset($_POST['submit'])) {
    $last_name = $_POST['lname'];
    $first_name = $_POST['fname']; 
    if (empty($last_name) || empty($first_name)) {
            $errors[] = "You forgot some information!";
    }
}
?>
<h2>Just a test form!</h2>
<?php
if (is_array($errors)) {
   foreach ($errors as $error) {
       echo "- $error<br />";
   }
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
First Name: <input type="text" name="fname" value="<?php echo htmlentities($first_name, ENT_QUOTES); ?>" />
Last Name: <input type="text" name="lname" value="<?php echo htmlentities($last_name, ENT_QUOTES); ?>" />
<input type="submit" name="submit" value="Submit Form" />
</form>

Link to comment
Share on other sites

I tried your code and it worked fine. I tried to change my file from 2 pages to 1 but i kept getting different errors and stuff.  Could you help me try to combine it so that my errors come up correctly?

 

The first page is the page with the form on it:

<html>
<head>
<title>Sign up for newsletter!</title>
</head>

<!--beginning statement-->
If you would like to sign up for the newsletter please use this form!

<br><br>

<form name="input" method="post" action="news.php">
Enter your email address:
<input name="get_email" type="text" id="get_email">
<br>
Enter your first name:
<input name="get_firstname" type="text" id="get_firstname">
<br>
Enter your last name:
<input name="get_lastname" type="text" id="get_lastname">
<br>
<input type="submit" name="submit" value="Submit">

</form>

<br><br>

To view a list of registered emails <a href="list.php">click here</a>.

<br><br>

<body>
</body>
</html>

 

The second page is the page where it processes it:

 

<?php

//include our variables
include ("variables.php");
include ("db.php");

//connect to database
$mysqli_connect = mysqli_connect($host,$db_user,$db_pass,$db_name)
or die ("Could not connect to database");

if (empty($get_email) || empty($get_firstname) || empty($get_lastname))
{
include ("index.php");
echo "You are missing some information";
}
else
{
//check for valid format of email address
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $get_email))
{
echo "The email you have entered is not a valid email format.";
}
else
{
//check to see if data is in database
$query_string = "SELECT email FROM users WHERE email = '$get_email' LIMIT 1";
$query = mysqli_query($mysqli_connect,$query_string)or die(mysqli_error($mysqli_connect));
$num = mysqli_num_rows($query);

}
//display this error if data is already in database
if ($num != 0)
{
include ("index.php");
echo "That email is in use by another account.";
}
else
{

//variable to send the data if all data is correct
$result = mysqli_query($mysqli_connect,$putinto_db)
or die ("Error: ".mysqli_error($mysqli_connect));
?>
Congratulations! To view a list of registered emails <a href="list.php">click here</a>.
<?php
}
}
?>

 

If you need any additional info please let me know.

 

Link to comment
Share on other sites

yes, i did and like i said the form that he gave me worked when i tried using it how he coded it. however when i tried to embed it into my script it didn't work. i even tried to make my script one page but it wasn't working which is why i asked if he could help me create a new single page with this embedded into it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.