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
https://forums.phpfreaks.com/topic/267011-help-with-form/
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
https://forums.phpfreaks.com/topic/267011-help-with-form/#findComment-1368937
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
https://forums.phpfreaks.com/topic/267011-help-with-form/#findComment-1368940
Share on other sites

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.