1

I have the form with textarea field like this:

<form action="post.php" method="post" accept-charset="utf-8" id="myForm">
<textarea name="content" rows="10" cols="60"></textarea>
<input name="submit" type="submit" value="submit" />
</form>

I do the query to insert the values into the table like this:

$q = "INSERT INTO tableName (x, y, x) VALUES (1, a, b)";
        $r = $mysqli->query($q);
        if ($mysqli->affected_rows == 1) {
            echo '<p>Your post has been entered.</p>';

        } else {
            echo '<p>Your post could not be handled due to a system error.</p>';
        }

Assumed that the form was submitted successfully, and the message returned your post has been entered.

Nothing to say if the users who used the form to submit the values leave the page immediately right after that. However, the issue is that if he/she reloads the current page (by refreshing the browser) to submit it again and over again, the same values get inserted into the table, `which I do not want to'.

Can you help me to clear the value of the textarea field so that the form could not be re-submitted (due to the form validation), using php?

Thanks

Phong
  • 75
  • 4
  • 2
    A well known strategy for you problem is posting the form to a redirect handler. This way you would no resubmit the form when reloading the result/success page. – mblaettermann Jan 09 '16 at 11:32
  • So, can you advise me of the code to redirect it to the **newly-inserted** item? assumed that the `newly-inserted` item in the table has item id, subject and body. Note that I know how to redirect to the item page, but dont know how to redirect to the new item link. Thanks – Phong Jan 09 '16 at 11:35

5 Answers5

1

So the reason it keeps the same value is because the browser caches form input to help users who accidentally leave the page. (Eg: Click the 'back' button)

You can use a Javascript/jQuery approach as suggested by venkatKA, but I would suggest adding in your page's head:

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">

As suggested in How to prevent browser from caching form fields?

Community
  • 1
  • 1
MrD
  • 4,517
  • 9
  • 40
  • 78
0

clear all the form fields after submitting into database:

document.forms['form_name'].reset()
Sanzeeb Aryal
  • 4,155
  • 3
  • 16
  • 42
  • I already tried this, and **failed** because the input type of the form is '`submit`', but not `button`. reference: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_reset – Phong Jan 09 '16 at 11:52
0
<form action="post.php" method="post" accept-charset="utf-8" id="myForm" autocomplete="off">
        <textarea name="content" rows="10" cols="60"></textarea>
        <input name="submit" type="submit" value="submit" />
</form>

Please refer this URL to disable form autocomplete : http://www.w3schools.com/tags/att_input_autocomplete.asp

Milan
  • 529
  • 1
  • 3
  • 18
0

redirect the page to anywhere after hitting your query

use:

$q = "INSERT INTO tableName (x, y, x) VALUES (1, a, b";
    $r = $mysqli->query($q);
    if ($mysqli->affected_rows == 1) {
        header('location:note/form');
        echo '<p>Your post has been entered.</p>';
    } else {
        echo '<p>Your post could not be handled due to a system error.</p>';
    }

change the header location to the page where you want to redirect.

redirecting to another page will change the URL and the data will not remain in cache and no same record can be submitted after it.

Enjoy!

nerdyDev
  • 368
  • 3
  • 15
  • Good idea, but do you know how to redirect to the specific newly-added item which has just been inserted into the database, e.g., `http://domain.com/details.php?id=2` (number 2 is the item id), instead of the new page, e.g., `index.php` or so? – Phong Jan 09 '16 at 12:54
  • if you are opening link with the id which will be generated by the query. Then `$last_id=mysql_insert_id();` if will give you the id that is saved. and append it to the link to the `header('location:details.php?id='.$last_id);` – nerdyDev Jan 09 '16 at 12:57
0

Simply just add following in your js file

$("form :input").attr("autocomplete", "off");

or change your form tag as follows

 <form action="post.php" method="post" accept-charset="utf-8" id="myForm" autocomplete="off">
Yaxita Shah
  • 1,369
  • 1
  • 11
  • 16
  • The `autocomplete = "off"` does not work consistently in all browsers!. Practically, I already tried in my FF version 43.0.4, but failed! – Phong Jan 09 '16 at 12:34