Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RE: Form Question. L@@K Please

Status
Not open for further replies.

about2flip

Technical User
Nov 21, 2003
31
US
Newbie PHP programmer on board here. I want practicing on forms and this one is giving me a headache. I want this form to let the user know when they did not fill in a field. When all fields are done I want it to go to a verify page (verify.php), & show the user what they put into the form.

I need to know will the error code post to the same page, and why I keep getting a Parsing Error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING (line 31)

Any help will be highly appreciated. Thanks AGAIN For all your help Here.


<?php
print'
<html>
<head>
</head>
<body>
$userform= <form method="post" action="verify.php">
<strong>First Name:</strong><input type="text" name="first_name" value="' . $_POST['first_name'] . '" size="30"><br>
<strong>Last Name:</strong><input type="text" name="last_name" value="' . $_POST['last_name'] . '" size="30"><br>
<strong>Email:</strong><input type="text" name="email" value="' . $_POST['email'] . '" size="30"><br>
<input type="hidden" name="op" value="ds">
<input type="submit" name="submit" value="Send Information">
</form>';

if($_POST['op'] !='ds')
{
//they need to see the form
echo $userform;
}
else
{
//check value of $_POST[first_name]
if($_POST['first_name']==")
{
$firstname_err='<font color='red'>Please enter your name!</font>';
$send='no';
}

//check value of $_POST[last_name]

if($_POST['last_name']==")
{
$lastname_err='<font color='red'>Please enter your last name!</font>';
$send='no';
}

//check value of $_POST['email']
if($_POST['email']==")
{
$email_err='<font color='red'>Please enter your email address!</font>';
$send='no';
}

if($send !='no')
{
}
else
{
echo $name_err;
echo $lastname_err;
echo $email_err;
}
print'
</html></body>

?>
 
Probably
Code:
$firstname_err='<font color='red'>Please enter your name!</font>';
above code is causing the error where you have nested single quotes.
Change the above to
Code:
$firstname_err="<font color='red'>Please enter your name!</font>";


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Hi: I guess I'm not getting form validation. I want this form to post errors to itself and then move on to a verify page to show the user what they typed in. Afer the verify page I am going to create the script to write to a DB. However I just need help with the coding here. Somehow the last line is wrong ?> because in the Zend Editor it is giving me a red line. Hate to be a pest on this board, but I am teaching myself PHP. THANKS AGAIN!!!

<?php
print '
<html>
<head>
</head>
<body>
$userform= "<form method="post" action="' . $_SERVER['PHP_SELF']. '">
<strong>First Name:</strong><input type="text" name="first_name" value="' . $_POST['first_name'] . '" size="30"><br>
<strong>Last Name:</strong><input type="text" name="last_name" value="' . $_POST['last_name'] . '" size="30"><br>
<strong>Email:</strong><input type="text" name="email" value="' . $_POST['email'] . '" size="30"><br>
<input type="hidden" name="op" value="ds">
<input type="submit" name="submit" value="Send Information">
</form>';

if($_POST['op'] !='ds')
{
//they need to see the form
echo $userform;
}
else
{

//check value of $_POST['first_name']
if($_POST['first_name']== '')
{
$firstname_err="<font color='red'>Please enter your name!</font>";
$send='no';
}

//check value of $_POST['last_name']

if($_POST['last_name']== '')
{
$lastname_err="<font color='red'>Please enter your last name!</font>";
$send='no';
}

//check value of $_POST['email']
if($_POST['email']== '')
{
$email_err="<font color='red'>Please enter your email address!</font>";
$send='no';
}

if($send !='no')
{
//send the form

if(isset($_POST['send_form'])) {

}
else
{
echo $name_err;
echo $lastname_err;
echo $email_err;
echo $userform;
}
}
print'
</html>';
?>
 
you have not closed the curly braces around the
Code:
 if($send !='no')
    {
    //send the form

block.

tip - if you are going to use the curly brace syntax ALWAYS open and close them at the same time, then go back and type your conditional (or whatever) statements in. that way you should never go wrong.
 
Seems to me like you forgot to close one curly brace. Better code imdentation will prevent mistakes like that.
 
Ok Thanks. But where do I put <form method ="post" action="verify.php">

or my question is how do I get to go the verify.php page?

Thanks
 
you don't.

with this method you validate and process the information on the same page that displays the form. if you *really* must go to another page you could start a session, assign the post variables to the session, go to another page, start a session again, retrieve the session variables and then process them.

i think you've not quite got your head around the whole form processing client-server thing yet. i posted a pretty comprehensive response to your earlier post thread434-1177230. you might have a look at that before posting back on the same point!
 
The thing with the redirection is you have to catch all the values from the form and resend them to wherever it is you are redirecting. They don't automatically get resent.

So if form.php sends information to itself to verify everything is filled in, you have to catch the info, and resend it, etiher appending it to the URL
[red]verify.php?value1=somevalue&value2=someothervalue....[/red]
Or use sessions as Jpadie suggests.

In any event you have to actually catch the values from the form and put them somewhere to be retrieved from the verify page.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok. Not quite following. Is there a link in this forum I can read on cathcing the form and putting it somewhere, or do you have sample code to show me based on the code I am using? Also where can I go or what book to buy and read on form validation. I am getting so many opinions I am little confused right now.

Thanks for your help.
 
Here'es a quick example:

Code:
<form action=myfrom.php method=post>
<input type=text name="email" value="">
<input type=submit name="pressed" >
</form>

when you click on the form, the info is submitted to itslef. myform.php.

From there you can access the values using
$_POST['pressed'] and $_POST['email'].


So you check that they are filled in correctly, and then you redirect to verify.php. Using something like
Code:
header("Location: verify.php"); /* Redirect browser */
When you get to verify.php $_POST['pressed'] and $_POST['email'], are no longer available because they dont get saved, or resent with the redirection.
So from myform.php you have to catch them as you would to veryfy them and send them along with the redirection.

Code:
$value=$_POST['email'];

and then do the redirect like:

header("Location: verify.php?email=" . $value); /* Redirect browser */

Alternatively you can use
Session variables to keep the values all through out the site.

Code:
$_SESSION['email']=$_POST['email'];

For more info on header redirection, and sessions read here:

Sessions : faq434-2036

Redirecting: faq434-1379


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i really really suggest that you take a look at pear.php.net and in particular the HTML_Quickform class.

there is a really simple to follow and well written tutorial at
with this combo you will get to grips with forms generally and validation in no time.

the basic thing you need to grasp is the page that displays the form can also do the processing. the display can be conditional on receiving certain inputs.

at a very basic level try this

Code:
<?
if (isset($_POST['submit'])): //we know the form was submitted
	if (validate_form()):
		display_success();
	else:
		$msg = "you must enter both your first and last name";
		display_form($msg);
	endif;
else:
	display_form();
endif;

function validate_form()
{
	if (empty($_POST['lastname']) || empty($_POST['firstname'])): //test whether EITHER field is empty
		return false;
	else:
		return true;
	endif;
}
function display_success()
{
?>
Well done for entering your name right <?=$_POST['firstname']?> <?=$_POST['lastname']?>.
<?
}
function display_form($msg=NULL)
{
$er = error_reporting();
error_reporting(0);
extract($_POST);
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" >
<fieldset>
<legend>User Details</legend>
<? if (!empty($msg)): echo "<div style=\"color=red\">$msg</div>"; endif; ?>
Enter your first name: <input name="firstname" type="text" value="<?=$firstname?>"/> <br/>
Enter your last name: <input name="lastname" type="text" value="<?=$lastname?>"/> <br/>
<input type="submit" name="submit" value="submit" />
</fieldset>
</form>
<?
error_reporting($er);
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top