Jump to content

[SOLVED] How to change where the "echo" appears


weetabix

Recommended Posts

Ok, I have some code and I'm using echo to display an error message...

Here's en example

 

		if(is_numeric($_POST['staffid'])){
		}else{
			echo 'Please enter a numeric staff id';
			show_form();
			exit;
		}

 

now I want the echo to appear in a cell within a table. Is that possible? If yes how can it be done?

Thanks again, awesome site  ;D

Link to comment
Share on other sites

It's hard to help you with your vague explanation.

 

I'm assuming the table you want to display the error message in is in the show_form() function? If so, you can make the message a parameter of the function.

 

Post your code to the show_form() function and explain more exactly what you want.

Link to comment
Share on other sites

show_form() just displays the form again with a validation error message, i'm not really aware of any better ways to do it. I thought about setting a flag and something like an array to list the errors (if multiple) but I don't know how to implement it.

 

The show_form() code is like this. I didn't copy the whole thing cause its just a repeatance of cells. What i want it to do, is add another box on the bottom stating something like "Error message: " and then list the error that happened when the user tried to send invalid data. Checks are applied later on.

 

function show_form(){
?>
<form action="addStaff.php" method="post">

<table width="50%" height="54%" cellspacing="1">
<tr>
	<td height="59" width="476" colspan="4">
	<p align="center">Add a new staff member</td>
</tr>
<tr>
	<td height="44" width="76">Staff ID:</td>
	<td height="44" width="128"><input type = "Text" name="staffid"></td>
	<td height="44" width="119">First Name</td>
	<td height="44" width="135"><input type = "Text" name="firstname"></td>
</tr>
<tr>

Link to comment
Share on other sites

<?php
function show_form()
{
	if(is_numeric($_POST['staffid'])){
// do something
exit;
		}else{
			echo 'Please enter a numeric staff id';
			show_form();
			exit;
		}
}
?>

<?php
show_form();
?>
<form action="addStaff.php" method="post">

<table width="50%" height="54%" cellspacing="1">
<tr>
	<td height="59" width="476" colspan="4">
	<p align="center">Add a new staff member</td>
</tr>
<tr>
	<td height="44" width="76">Staff ID:</td>
	<td height="44" width="128"><input type = "Text" name="staffid"></td>
	<td height="44" width="119">First Name</td>
	<td height="44" width="135"><input type = "Text" name="firstname"></td>
</tr>
<tr>

 

Place the show_form() function where ever you want it to display at.

Link to comment
Share on other sites

Okay, you can do something like this with your function

 

<?php

function show_form($errors = FALSE){
?>
<form action="addStaff.php" method="post">

<table width="50%" height="54%" cellspacing="1">
<tr>
	<td height="59" width="476" colspan="4">
	<p align="center">Add a new staff member</td>
</tr>

   <?php
   
   if (isset($errors)){
      echo '<tr><td><b>ERRORS</b><p>';
      
      foreach ($errors as $error){
         echo '-'.$error.'<br>';
      }
      
      echo '</td></tr>';
   }

?>

<tr>
	<td height="44" width="76">Staff ID:</td>
	<td height="44" width="128"><input type = "Text" name="staffid"></td>
	<td height="44" width="119">First Name</td>
	<td height="44" width="135"><input type = "Text" name="firstname"></td>
</tr>
<tr>

?>

 

You can now pass an array of errors to your function, and it will display them if there are any.

 

Here is an example of how you would call the function in your case

 

<?php

	if(is_numeric($_POST['staffid'])){
		}else{
			$error[] = "Please enter a numeric staff id";
                                $error[] = "You can do as many errors as you want";
			show_form($error);
			exit;
		}
?>

 

 

Link to comment
Share on other sites

What I mean is that I'm now creating an array of errors which will be listed once the form function is called again and the form reloads.

 

Once this is done it's not actually displaying all the errors apart from one. Shall I call the function once all the checks are applied on to the data an my array is not empty ?

 

My(your) code is just like you posted it, but I just had this idea i told you about...

Link to comment
Share on other sites

So the errors display fine the first time, but when you recall the function to reload the page it only displays one error when there are more? Could you post the code to where you recall the function the second time?

 

I'm having trouble figuring out what your trying to say...so I may be way off.

Link to comment
Share on other sites

Yup, here it is

 

<?php

function show_form($errors = FALSE){
?>
<form action="addStaff.php" method="post">

<table width="50%" height="54%" cellspacing="1">
<tr>
	<td height="59" width="476" colspan="4">
	<p align="center">Add a new staff member</td>
</tr>
<tr>
	<td height="44" width="76">Staff ID:</td>
	<td height="44" width="128"><input type = "Text" name="staffid"></td>
	<td height="44" width="119">First Name</td>
	<td height="44" width="135"><input type = "Text" name="firstname"></td>
</tr>
<tr>
	<td height="41" width="76">Username:</td>
	<td height="41" width="128"><input type = "Text" name="username"></td>
	<td height="41" width="119">Last Name:</td>
	<td height="41" width="135"><input type = "Text" name="surname"></td>
</tr>
<tr>
	<td height="43" width="76">Password:</td>
	<td height="43" width="128"><input type = "Text" name="password"></td>
	<td height="43" width="119">Address:</td>
	<td height="43" width="135"><input type = "Text" name="address"></td>
</tr>
<tr>
	<td height="40" width="76">Telephone:</td>
	<td height="40" width="128"><input type = "Text" name="telephone"></td>
	<td height="40" width="119">Postcode:</td>
	<td height="40" width="135"><input type = "Text" name="postcode"></td>
</tr>
<tr>
	<td height="42" width="76">Email:</td>
	<td height="42" width="128"><input type = "Text" name="email"></td>
	<td height="42" width="119">Hours Per week:</td>
	<td height="42" width="135"><input type = "Text" name="workhrs"></td>
</tr>
<tr>
	<td height="36" width="76">Position ID:</td>
	<td height="36" width="128"><input type = "Number" name = "positionid"></td>
	<td height="36" width="119">Role:</td>
	<td height="36" width="135"><input type = "Text" name="role"></td>
</tr>
   <?php
   
   if (isset($errors)){
      echo '<td><b>ERRORS OCCURRED: </b><p>';
      
      foreach ($errors as $error){
         echo '-'.$error.'<br>';
      }
      
      echo '</td>';
   }

?>
</table>

<input type= "Submit" name="submit" value= "Submit">	



</form>
<?PHP
}

 

And the code for the somewhat validation I made

		if(is_numeric($_POST['staffid'])){
		}else{
			$error[] = 'Please enter a numeric staff id';
			show_form($error);
			exit;
		}
	//Username must not be left empty
	if(!empty($_POST['username'])){
		}else{
			$error[] =  'You need to enter a username';
			show_form($error);
			exit;
		}
	//Password must not be left empty
	if(!empty($_POST['password'])){
		}else{
			$error[] =  'You need to enter a username';
			show_form($error);
			exit;
		}
	//Telephone number must not be empty and be numeric
	if(is_numeric($_POST['telephone']) || !empty($_POST['telephone'])){
		}else{
			$error[] =  'Please enter a numeric telephone number';
			show_form($error);
			exit;
		}
	//Position ID must be entered
	if(is_numeric($_POST['positionid'])){
		}else{
			$error[] =  'Please enter a numeric position id';
			show_form($error);
			exit;
		}

	//First name shall be completed
	if(!empty($_POST['firstname'])){
		}else{
			$error[] =  'Please enter your first name';
			show_form($error);
			exit;
		}

	//Last name shall be completed
	if(!empty($_POST['surname'])){
		}else{
			$error[] =  'Please enter your last name';
			show_form($error);
			exit;
		}

	//Address as well
	if(!empty($_POST['address'])){
		}else{
			$error[] =  'Please enter your address';
			show_form($error);
			exit;
		}

	//And postcode
	if(!empty($_POST['postcode'])){
		}else{
			$error[] =  'Please enter your postcode';
			show_form($error);
			exit;
		}

	//Working hours must be numeric and not null
	if(is_numeric($_POST['workhrs']) || !empty($_POST['workhrs'])){
		}else{
			$error[] =  'Please enter numeric working hours';
			show_form($error);
			exit;
		}

	//E-Mail must be of a proper format
	if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])){ 
		}else{
			$error[] =  'Please enter a valid email address';	
			show_form($error);
			exit;
		}

Link to comment
Share on other sites

You had A LOT of unneeded else if statements. You were also calling the function every single time you added an error...which won't work. Change your code to this

 

<?php

if (!is_numeric($_POST['staffid'])) 
    $error[] = 'Please enter a numeric staff id';

//Username must not be left empty
if (empty($_POST['username'])) 
    $error[] =  'You need to enter a username';

//Password must not be left empty
if (empty($_POST['password']))
    $error[] =  'You need to enter a username';

//Telephone number must not be empty and be numeric
if (!is_numeric($_POST['telephone']) || empty($_POST['telephone'])) 
    $error[] =  'Please enter a numeric telephone number';

//Position ID must be entered
if (!is_numeric($_POST['positionid']))
    $error[] =  'Please enter a numeric position id';

//First name shall be completed
if (empty($_POST['firstname']))
    $error[] =  'Please enter your first name';

//Last name shall be completed
if (empty($_POST['surname']))
    $error[] =  'Please enter your last name';

//Address as well
if (empty($_POST['address']))
    $error[] =  'Please enter your address';

//And postcode
if (empty($_POST['postcode']))
    $error[] =  'Please enter your postcode';

//Working hours must be numeric and not null
if (!is_numeric($_POST['workhrs']) || empty($_POST['workhrs']))
    $error[] =  'Please enter numeric working hours';


//E-Mail must be of a proper format
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email']))
    $error[] =  'Please enter a valid email address';
    
if (!empty($error)){
   show_form($error);
   exit;
}

?>

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.