user3235574
user3235574

Reputation: 103

how to unsave if page refreshed

I got a code here that if I refreshed the page it automaticaly save the data....can anyone help me that it will only save if the submit button is clicked.

current code:

<?php 
ob_start();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
include('include/connect.php');

$query = mysql_query("SELECT DISTINCT count(batchcode) as batchcode1 FROM batchcodes");
while( $rows = mysql_fetch_array($query)) {
    $code=$rows['batchcode1'];
}

if(isset($_POST['save'])){
    $var = $code+1;
    $sql = mysql_query("INSERT INTO batchcodes(batchcode) VALUES (". $var .")");
}
?>
<form method="post" action="index.php" >
<input type="text" value="batch<?php echo $var; ?>" />
<input type="submit" name="save">
</form>
</body>
</html>

Upvotes: 6

Views: 150

Answers (4)

efux
efux

Reputation: 466

As soon as the form is submitted once it has got the $_POST-Array in the site request. When you reload the page after the first submit, it will always send the data again.

You got multiple possibilities to resolve this problem:

1)

Reload the page after the execution of the PHP code. To do so put the PHP code at the top of the page (before writing anything in HTML) and reload the page after the execution of the query:

if(isset($_POST["save"])) {
    /* MySQL Query */
    $back = $_SERVER['HTTP_REFERER'] ; // the site who called this site
    header("Location: $back") ;        // Link back to this site
}

2)

Personally I prefer to execute my PHP scripts with an Ajax call, which would look as follows in jQuery.

function ajaxCall()
{
    $.ajax({
         type: "POST",
         url: "handler.php",
         data: {save: 1, textfield: $("#textfield").val()}
    }) ;
}

Don't forget, that the forms action isn't the redirect to another site anymore, it is the call to this function ajaxCall. If you want more fields to submit, have a look at the serialize-function. The handler.php-file contains only your php-Code:

<?php
    ob_start();
    include('include/connect.php');

    $query = mysql_query("SELECT DISTINCT count(batchcode) as batchcode1 FROM batchcodes");
    while( $rows = mysql_fetch_array($query)) {
        $code=$rows['batchcode1'];
    }

    if(isset($_POST['save'])){
        $var = $code+1;
        $sql = mysql_query("INSERT INTO batchcodes(batchcode) VALUES (". $var .")");
    }

   exit(0) ;
?>

In the ajax function you could also handle what happens when the call is successful (e.g. redirect). Have a look at the $.ajax-reference of jQuery. If you want you could also use ajax without jQuery.

3)

You could also make your page in action similiar to the handler.php in the second possibility.

<form action="handler.php" method="POST"></form>

In this case you had to replace the exit-statement with the $back and header-call in possibility 1 (similar to the response of Konerak).

Upvotes: 0

Nagesh Reddy
Nagesh Reddy

Reputation: 9

Create one button in html

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

In php Code, you can write like

<?php
if(isset($_POST['submit']))
{

  //place your total code here

}
?>

Upvotes: 0

Mr Cathode
Mr Cathode

Reputation: 77

The problem is the form is getting submitted again, you can make header redirect to this same page,

header("location: index.php) after updating your database and this will solve your issue.

Upvotes: 0

Konerak
Konerak

Reputation: 39763

The code you show is from your "handling" page. That page handles the post, it checks if there was a parameter "save" and if so, it saves.

If the user refreshes that page, he visits the page again, sending again a "save" parameter, so the INSERT is done twice.

To avoid this, you should use the POST-REDIRECT-GET model, where your handling page gets the data, saves it, and then redirects the user to a "GET" page (no post, no insert) that just shows the data. If the user then hits refresh, he only refreshes the "GET" page.

Offcourse, a user can always keep using the BACK button to go to the actual insert page. His browser will warn him "you are resubmitting form data...", but if he chooses to, he can. If you really want to handle this, you can work with session keys: have an extra field "submitID" on your form, and on INSERT, first check if that ID was already "used". You'll need an extra table/column "submitID" somewhere to ensure a form can only be submitted once.

Upvotes: 1

Related Questions