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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep form info from disappearing? 1

Status
Not open for further replies.

stealth71

Programmer
Feb 22, 2005
14
0
0
US
I have a form on a page at the bottom I have a checkbox to agree to some rules before the info is submitted to the db. At the top in the <? I Check if submitted and then check the status of the checkbox. If the checkbox has been checked then it proceeds if not a javascript alert appears telling the user that they must agree to continue and the page refreshes for some reason, when this happens all the fields the user had entered are erased. What can I do to avoid this? I need to keep the info in the fields and alert or how can I put the error message on the page dynamically with php? I will have to verify the info in the fields and I have seen people use red text when a field is blank or improperly filled, how can I accomplish this?
 
Can u post a relevant code ?

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
the page doesn't "refresh" as such. by pressing submit you are directing the browser to a new page. if you do server side validation then this occurs on the new page. assuming you have a self processing page then you are most likely failing the "if" clause at the beginning and then display the blank form.

you can fix this one of two ways:

1. if there are post variables you could set the value attribute of each control in the html. do this by adding the following code in the failed part of the if clause :

Code:
else
{
  extract($_POST);
}
and then add to each control an attribute of
Code:
  value = <?=$fieldname?>; //where $fieldname is "$" followed by the name of the field

the above works to handle text fields but you will need to adapt the method for other control types.

Method 2:
use client side validation (javascript) on the first page to ensure that the terms are agreed prior to submitting the form. trigger the validation on the button click.

hth
Justin
 
<?php
if(isset($_POST['submit'])){
if(isset($_POST['agree2']) && $_POST['agree2']=='yes'){
header("Location: sucess.php");
exit;
}else{
print "<script language='JavaScript'>\n";
print "alert('You must agree to submit.')\n";
print "</script>\n";
}
}
?>

<body>
<html>
<form name="approve" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="2">NAME:</td>
<td colspan="2"><input type="text" name="name" size="75"></td>
</tr>
<tr>
<td colspan="2">Specific Product requested:</td>
<td colspan="2"><input type="text" name="product" size="50"></td>
</tr>
<tr>
<td colspan="4"><br><input type="checkbox" name="agree2" value="yes">&nbsp;I HERBY AGREE TO CONDITIONS IDENTIFIED ON THE PREVIOUS PAGE.
</td>
</tr>


<tr>
<td align="center"><br><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

Here is a small working portion of my form so you can see the problem. Copy and paste it and try it. Is there a way I can show the error on the same page without using the alert?
 
roughly my suggestion under method 1 might be as follows:

Code:
<?
if ((isset($_POST['agree'] || $_POST['agree'] != "yes") && isset($_POST))
{
  extract ($_POST);
 }
 ?>
<form name="approve" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                      <table cellpadding="0" cellspacing="0" border="0">
                                            <tr>
                                                <td colspan="2">NAME:</td>
                                                <td colspan="2"><input type="text" name="name" size="75" value="<?=$name?>"></td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">Specific Product requested:</td>
                                                <td colspan="2"><input type="text" name="product" size="50" value="<?=$product?>"> </td>
                                            </tr>
<tr>
                                                <td colspan="4"><br><input type="checkbox" name="agree2" value="yes" checked="">&nbsp;I HERBY AGREE TO CONDITIONS IDENTIFIED ON THE PREVIOUS PAGE.
                                                </td>
                                            </tr>
                                                
                                            
                                            <tr>
                                                <td align="center"><br><input type="submit" name="submit" value="Submit"></td>
                                            </tr>
                                        </table>
 
OK thanks for the help jpadie I found the solution. I did what you told me but it filled the field with </br> so I set $name = ""; at the top now it works great. Thank You again for your prompt response this has been bugging me for an hour.

Here is the altered code if anyone needs it in the future.
<?php
$name = "";
$product = "";
if(isset($_POST['submit'])){
if(isset($_POST['agree2']) && $_POST['agree2']=='yes'){
header("Location: sucess.php");
exit;
}else{
print "<script language='JavaScript'>\n";
print "alert('You must agree to submit.')\n";
print "</script>\n";
extract($_POST);
}
}
?>

<body>
<html>
<form name="approve" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="2">NAME:</td>
<td colspan="2"><input type="text" name="name" size="75" value="<?=$name?>"></td>
</tr>
<tr>
<td colspan="2">Specific Product requested:</td>
<td colspan="2"><input type="text" name="product" size="50" value="<?=$product?>"></td>
</tr>
<tr>
<td colspan="4"><br><input type="checkbox" name="agree2" value="yes">&nbsp;I HERBY AGREE TO CONDITIONS IDENTIFIED ON THE PREVIOUS PAGE.
</td>
</tr>


<tr>
<td align="center"><br><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
 
You all are great. What can I do about a textarea that I have is there anyway to do it with that?
 
exactly the same
Code:
<textarea name="textareaname"><?=$textareaname?></textarea>
 
Thanks for your help. I got it to work with a state dropdown too.
<select name="state">
<option value="<?=$state?>"><?=$state?></option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
.....

You are awesome.
 
hmm, not sure that the option box works that way. i think you're just adding an option to the top of the box.

the way that i ordinarily do it is as follows (it's even easier if the values come from php rather than being hardcoded):

Code:
<select name = "">
  <option value="something" <? if ($selectionvalue === "something") {echo "selected = \"selected\""} ?>>something</option>
  <options other>
</select>
 
I don't quite understand what you are doing? Do you mind explaining a little more?
 
on the select box, the html spec allows the coder to preselect a particular option. this is done by including an attribute called "selected" in the option tag. the value of the "selected" attribute is set to "selected" for compliance with xhtml. thus the full tag would look something like:
Code:
<option value="something" selected="selected">something text</option>


the code i posted above compares the value of the incoming variable from the previously posted form to each of the values in the options. if the incoming variable is equal to the value of the "value" attribute then the code echos the 'selected="selected"' statement causing the client to display the option as selected. if there is no match then the selected statement is not echo'd and the client won't make this option look as if it is selected. More than one option can be selected if the select box is a multi-select.

radio buttons can be handled in exactly the same way. for each radio button you compare the value preset in the html tag with the incoming variable value and instead of the attribute-value pair 'selected="selected"' you use 'checked="checked"'.

hth
Justin

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top