Jump to content

Help with form


mds1256

Recommended Posts

Hi

 

I am looking to create a form that will display an error message with the fields still populated.

 

I am after some advice on how I would achieve it.

 

If the form contains errors then I want to display an error message underneath the form and have the fields populated with the data they have already entered. If the form submission was successful then I want to hide the form and show just a confirmation message (this is the part I am having trouble with as I can achieve this but It means I would need to duplicate the form code and have it twice in my function and I dont want that).

 

 

My code I have chucked together (very roughly) to demonstrate my issue:

 

 

<?php

function form()
{
$error = "";
$nameVal = "";
$emailVal = "";

if(isset($_POST['submit']))
{
	$nameVal = $_POST['name'];
	$emailVal = $_POST['email'];

	if($_POST['name'] <> "John")
	{
		$error = "not the correct name";
	}
	else
	{
		// hide form and display confirmation message here
	}
}

echo "
	<form action=\"\" method=\"post\" name=\"form1\">
		<input id=\"name\" name=\"name\" type=\"text\" value=\"{$nameVal}\" /><br />
		<input id=\"email\" name=\"email\" type=\"text\" value=\"{$emailVal}\" /><br />
		<input name=\"submit\" id=\"submit\" type=\"submit\" />
	</form>
	<br />
	{$error}
";
}

?>

<html>
<head>
</head>

<body>

<?php form(); ?>

</body>
</html>

Link to comment
Share on other sites

You could have sent them to another page, but if you want to do it like that, you could set a boolean variable to be true along with $error and the other variables. Then if the script runs to this part: "// hide form and display confirmation message here", you set the boolean variable to false. When we finally come to the form, if boolean variable is true, show the form.

Link to comment
Share on other sites

You could have sent them to another page, but if you want to do it like that, you could set a boolean variable to be true along with $error and the other variables. Then if the script runs to this part: "// hide form and display confirmation message here", you set the boolean variable to false. When we finally come to the form, if boolean variable is true, show the form.

 

Thanks!

 

That seems to work :)

 

<?php

function form()
{
$error = "";
$nameVal = "";
$emailVal = "";
$bool = true;

if(isset($_POST['submit']))
{
	$nameVal = $_POST['name'];
	$emailVal = $_POST['email'];

	if($_POST['name'] <> "John")
	{
		$error = "not the correct name";
	}
	else
	{
		$bool = false;
		echo "Posted Sucessfully";
	}
}

if($bool == true)
{
	echo "
		<form action=\"\" method=\"post\" name=\"form1\">
			<input id=\"name\" name=\"name\" type=\"text\" value=\"{$nameVal}\" /><br />
			<input id=\"email\" name=\"email\" type=\"text\" value=\"{$emailVal}\" /><br />
			<input name=\"submit\" id=\"submit\" type=\"submit\" />
		</form>
		<br />
		{$error}
	";
}
}

?>

<html>
<head>
</head>

<body>

<?php form(); ?>

</body>
</html>


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.