Page 1 of 1

post form memory

Posted: Fri Sep 16, 2005 4:11 pm
by cipherus
hi, im sure this has been discussed here before, but i tried searching and can't come up with the right words to find what to do.

my problem is that i have a form, and when i post it, php will check if everything is valid before sending out an email, but if things are not valid it will give you the proper error message and reload the form page. all this works fine, but how would i put all the information they have already entered back into the new form page?

here is my code:

Code: Select all

//EMAIL FORM
function email_form()
{
	?>
		<form method="post" action="feedback.php">

			Subject:
			<SELECT name="topic">
				  <Option value="General feedback"> General feedback </option>
				  <Option value="Help with the website"> Help with the website </option>
				  <Option value="Help with an order"> Help with an order </option>
			</SELECT>

			<br><br>

			Message:<br>
			<TEXTAREA name="message" rows="6" cols="40"></textarea><br>
		<br>
			If you would like a reply please enter your email address here:<br>
			<input type="text" name="from" cols=15></input><br>
			
			<input type="submit" name="submit" value="submit"></input>
		</form>
	<?
}


//PAGE START

//if submit has been clicked
if ($submit)
{
	if(!$message)  //no message
	{
		echo "<b style='color:FF0000'> *Message cannot be blank</b><br><br>";

		email_form();
	}
	else 
	{
		if(!$from) //allowing blank from field for annonymous users
		{
			send_email($message, $from, $topic);
		}
		else
		{

			if(!valid_email($from))  //invalid email (user typo maybe)
			{
				echo "<b style='color:FF0000'> *Invalid email address</b><br><br>";

				email_form();
			}
			else
			{
				//All checks have been made, you pass
				send_email($message, $from, $topic);
			}
		}
	}
}
else
{
//first time page view
	email_form();
}
?>
any help would be greatly appreciated!

Posted: Fri Sep 16, 2005 5:23 pm
by feyd
set up an argument list to pass into email_form() with default values set to empty strings. Send it all the variables when calling it (outside of the one that isn't or whatever)

I'd advise rewriting your code to support register_globals being off however..

Posted: Fri Sep 16, 2005 6:28 pm
by cipherus
thanks for the suggestion, it worked great for the text boxes.

but for the pulldown menu the only way i know how to make a default option selected is to do such as this:

Code: Select all

<select name="topic">
<option selected nam="option1"> option1 </option>
<option  nam="option2"> option2 </option>
</select>
so how would i pick a default option dynamically?

Posted: Fri Sep 16, 2005 6:29 pm
by feyd
you compare the value you get to the value of each option, emitting the selected mark on whichever matches :)

Posted: Fri Sep 16, 2005 6:50 pm
by cipherus
wow, that was a lot more complicated than i thought it would be, but it works! thanks so much!

just my thoughts..

Posted: Fri Sep 16, 2005 9:09 pm
by Charles256
I actually didn't think it was too complex. Here's the code I used to do that. :-D

Code: Select all

<select name="day">
			<?php
		if ($day!='')
		{
			$i=1;
			while ($i<13)
			{
				echo ("<option value='$i'");
				if ($day==$i)
				{
					echo (" selected");
				}
				echo (">$i");
				$i++;
			}
		}
		else
		{
				$i=1;
					while ($i<13)
					{
						echo ("<option value='$i'>$i");
						$i++;
					}
		}
		?>
		</select>
Hope that helps.

Posted: Fri Sep 16, 2005 11:10 pm
by cipherus
that was a great idea, i didn't even think of using a loop. i gave it a try and here's what i got, works beautifully:

Code: Select all

$option[0] = "I need help placing an order";
	$option[1] = "I have a question about an existing order";
	$option[2] = "I cannot find the items I need";
	$option[3] = "I am having technical difficulty with the website";
	$option[4] = "Other / General feedback";

	?>
		<form method="post" action="contact_us.php">

			Subject:
			<SELECT name="topic">
			<?
				
				for($i=0; $i < count($option); $i++)
				{
					if($option[$i] == $topic)
						echo "<Option value=\"".$option[$i]."\" selected> ".$option[$i]." </option>";
					else
						echo "<Option value=\"".$option[$i]."\"> ".$option[$i]." </option>";
				}

			?>
			</SELECT>

and i know i should not be using global variables, but they just seem so harmless for this script. and besides i never properly learned the other way even though it's probably just as easy.

Posted: Fri Sep 16, 2005 11:52 pm
by Charles256
it's actually pretty easy to not use global variables. On the page that your form submits to, (even if it's the same page), and if your using the method get then do the following..

Code: Select all

$MyConvientVariableName=$_GET['FieldNameInMyForm'];
and post...

Code: Select all

$MyConvientVariableName=$_POST['FieldNameInMyForm'];
Charmingly simple, isn't it? :-D