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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.