Asked  7 Months ago    Answers:  5   Viewed   40 times

Is there a way to do the above? Basically, I don't want the form to be submitted again if someone presses refresh after already submitting the form once. In which case the browser asks, do you want to submit the form again. Will unset($_POST['username']) be of any help is this case?

 Answers

87

The post/redirect/get is a good option as some posters have already mentioned.

One another way I can think of is to set a session in the dostuff.php page to indicate that the posting has already been done. Check this session var each time to see if the page is being loaded again because of a page refresh.

<?php
    session_start();
    if(isset($_SESSION['indicator'])) 
    {
        /*
        dont do anything because session indicator says 
        that the processing was already done..

        you might want to redirect to a new url here..          
        */
    }   
    else
    {

        /*
        first set session indicator so that subsequent 
        process requests will be ignored
        */
        $_SESSION['indicator'] = "processed"; 

        //process the request here..
    }
    ?>

In the page you redirect to, unset the session var so that the form can be resubmitted again afresh, making it a new post operation. This will allow new form posts but will prevent post operations due to page refresh

Wednesday, March 31, 2021
 
Novalirium
answered 7 Months ago
94

I would suggest something like:

<label for="user_firstname">Nume</label>
    <input id="user_firstname" type="text" name="user_firstname" value=<?(isset($_POST['user_firstname']) ? $_POST['user_firstname'] : ""; ?>>
    <? if (isset($errors['user_firstname'])): ?>
    <span class="error"><?= $errors['user_firstname']; ?></span>
<? endif; ?> 

You also had a typo in the $_POST["user_firstmane"] should be $_POST["user_firstname"] :)

Wednesday, March 31, 2021
 
vuliad
answered 7 Months ago
97

i think you already suggested the answer to your problem . it owuld be better to validate other fields of the form using ajax and don't submit the file initially. anbd if you have some problems with the file you can have some basic checks for your file using like file extension using ajax

Wednesday, March 31, 2021
 
hakimoun
answered 7 Months ago
93

the solution - not nice but it works - thanks for all the comments and help!!!

<?php include("db_connect.php");?>

<html>
<head>
</head>
<body>
<form method="post" action="test.php">

New:

<?php
$left='SELECT * FROM test1 ORDER BY name ASC';
$result1=mysql_query($left);
$count=mysql_num_rows($result1);
while($resulta = mysql_fetch_array($result1))
  {
?>

<input name="checkbox_add[]" type="checkbox" id="checkbox_add[]" value="<? echo $resulta['id']; ?>"/> <? echo $resulta['name']; ?>
<br />
<?php
   }
?>

</td> <td><input type="submit" id="add" name="add" value="Add" /><br /><input type="submit" id="delete" name="delete" value="Del" /></td><td>
<?php
$right='SELECT test2.id, test1.name FROM test2, test1 WHERE test1.id=test2.collect AND test2.status=1';
$result2=mysql_query($right);
while($resultb = mysql_fetch_array($result2))
  {
?>
   <input name="checkbox_del[]" type="checkbox" id="checkbox_del[]" value="<?php echo $resultb['id']; ?>"/>, <?php echo $resultb['id']; ?>, <? echo $resultb['name']; ?>
<br />
<?php
  }
?>
</td></tr></table>


<?php
// Check if add button active, start this
if (isset($_POST['add'])) {
  for ($c = 0; $c < count($_POST['checkbox_add']); $c++){
    $checkbox_add = $_POST['checkbox_add'][$c];
    $sql = "INSERT INTO test2 (status, collect) VALUES(1, ".$checkbox_add.")";
    echo $sql;
    $result = mysql_query($sql);
    if($result){
      echo "<meta http-equiv="refresh" content="0;URL=test.php">";
      }
    }
  }
elseif (isset($_POST['delete'])) {
  for ($c = 0; $c < count($_POST['checkbox_del']); $c++){
    $checkbox_del = $_POST['checkbox_del'][$c];
    echo date("Y-m-d");
    $sql = "UPDATE  test2 SET status='2', log='".date('Y-m-d')."' Where id=".$checkbox_del;
    echo $sql;
    $result = mysql_query($sql);
    if($result){
      echo "<meta http-equiv="refresh" content="0;URL=test.php">";
     }
    }
  }
elseif (isset($_POST['new'])) {
    $sql = "INSERT INTO test1 (status, name) VALUES(1, '".$_POST['newitem']."')";
    echo $sql;
    $result = mysql_query($sql);
    if($result){
      echo "<meta http-equiv="refresh" content="0;URL=test.php">";
    }
  }
mysql_close();
?>

</form>

</body>
</html>
Wednesday, March 31, 2021
 
Amber
answered 7 Months ago
69

Your input elements do not have name attributes. Should be:

<input type="text" id="login_email" name="login_email" />

If an input element does not have a name attribute, it is not sent as part of the POST data.

Friday, July 30, 2021
 
xrdty
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share