Jump to content

when webpage is refreshed, $_POST["form"] remains "Submit"


aooga

Recommended Posts

when webpage is refreshed, $_POST["form"] remains "Submit"

Basically I have a form on the site that refreshes the page and contains an

if ($_POST["form"] == "Submit"){

// add X into database

}

so every time page is refreshed X is added! Is there an elegant way to avoid this?

Sure

File named index.php{

 

if ($_POST["form"] == "Submit"){

 

$name = $_POST['name'];

$startDate = $_POST['startDate'];

$endDate = $_POST['endDate'];

$duration = $_POST['duration'];

$type = $_POST['type'];

$source = $_POST['source'];

$details = $_POST['details'];

 

$sql = "

INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details)

VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details')

";

$query = mysql_query($sql, $con);

}

 

 

<form action="index.php" method="post">

<tr>

<td><input type="text" name="name" value="" /></td>

<td><input type="text" name="startDate" value="" /></td>

<td><input type="text" name="endDate" value="" /></td>

<td><input type="text" name="duration" value="" /></td>

<td><input type="text" name="type" value="" /></td>

<td><input type="text" name="source" value="" /></td>

<td><input type="text" name="details" value="" /></td>

</tr>

 

</table>

<input type="submit" name="form" value="Submit" />

</form>

}

 

After submitting 1 name, every time I refresh it is duplicated.

why dont you actually make it check the variables. something like

 


if (($name != "") && ($startDate != "")
{
$sql = "
      INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details)
      VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details')
      ";
      $query = mysql_query($sql, $con);
}


 

You would have to continue on with the variable checking but basically this will not post unless something is set for the variables. The way you have it now it will post if the submit button is there.

Refreshing automatically resends previously posted data.  That's why it's entering more stuff into db.  You can check if it's set or != '' and everything else and it will still evaluate true.

 

That's a clientside thing.  Possibly sending a no-cache header might work.

see if this helps:

 

if ($_POST["form"] == "Submit"){
   
   $name = $_POST['name'];
   $startDate = $_POST['startDate'];
   $endDate = $_POST['endDate'];
   $duration = $_POST['duration'];
   $type = $_POST['type'];
   $source = $_POST['source'];
   $details = $_POST['details'];
   
      $sql = "
      INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details)
      VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details')
      ";
      $query = mysql_query($sql, $con);

      unset($_POST["form"]);
}


<form action="index.php" method="post">
<tr>
<td><input type="text" name="name" value="" /></td>
<td><input type="text" name="startDate" value="" /></td>
<td><input type="text" name="endDate" value="" /></td>
<td><input type="text" name="duration" value="" /></td>
<td><input type="text" name="type" value="" /></td>
<td><input type="text" name="source" value="" /></td>
<td><input type="text" name="details" value="" /></td>
</tr>

</table>
<input type="submit" name="form" value="Submit" />
</form>
}

Use header to redirect back to index.php to clear up the post data.

 

if ($_POST["form"] == "Submit"){
   
   $name = $_POST['name'];
   $startDate = $_POST['startDate'];
   $endDate = $_POST['endDate'];
   $duration = $_POST['duration'];
   $type = $_POST['type'];
   $source = $_POST['source'];
   $details = $_POST['details'];
   
      $sql = "
      INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details)
      VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details')
      ";
      $query = mysql_query($sql, $con);
      header("Location: index.php");
}


<form action="index.php" method="post">
<tr>
<td><input type="text" name="name" value="" /></td>
<td><input type="text" name="startDate" value="" /></td>
<td><input type="text" name="endDate" value="" /></td>
<td><input type="text" name="duration" value="" /></td>
<td><input type="text" name="type" value="" /></td>
<td><input type="text" name="source" value="" /></td>
<td><input type="text" name="details" value="" /></td>
</tr>

</table>
<input type="submit" name="form" value="Submit" />
</form>

 

I usually send users to a "confirmation" page so they know it was entered. Using the header redirect will clear out any post data so even if they hit back it will not re-post the data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.