Chezshire Chezshire - 8 months ago 70
PHP Question

PHP: Clear/reset form which submits to self

newb here.

Site url:

My student portal has two assignments on it which i've completed, both are forms and I'd like to be able to reset/clear them. I know that i could create a button to clear them that sends the user from '' to '' which would remove the anchor tag and that would reload the page clearing/resetting the forms in the process. But it would also require scrolling. I could also create a second anchor tag ('#011b') and have that in an invisible element and reload the page to that to keep/prevent scrolling but that seems like cheating/a dirty hack to me vs good programmatic practice.

I attempted to use this to reset the form ('echo '') but it doesn't seem to do anything. So i'm hoping someone can help me to find a good method to achieve my end goal which is to have a repeatable tool that will clear/reset the page so i can test out multiple scenarios without needing to scroll/rescroll continuously.


//Constant never changes, 100% available everywhere- almost always simple datatypes (T or F, num, string)
//define('THIS_PAGE', $_SERVER['PHP_SELF']);//shows webroot
define('THIS_PAGE', basename($_SERVER['PHP_SELF']));//shows app root - gets name of the file basically

define('TAX_RATE', .095); //current tax rate us => .095 => is 9.5%

//echo THIS_PAGE;// shows this pagefile
//VAR_DUMP - we love it cause we can see everything

if (isset($_POST['submit'])) {//if data, do processing

$purch01 = (float) $_POST['purchase01'];//float before floats data after
$purch02 = (float) $_POST['purchase02'];//float before floats data after

$subtotal = $purch01 + $purch02;

$amountTax = TAX_RATE * $subtotal;

$amountTotal = $amountTax + $subtotal;

echo "<br /><br />";

echo 'First Purchase: $' . round($purch01, 2) . '<br />';//standard
echo 'First Purchase: $' . round($purch02, 2) . '<br />';//standard
echo '<hr />';

echo 'Subtotal before tax: $' . round($subtotal, 2) . '<br />';//standard
echo 'Tax: $' . round($amountTax, 2) . '<br />';//standard
echo '<hr />';

echo 'Washington state tax: %' . TAX_RATE . '<br />';
echo '<hr />';

echo 'your purchase amount is $' . round($amountTotal, 2) . '<br /><br />';//risky double dollar sign

echo '<pre>';
var_dump($_POST); //shows everything the post knows
echo '</pre>';
echo '<input type="reset" "reset the form." /><br /><br /><hr />';

}else{//show Output

<form method="post" action="itc250.php#011">

Enter Purchase Amount:
<input type="text" name="purchase01" />
<br />
<br />

Enter Purchase Amount:
<input type="text" name="purchase02" />
<br />

<input type="submit" name="submit" />

}//if you dont' need else, do a 'print and die statement' so if else happens you know else happened.


Answer Source

I found a solution which is as follows:

In the head I added:

<script> function reloadPage() { location.reload(); } </script>

In the body I added:

<input type="button" value="Reload page" onclick="reloadPage()">

This does what I want and it's something I can neatly apply over and over again so that's cool.