Jump to content

Form with 2 Submit Buttons


yandoo

Recommended Posts

Hi there,

 

Im have a form that has a drop down and a user selects an option and clicks submits a record is inserted into a database and the inserted record is echoed to show whats in the table.

 

Accompaying each record that is echoed back is a check box. There are two submit buttons, one for inserting a record and the other for deleting a record (if a check box has been ticked by user next to echoed record).

 

Both the inserting query and delete query work independantly to each other and im am trying to integrate them...

 

Heres the basic bit to the delete query:

if(isset($_POST['perform'])) {

foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes

	$query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'"; // Change yourtable and id. 
	$test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error());
}
}
?>
<?php do { ?>
    <tr>
        
        <td align="center"><span class="style12"><?php echo $row_test['FearID']; ?></span></td><td><label>

          <input type="checkbox" name="id[<?php echo $row_test['AnimalFearID']; ?>]" id="id_<?php echo $row_test['AnimalFearID']; ?>" value="<?php echo $row_test['AnimalFearID']; ?>" />
        </label></td>
        
      </tr><?php } while ($row_test = mysql_fetch_assoc($test)); ?>

 

and heres the basic bit of code for the insert:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO animalfears (AnimalFearID, AnimalID, FearID) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['animalfearid'], "int"),
                       GetSQLValueString($_POST['animalid'], "int"),
                       GetSQLValueString($_POST['select'], "text"));

  mysql_select_db($database_woodside, $woodside);
  $Result1 = mysql_query($insertSQL, $woodside) or die(mysql_error());

  $insertGoTo = "add_animal_fear.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  <?php do { ?>
    <tr>
        
        <td align="center"><span class="style12"><?php echo $row_test['FearID']; ?></span></td><td><label>

          <input type="checkbox" name="id[<?php echo $row_test['AnimalFearID']; ?>]" id="id_<?php echo $row_test['AnimalFearID']; ?>" value="<?php echo $row_test['AnimalFearID']; ?>" />
        </label></td>
        
      </tr><?php } while ($row_test = mysql_fetch_assoc($test)); ?>

 

The Delete code was added to the insert form so only 1 form for both insert and delete  buttons....

 

Im thinknig perhaps an if else statement of some kind may be the right way to go .. lke:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

else 

if(isset($_POST['perform'])) {

foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes

	$query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'";  
	$test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error());
}
}

 

I want to make it so when either the insert or delete button is clicked that function is carried out....

 

How can i do this please? If any body could help that would be ace :

 

Thanks

 

Heres full code:

 

 
<?php require_once('Connections/woodside.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);

  $logoutGoTo = "login.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
$colname_user_conditional = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_user_conditional = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_woodside, $woodside);
$query_user_conditional = sprintf("SELECT * FROM `user` WHERE Username = '%s'", $colname_user_conditional);
$user_conditional = mysql_query($query_user_conditional, $woodside) or die(mysql_error());
$row_user_conditional = mysql_fetch_assoc($user_conditional);
$totalRows_user_conditional = mysql_num_rows($user_conditional);
?>

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO animalfears (AnimalFearID, AnimalID, FearID) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['animalfearid'], "int"),
                       GetSQLValueString($_POST['animalid'], "int"),
                       GetSQLValueString($_POST['select'], "text"));

  mysql_select_db($database_woodside, $woodside);
  $Result1 = mysql_query($insertSQL, $woodside) or die(mysql_error());

  $insertGoTo = "add_animal_fear.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$colname_animal = "-1";
if (isset($_GET['recordID'])) {
  $colname_animal = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_woodside, $woodside);
$query_animal = sprintf("SELECT * FROM animal WHERE AnimalID = %s", $colname_animal);
$animal = mysql_query($query_animal, $woodside) or die(mysql_error());
$row_animal = mysql_fetch_assoc($animal);
$totalRows_animal = mysql_num_rows($animal);

mysql_select_db($database_woodside, $woodside);
$query_fears = "SELECT * FROM fears ORDER BY FearID ASC";
$fears = mysql_query($query_fears, $woodside) or die(mysql_error());
$row_fears = mysql_fetch_assoc($fears);
$totalRows_fears = mysql_num_rows($fears);

$maxRows_animalfears = 10;
$pageNum_animalfears = 0;
if (isset($_GET['pageNum_animalfears'])) {
  $pageNum_animalfears = $_GET['pageNum_animalfears'];
}
$startRow_animalfears = $pageNum_animalfears * $maxRows_animalfears;

mysql_select_db($database_woodside, $woodside);
$query_animalfears = "SELECT * FROM animalfears";
$query_limit_animalfears = sprintf("%s LIMIT %d, %d", $query_animalfears, $startRow_animalfears, $maxRows_animalfears);
$animalfears = mysql_query($query_limit_animalfears, $woodside) or die(mysql_error());
$row_animalfears = mysql_fetch_assoc($animalfears);

if (isset($_GET['totalRows_animalfears'])) {
  $totalRows_animalfears = $_GET['totalRows_animalfears'];
} else {
  $all_animalfears = mysql_query($query_animalfears);
  $totalRows_animalfears = mysql_num_rows($all_animalfears);
}
$totalPages_animalfears = ceil($totalRows_animalfears/$maxRows_animalfears)-1;

mysql_select_db($database_woodside, $woodside);
$query_test = "SELECT * FROM animalfears WHERE AnimalID='" . $row_animal['AnimalID'] . "'";
$test = mysql_query($query_test, $woodside) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);

mysql_select_db($database_woodside, $woodside);
$query_fearslist = "SELECT * FROM fears WHERE FearID NOT IN (SELECT FearID FROM animalfears WHERE AnimalID='" . $row_test['AnimalID'] . "')";
$fearslist = mysql_query($query_fearslist, $woodside) or die(mysql_error());
$row_fearslist = mysql_fetch_assoc($fearslist);
$totalRows_fearslist = mysql_num_rows($fearslist);

if(isset($_POST['perform'])) {

foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes

	$query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'"; // Change yourtable and id. 
	$test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error());
}
}

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <table width="362" border="0">
        <tr>
          <td width="109"><span class="style11">Add New Fear:</span></td>
          <td width="108" align="center"> <label>
  <select name="select">
    <?php
do {  
?>
    <option value="<?php echo $row_fearslist['FearID']?>"><?php echo $row_fearslist['FearID']?></option>
    <?php
} while ($row_fearslist = mysql_fetch_assoc($fearslist));
  $rows = mysql_num_rows($fearslist);
  if($rows > 0) {
      mysql_data_seek($fearslist, 0);
  $row_fearslist = mysql_fetch_assoc($fearslist);
  }
?>
  </select>
  </label></td>
          <td width="123" align="center"><input type="submit" name="Submit" value="Submit" /></td>
        </tr>
        <tr>
          <td> </td>
          <td> </td>
          <td> </td>
        </tr>
        <tr>
          <td> </td>
          <td width="109 "align="center"><table width="109" border="0" class="newbord">
      <tr><td align="center"><span class="style7 style13"><strong>This Animals Fears:</strong></span></td>
      </tr>
  <?php do { ?>
    <tr>
        
        <td align="center"><span class="style12"><?php echo $row_test['FearID']; ?></span></td><td><label>

          <input type="checkbox" name="id[<?php echo $row_test['AnimalFearID']; ?>]" id="id_<?php echo $row_test['AnimalFearID']; ?>" value="<?php echo $row_test['AnimalFearID']; ?>" />
        </label></td>
        
      </tr><?php } while ($row_test = mysql_fetch_assoc($test)); ?>
          </table></td>
          <td><label>
             <input type="submit" name="perform" id="perform" value="Delete" /> 
          </label></td>
        </tr>
        <tr>
          <td> </td>
          <td> </td>
          <td> </td>
        </tr>
        <tr>
          <td> </td>
          <td align="center"><a href="add_animal2.php?recordID=<?php echo $row_animal['AnimalID']; ?>" class="style7">Continue</a></td>
          <td> </td>
        </tr>
      </table>  <p>
    <input name="animalfearid" type="hidden" id="animalfearid" value="<?php echo $totalRows_animalfears +1 ?>" />
    <input name="animalid" type="hidden" id="animalid" value="<?php echo $row_animal['AnimalID']; ?>" />
  </p>
  <p>
    <label>    
    </label>
  </p>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<?php
mysql_free_result($user_conditional);
mysql_free_result($animal);
mysql_free_result($fears);
mysql_free_result($animalfears);

 

:o

Link to comment
https://forums.phpfreaks.com/topic/98387-form-with-2-submit-buttons/
Share on other sites

I don't have time to read through all of your code right now but I had a similar problem. I was trying to add a video to the db but first be able to see it and either accept it or go back and edit the form. The way I finally got it to work was to use "hidden" form variables with a switch statement. One button would send all of the data as session variables with one of them being the "hidden" variable. The hidden variable was called "preview" for when the preview button was pressed and "add" for when the add button was pressed. This way I could input the data into a form. Hit a button. A new page loads showing the entered data. There are two buttons at the bottom of the data, Edit, and, Add. The edit would take you back to the form and input all of the data back in. The add would add the data to the DB. This is all on one script as well.

 

I know that was a dense explanation, sorry.

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.