Connecting Tech Pros Worldwide Forums | Help | Site Map

Input form

TJ
Guest
 
Posts: n/a
#1: Jul 17 '05
Dear PHP group,

First question:

I have an input form that upon pressing a button validates the values
entered into this form. I am using the following code recall this form if
there is an error in one of the requested fields:

<form name="form1" method="post" action= "InputForm.php" >

All works fine. However, if there is an error and this form is recalled, all
the fields are blanked out when you are brought back to the form. I'd like
for the form to retain the values that had been entered. How do I do this?

Second question:

For the fields that are in error, I'd like for the titles for the fields to
display an asterisk and change the text to red. Have any of you done this
and if so how do you implement it?

Thanks,



Jochen Buennagel
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Input form


TJ wrote:[color=blue]
> I'd like
> for the form to retain the values that had been entered. How do I do this?[/color]

You need to put the values that where transmitted by the form into the
"value" of the inputs like this:

<input type="text" name="firstname" value="<?php echo
$_POST['firstname'];?>" />
[color=blue]
> For the fields that are in error, I'd like for the titles for the fields to
> display an asterisk and change the text to red. Have any of you done this
> and if so how do you implement it?[/color]

<?php if (valid($_POST['firstname'], 'name')): ?>
<font color="#ff0000">*&nbsp;
<?php else: ?>
<font>
<?php endif; ?>
Firstname:
</font>

Somewhere you define a function:
<?php
function valid($value, $type){
if(type=='name'){
// return true if this is a valid name, false otherwise
}
}
?>

Of course, this is VERY simplistic....

Jochen

Geoff Berrow
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Input form


I noticed that Message-ID: <RRoGb.10873$JD6.5055@lakeread04> from TJ
contained the following:
[color=blue]
>Dear PHP group,
>
>First question:
>
>I have an input form that upon pressing a button validates the values
>entered into this form. I am using the following code recall this form if
>there is an error in one of the requested fields:
>
><form name="form1" method="post" action= "InputForm.php" >
>
>All works fine. However, if there is an error and this form is recalled, all
>the fields are blanked out when you are brought back to the form. I'd like
>for the form to retain the values that had been entered. How do I do this?
>
>Second question:
>
>For the fields that are in error, I'd like for the titles for the fields to
>display an asterisk and change the text to red. Have any of you done this
>and if so how do you implement it?
>[/color]
Check this out - it simply checks if the field has been entered or not.
If the form is resubmitted it repopulates the textfields with the $_POST
variables.

<html>
<head>
<title>Check fields</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
..red { color: #FF0000}
-->
</style>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<?php
function error($field){
if (!$field && $_POST["Submit"]){
print"<p class=\"red\">* ";
}
else{
print"<p>";
}
}
?>
<form name="form1" method="post" action="">
<?php
error($_POST["textfield"]);
?>
First name <input type="text" name="textfield" value="<?php print
$_POST["textfield"];?>">
</p>
<?php
error($_POST["textfield2"]);
?>
Last name <input type="text" name="textfield2"value="<?php print
$_POST["textfield2"];?>"></p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
TJ
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Input form


Thanks -- I'll review your solution.

"Jochen Buennagel" <zang.NOSPAM@buennagel.com> wrote in message
news:bsd6b6$5qd$01$1@news.t-online.com...[color=blue]
> TJ wrote:[color=green]
> > I'd like
> > for the form to retain the values that had been entered. How do I do[/color][/color]
this?[color=blue]
>
> You need to put the values that where transmitted by the form into the
> "value" of the inputs like this:
>
> <input type="text" name="firstname" value="<?php echo
> $_POST['firstname'];?>" />
>[color=green]
> > For the fields that are in error, I'd like for the titles for the fields[/color][/color]
to[color=blue][color=green]
> > display an asterisk and change the text to red. Have any of you done[/color][/color]
this[color=blue][color=green]
> > and if so how do you implement it?[/color]
>
> <?php if (valid($_POST['firstname'], 'name')): ?>
> <font color="#ff0000">*&nbsp;
> <?php else: ?>
> <font>
> <?php endif; ?>
> Firstname:
> </font>
>
> Somewhere you define a function:
> <?php
> function valid($value, $type){
> if(type=='name'){
> // return true if this is a valid name, false otherwise
> }
> }
> ?>
>
> Of course, this is VERY simplistic....
>
> Jochen
>[/color]


TJ
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Input form


Thanks, I'll review this.

"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:gv8kuvoa3pullv0oa5lrlr1tqvi8i6r9qi@4ax.com...[color=blue]
> I noticed that Message-ID: <RRoGb.10873$JD6.5055@lakeread04> from TJ
> contained the following:
>[color=green]
> >Dear PHP group,
> >
> >First question:
> >
> >I have an input form that upon pressing a button validates the values
> >entered into this form. I am using the following code recall this form if
> >there is an error in one of the requested fields:
> >
> ><form name="form1" method="post" action= "InputForm.php" >
> >
> >All works fine. However, if there is an error and this form is recalled,[/color][/color]
all[color=blue][color=green]
> >the fields are blanked out when you are brought back to the form. I'd[/color][/color]
like[color=blue][color=green]
> >for the form to retain the values that had been entered. How do I do[/color][/color]
this?[color=blue][color=green]
> >
> >Second question:
> >
> >For the fields that are in error, I'd like for the titles for the fields[/color][/color]
to[color=blue][color=green]
> >display an asterisk and change the text to red. Have any of you done this
> >and if so how do you implement it?
> >[/color]
> Check this out - it simply checks if the field has been entered or not.
> If the form is resubmitted it repopulates the textfields with the $_POST
> variables.
>
> <html>
> <head>
> <title>Check fields</title>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> <style type="text/css">
> <!--
> .red { color: #FF0000}
> -->
> </style>
> </head>
>
> <body bgcolor="#FFFFFF" text="#000000">
> <?php
> function error($field){
> if (!$field && $_POST["Submit"]){
> print"<p class=\"red\">* ";
> }
> else{
> print"<p>";
> }
> }
> ?>
> <form name="form1" method="post" action="">
> <?php
> error($_POST["textfield"]);
> ?>
> First name <input type="text" name="textfield" value="<?php print
> $_POST["textfield"];?>">
> </p>
> <?php
> error($_POST["textfield2"]);
> ?>
> Last name <input type="text" name="textfield2"value="<?php print
> $_POST["textfield2"];?>"></p>
> <p>
> <input type="submit" name="Submit" value="Submit">
> </p>
> </form>
> </body>
> </html>
>
> --
> Geoff Berrow (put thecat out to email)
> It's only Usenet, no one dies.
> My opinions, not the committee's, mine.
> Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


Closed Thread