0

Hello, I have 2 php forms, the first that takes the input and sends it to the second form which checks to see if the fields are empty.

What I have to do is if both fields are not entered is return to the main page and highlight the field/s that is not fill with an asterix, If one of the fields was entered I need to return that value to the textbox on the first page and then link back to the first page to enter in the missing field.

here is the first page input.php

$asterixFirst = "";
$asterixLast = "";
// check for asterix value
if ($checkFirst == false)
{
	$asterixFirst = "* required";
}
else
{
	$asterixFirst = "";
}
if ($checkLast == false)
{
	$asterixLast = "* required";
}
else
{
	$asterixLast = "";
}


echo "<form action=\"inputcheck1.php\" method=\"post\">";
echo "<p>Enter first name: <input type=\"text\" name=\"first\" />", $asterixFirst, "</p>";
echo "<p>Enter last name: <input type=\"text\" name=\"last\"/>", $asterixLast, "</p>";
echo "<input type=\"submit\" value=\"Process\"/>";

echo "</form>";
?>
</body>
</html>

here is the 2nd page

<?php
if ((checkFirst() == false) && (checkLast() == true))
{
	echo "First name needed";
}
elseif ((checkFirst() == true) && (checkLast() == false))
{
	echo "Last name needed";	
}
elseif ((checkFirst() == false) && (checkLast() == false))
{
	echo "Both fields need filing";
}
else
{
	echo "First Name: ", $_POST['first'];
	echo "<p>Last Name: ", $_POST['last'], "</p>";
	echo "<a href=\"emptyinput1.php\">return to main page</a>";
}
?>

<?php
function checkFirst()
{	
	$_POST['first'] = trim($_POST['first']);
	if (isset($_POST['first']) && $_POST['first'] !== '')
	{	
		//echo "First Name: ", $_POST['first'];
		return true;
	}
	else
	{
		return false;
	}
}
	
function checkLast()
{
	$_POST['last'] = trim($_POST['last']);
	if (isset($_POST['last']) && $_POST['last'] !== '')
	{	
		//echo "<p>Last Name: ", $_POST['last'], "</p>";
		return true;
	}
	else
	{		
		return false;
	}
}
?>

I have the checks working ok, just not sure how to pass the value of a entered field gathered by form 2 back to that field on the first form and then how to send the asterix check either.

I assuming it needs to be done using the $_SESSION command somehow.

any help much appreciated

Edited by mr_scooby: n/a

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by mr_scooby
1

Insert this at the top of both pages:

if (! session_id())
  session_start();

Then you can use

$_SESSION['first'] = $_POST['first'];

in the second file. In the first file you can use:

if (isset($_SESSION['first']))
  // first name was set by second page, you can use it

Edited by pritaeas: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.