Jump to content

How to output errors on the same page?


tmharrison

Recommended Posts

Thank you, jesirose and mattd8752

 

jesirose, i didn't understand the sessions, part, so I went with mattd8752's reply.

 

i can get it to work mattd8752, thank you very much.

 

is there a way to embed it into a specific spot on my html page?

 

thank you so much

 

tina

Jesirose

 

Can you please explain your process to me.

 

I understand the saving as php page, I need to understand, what you mean by processing the form on the same page and then the sessions part.

 

As i can' t seem to get the errors to list inside the html page by using the include statement.

 

Thank you,

 

Tina

very simple example:

 

form.php

<?php
   session_start();
   if($_SESSION['errorMsg']) {
      echo $_SESSION['errorMsg'];
      unset ($_SESSION['errorMsg']);
   }
?>

<form method = 'post' action='process.php'>
   Enter something   <input type = 'text' name = 'something'>
   <input type = 'submit' value = 'send'>
</form>

 

process.php

<?php
   session_start();
   if (!isset($_POST['something']) || trim($_POST['something']) == "") {
      $_SESSION['errorMsg'] = "please fill out field";
   } elseif (strlen($_POST['something']) > 10)) {
      $_SESSION['errorMsg'] = "something is too long. Please make it 10 or less chars";
   } 

   if ($_SESSION['errorMsg']) {
      header('Location: form.php'); exit();
   } else {
      // no errors, do whatever. 
   }
?>

 

Still a little bit unclear to me, probably because of never using sessions before.

 

below is what and how I am dealing with errors on the "process side of things, which is my register.php"

if(isset($_POST['Send'])){
$errors = array(); // Initialize error array.

if(empty($_POST['name'])) {
	$errors[] = 'You did not enter your Name.';

 

below is the scaled down version of my registration.php form which is really just html

<form action="register.php" method="post" target="_self">
                <tr>
                  <td height="20" colspan="3" align="left" valign="top"><input name="name" type="text" class="graytext1" id="name" size="60">      
                  <td height="20" align="left" valign="top"><input name="email" type="text" class="graytext1" id="email">
                <tr>
                  <td height="15" colspan="2" align="left" valign="bottom" class="graytext">Address
                  <td height="15" colspan="2" align="left" valign="bottom" class="graytext">Add'l Address
                <tr>
                  <td height="20" colspan="2" align="left" valign="top"><input name="address" type="text" class="graytext1" id="address" size="40">

 

this is the bottom of my register.php for the errors

} else {
	echo '<id="errors">Error!<p class="error">The following error(s) occurred:<br />';
	foreach ($errors as $msg) {
	echo " - $msg<br />\n";

 

if you could possibly let me know where things should go, that would probably make it clearer to me.

 

Thank you,

 

Tina

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.