Forum Moderators: coopster

Message Too Old, No Replies

Reset doesn't work on this php form

How to get Reset button to work in

         

vinai

10:47 am on Aug 29, 2007 (gmt 0)

10+ Year Member



Hi,

I have written a form which takes keywords, and returns them in another textarea with quotation marks on either side.

The problems I am facing are:
1. I am not able to get the Reset button to Blank out the two Text areas.
2. Ideally I would like to accomplish this within 1 form element only - containing both the text areas.

Please suggest what is wrong in this code. Thanks.

<body>
<table width="300" border="0" cellspacing="5" cellpadding="5">
<tr>
<td align="left">
<form name="a" method="POST" action="test1.php">
<?php
$outlist = $_POST['outlist'];
$text = str_replace("\r", "", $_POST['search']);//to process the windows new line
$arrtext = explode("\n", $text); // create array of keywords
echo "Key in one keyword in each row<br>";
echo "<textarea name=\"search\" cols=\"25\" rows=\"10\">$text</textarea>";
foreach($arrtext as $keyword)
{
$outlist = $outlist . '"' . $keyword . '"' . "<br>";
}
$outlist = str_replace("<br>", "\n", $outlist); //to process the windows new line
?>
</form></td>
<td align="center">
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Reset" value="Reset " />
</td>
<td>
Output List:
<?php
echo "<form>";
echo "<textarea name=\"outlist\" cols=\"25\" rows=\"10\">$outlist</textarea></form>";
?>
</td>
</tr>
</table>
</body>

d40sithui

11:15 am on Aug 29, 2007 (gmt 0)

10+ Year Member



your reset doesnt work because you prematurely ended your form before your reset button. try changing to this code

<body>
<table width="300" border="0" cellspacing="5" cellpadding="5">
<tr>
<td align="left">
<form name="a" method="POST" action="test1.php">
<?php
$outlist = $_POST['outlist'];
$text = str_replace("\r", "", $_POST['search']);//to process the windows new line
$arrtext = explode("\n", $text); // create array of keywords
echo "Key in one keyword in each row<br>";
echo "<textarea name=\"search\" cols=\"25\" rows=\"10\">$text</textarea>";
foreach($arrtext as $keyword)
{
$outlist = $outlist . '"' . $keyword . '"' . "<br>";
}
$outlist = str_replace("<br>", "\n", $outlist); //to process the windows new line
?>
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Reset" value="Reset " />
</td>
<td>
Output List:
<?php
echo "<textarea name=\"outlist\" cols=\"25\" rows=\"10\">$outlist</textarea></form>";
?>
</td>
</tr>
</table>
</body>

vinai

11:42 am on Aug 29, 2007 (gmt 0)

10+ Year Member



Thanks for your advice. I removed the form, and moved it to the end of the table, but to no avail.

here's the latest code now, and reset is still not working. Please help.

<body>
<table width="300" border="0" cellspacing="5" cellpadding="5">
<tr>
<form name="a" method="POST" action="test1.php">
<td align="left">
<?php
$outlist = $_POST['outlist'];
$text = str_replace("\r", "", $_POST['search']);//to process the windows new line
$arrtext = explode("\n", $text); // create array of keywords
echo "Key in one keyword in each row<br>";
echo "<textarea name=\"search\" cols=\"25\" rows=\"10\">$text</textarea>";
foreach($arrtext as $keyword)
{
$outlist = $outlist . '"' . $keyword . '"' . "<br>";
}
$outlist = str_replace("<br>", "\n", $outlist); //to process the windows new line
?>
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Reset" value="Reset " />
</td>
<td>
Output List:
<?php
echo "<textarea name=\"outlist\" cols=\"25\" rows=\"10\">$outlist</textarea>";
?>
</td></form>
</tr>
</table>
</body>

deMorte

12:52 pm on Aug 29, 2007 (gmt 0)

10+ Year Member



Reset button does not empty fields that come from PHP variables. So if you haven't submitted the data yet it should work normally.

One solution could be to create a PHP-script that empties the variables when Reset is pressed. Something along the lines:

<?php
if($_POST["Reset"]) {

$outlist = "";
$text = "";

}
?>

I'm not sure how the reset button actually works (is it sent in POST?) and a quick Google did not reveal the answer so if this does not work, change the type of the Reset button to Submit and name it "Reset".

Hope this helps some.

d40sithui

2:26 pm on Aug 29, 2007 (gmt 0)

10+ Year Member



you can try using JS see below.

<body>
<script>
function resetForm(){
document.forms[0].search.value = "";
document.forms[0].outlist.value= "";
}
</script>
<table width="300" border="0" cellspacing="5" cellpadding="5">
<tr>
<form name="a" method="POST" action="index.php">
<td align="left">
<?php
$outlist = $_POST['outlist'];
$text = str_replace("\r", "", $_POST['search']);//to process the windows new line
$arrtext = explode("\n", $text); // create array of keywords
echo "Key in one keyword in each row<br>";
echo "<textarea name=\"search\" cols=\"25\" rows=\"10\">$text</textarea>";
foreach($arrtext as $keyword)
{
$outlist = $outlist . '"' . $keyword . '"' . "<br>";
}
$outlist = str_replace("<br>", "\n", $outlist); //to process the windows new line
?>
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit" />
<input type="button" name="Reset" onclick="resetForm();" value="Reset " />
</td>
<td>
Output List:
<?php
echo "<textarea name=\"outlist\" cols=\"25\" rows=\"10\">$outlist</textarea>";
?>
</td></form>
</tr>
</table>
</body>

vinai

3:04 pm on Aug 29, 2007 (gmt 0)

10+ Year Member



Thanks a lot to both of you. Both of your tips worked - setting the Reset button as as "Submit" button, and using JS to validate.

I really appreciate your quick help and support.

Best of Luck...

Cheers,
vinai