Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.