Jump to content

After refreshing, another form is submitted


nitation

Recommended Posts

Hello,

 

I have a php form script whereby, if i insert a record and refresh the page, it will automatically add another row. What am i doing wrong. Kindly verify.

 

$addcattab=true;
$showcattab=true;
$edittab=false;
$deletetab=false;
$msg=ADMIN_ADD_SUB_LINKS;


if($_POST['load']){

if(trim($name)==""){
$msg=ADMIN_ADDSUBLINKNAME_ERROR;
}

if(trim($title)==""){
$msg=ADMIN_ADDSUBLINKTITLE_ERROR;
}

else if(($name) && ($title)){
$SqlAdd=mysql_query("insert into southplinks_sub(name,title,DataInsertDate,ownerID,status) values('$name', '$title', NOW(),'$ownercat','0')") or die(mysql_error());

$msg=ADMIN_SUCCESS_ADDITION;
  }
}

if($edit){
$msg=ADMIN_EDIT_SUBLINKS;
$addcattab=false;
$showcattab=false;
$edittab=true;
$Sqleditcat=mysql_query("select * from southplinks_sub where id='$edit'") or die (mysql_error());
if(!empty($Sqleditcat)){
$row=mysql_fetch_array($Sqleditcat);
              $id=$row["id"];
		  $name=$row["name"];
		  $title=$row["title"]; 
		  $status_edit=$row["status"];      
  }
}

if($editcat){

if(trim($name)==""){
$msg=ADMIN_ADDSUBLINKNAME_ERROR;
}

if(trim($title)==""){
$msg=ADMIN_ADDSUBLINKTITLE_ERROR;
}

else if(($name) && ($title)){
$Sqlupdate=mysql_query("UPDATE southplinks_sub  SET  name= '$name', title= '$title', status= '$status', ownerID= '$ownercat', DataUpdateDate= NOW() WHERE id='$catid'") or die (mysql_error());
$msg=ADMIN_SUCCESS_EDITTING;
$addcattab=true;
$showcattab=true;
$edittab=false;
     }
}

if($delete){
$msg=ADMIN_DELETE_QUESTION;
$addcattab=false;
$showcattab=false;
$deletetab=true;
}

if($delcat){

$sqldelete=mysql_query("DELETE FROM southplinks_sub WHERE  id='$catid'") or die (mysql_error());
$msg=ADMIN_SUCCESS_DELETE;
$addcattab=true;
$showcattab=true;
$deletetab=false;
}

require ("templates/header_html.php");

?>
<div id="colone">
  <?php if($addcattab){ ?>
<form action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">

		<fieldset>
           <legend><b><font color="#003399"><? print $msg; ?></font></b></legend>
                <table width="300" height="50" bordercolor="#FF9900">
                    <tr> 
			   <td width="100" align="right" valign="top">
			   <label class="loginLabel" for="loginname"><?php print ADMIN_SUBLINK_NAME; ?></label></td>
                    <td width="200"><input name="name" type="text" style="float: left" maxlength="100" value=""></td>
                  </tr>
			     <tr> 
			   <td width="100" align="right" valign="top">
			   <label class="loginLabel" for="loginname"><?php print ADMIN_SUBLINK_TITLE; ?></label></td>
                    <td width="200"><input name="title" type="text" style="float: left" maxlength="100" value=""></td>
                  </tr>
			  
			   <tr> 
			   <td width="100" align="right" valign="top">
			   <label class="loginLabel" for="loginname"><?php print ADMIN_LINK_CAT; ?></label></td>
                    <td width="200"><select name="ownercat">
                          <?php 
		              $Sqlcat=mysql_query("select * from southplinks_main order by id ");
		              if(!empty($Sqlcat)){
		              while($row=mysql_fetch_array($Sqlcat)){
		              $cat_name=$row["name"];
		              $cat_id=$row["id"];
		               ?>
                          <option value="<?php print "$cat_name" ?>"><?php print "$cat_name";?></option>
                          <?php 
					    }
					  }
					  ?>
                        </select></td>
                  </tr>
                   
			        <tr align="center" valign="bottom"> 
                    <td colspan="2"><input type="submit" name="load" class="button" value="<?php print ADMIN_UPLOAD_BUTTON; ?>" />
									</td>
                  </tr>
                </table></fieldset>
<?php }  ?> 
</form>
</div>

<TD colspan="8" rowspan="4" align="center" valign="baseline"> 
          <?php if($showcattab){ ?>
          <table width="638" align="center">
            <!--DWLayoutTable-->
            <tr align="left"> 
              <td width="294" height="21" valign="top" scope="col"><font color="#003399"><b><?php print ADMIN_SUBLINK_NAME; ?></b></font></td>
              <td width="131" align="center" valign="top" scope="col"><font color="#003399"><b><?php print ADMIN_EDIT; ?></b></font></td>
              <td width="96" align="center" valign="top" scope="col"><font color="#003399"><b><?php print ADMIN_DELETE; ?></b></font></td>
              <td colspan="2" valign="top"><div align="center"><font color="#003399"><b><?php print ADMIN_PUBLISH; ?></b></font></div></td>
            </tr>
             <?php $Sqlcat=mysql_query("select * from southplinks_sub order by id ") or die (mysql_error());
		  if(!empty($Sqlcat)){
		  while($row=mysql_fetch_array($Sqlcat)){
		  $linkname=$row["name"];
		  $id=$row["id"];
		  $ownerID=$row["ownerID"];
		  $status_edit=$row["status"];
		  
              if($status==1){
		  $activeuser=$active;
		    } else{
		    $activeuser=$innactiv;
		    }
		    ?>
            <tr> 
              <td height="21" align="left" valign="top"><a href="<?php  print $_SERVER['PHP_SELF'] . "?edit=$id"; ?>"><?php print $linkname; ?></a> [ Belongs to : <?php print $ownerID; ?>]</td>
              <td align="center" valign="top"><a href="<?php print $_SERVER['PHP_SELF'] . "?edit=$id"; ?>"><img src="images/edit.gif" width="16" height="14" border="0"></a></td>
              <td align="center" valign="top"><a href="<?php print $_SERVER['PHP_SELF'] . "?delete=$id"; ?>"><img src="images/delete.gif" width="16" height="14" border="0"></a></td>
              <td width="2"> </td>
              <td width="80" align="center" valign="top" onClick="document.location.href='<?php print $_SERVER['PHP_SELF'] . "?edit=$id"; ?>'"><?php print $activeuser; ?></a></td>
            </tr>
            
            
            
            
            
            
            <?php 
		    } 
		  } ?>
          </table>
          <?php } ?>
           </TD>
        <TD></TD>
      </TR>
  
<div id="colone">

  <?php if($edittab){ ?>
<form action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
		<fieldset>
           <legend><font color="#003399"><b><? print $msg; ?></b></font></legend>
	    <input name="catid" type="hidden" value="<?php print "$edit" ?> ">
                <table width="300" height="50" bordercolor="#FF9900">
                    <tr> 
			   <td width="100" align="right" valign="top">
			   <label class="loginLabel" for="loginname"><?php print ADMIN_LINK_NAME; ?></label></td>
                    <td width="200"><input name="name" type="text" style="float: left" value="<?php echo $name; ?>" maxlength="100"></td>
                  </tr>
			                       <tr> 
			   <td width="100" align="right" valign="top">
			   <label class="loginLabel" for="loginname"><?php print ADMIN_LINK_TITLE; ?></label></td>
                    <td width="200"><input name="title" type="text" style="float: left" value="<?php echo $title; ?>" maxlength="100"></td>
                  </tr>
			     <tr> 
			   <td width="100" align="right" valign="top">
			   <label class="loginLabel" for="loginname"><?php print ADMIN_STATUS; ?></label></td>
                    <td width="200"><?

          echo "<SELECT name=\"status\">
		<OPTION value=\"\">--</OPTION>";
		$pub_array = array("1", "0");
		foreach ($pub_array as $pub) {
			if ($pub == "$status_edit") {
				echo "<OPTION value=\"$pub\" selected>$pub</OPTION>";
			} else {
				echo "<OPTION value=\"$pub\">$pub</OPTION>";
			}
		}
		echo "</SELECT>";
   ?></td>
                  </tr>
			  				   <tr> 
			   <td width="100" align="right" valign="top">
			   <label class="loginLabel" for="loginname"><?php print ADMIN_LINK_CAT; ?></label></td>
                    <td width="200"><select name="ownercat">
                          <?php 
		              $Sqlcat=mysql_query("select * from southplinks_main order by id ");
		              if(!empty($Sqlcat)){
		              while($row=mysql_fetch_array($Sqlcat)){
		              $cat_name=$row["name"];
		              $cat_id=$row["id"];
		               ?>
                          <option value="<?php print "$cat_name" ?>"><?php print "$cat_name";?></option>
                          <?php 
					    }
					  }
					  ?>
                        </select></td>
                  </tr>
			  
	  <tr align="center" valign="bottom"> 
                    <td colspan="2"><input type="submit" name="editcat" class="button" value="<?php print ADMIN_EDIT_BUTTON; ?>" />
									</td>
                  </tr>
                </table></fieldset>
<?php }  ?></form> </div>
</div>  

<div align="center">
  <?php if($deletetab){ ?>
        <table width="400" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
          <tr> 
            <td bgcolor="#ffffff" scope="col">
		<div align="center">
		<b><font color="#003399">
		<?php print $msg; ?></font></b></div></td>
          </tr>
          <tr> 
            <td scope="col"><table width="400">
                <tr bordercolor="#CCFFFF"> 
                  <td width="208" align="right" valign="top" bordercolor="#CCFFFF" scope="col"> 
                    <form action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
                      <input name="catid" type="hidden" value="<?php print "$delete" ?> ">
                      <table width="100" border="0" cellspacing="0" cellpadding="0">
                        <tr> 
                          <td align="center" scope="col"><input name="delcat" type="submit" value="<?php print "$Submit_yes"; ?>" > 
                          </td>
                        </tr>
                      </table>
                    </form></td>
                  <td width="180" align="center" valign="top" bordercolor="#CCFFFF" scope="col"> 
                    <form action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
                      <table width="100" border="0" cellspacing="0" cellpadding="0">
                        <tr> 
                          <td scope="col"><input name="notdelcat" type="submit" value="<?php print "$Submit_no"; ?>" ></td>
                        </tr>
                      </table>
                    </form></td>
                </tr>
              </table></td>
          </tr>
        </table>
        <?php }  ?>
		  </div>

the easiest way to do it it is to make the form go to another pure php page that does the inserting etc then when complete returns you to the main page, this will then clear the $POST variable and then it will not re-insert the same data.

@ Paul

 

If i get u right Paul, are you saying that am experiencing that error because my form is processed on a single page? I do not want another pure php page for inserting. I wanna insert on the same page. Can anyone please tell me what am not doing right?

To use a single page, you will need to set a session variable that says the data has already been processed and then check this session variable on each page request to prevent multiple submissions from being inserted.

 

Browsers think they are doing you a favor by remembering form data and resubmitting it when the url of the action="..." parameter is requested again.

 

Another solution for having the form and the form processing code on the same page involves submitting to an intermediate page (that has a different url than the form/actual form processing page, save any form data in a session on this intermediate page, and then redirect back to the single form/form processing url.) By doing this, the url of the action="..." parameter is different than the form/form processing url and if you refresh or hit the back button, the browser won't attempt to resubmit the form 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.