douya5194
2014-04-12 12:35
浏览 103
已采纳

PHP:清除/重置表单,提交给自己

newb here.

Site url: http://school.max-o-matic.com/itc250.php#011

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 'http://school.max-o-matic.com/itc250.php#011' to 'http://school.max-o-matic.com/itc250.php' 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.

<?php

    //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

        echo
        '
            <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.

?>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doujiena0025 2014-04-12 21:11
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douduan4116 2014-04-12 21:15

    reloading the page to clear the form is bad practice, and the functionality you want is built in:

    <input type="reset" value="Clear" />
    

    Further reading: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input

    评论
    解决 无用
    打赏 举报
  • dppb79372 2015-03-20 20:53

    why don’t you do something simple like :

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <input type="submit" name="reset" value="reset">
    </form>
    

    works and is clean, I don’t really see the "bad practice" in that ?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题