Skip to main content
Known Participant
February 27, 2010
Question

How do I change the behaviour of an update button.

  • February 27, 2010
  • 1 reply
  • 385 views

I have a variable - $UpdateFlag, which contains a string and updates a table with it on the click of the update button.

The problem I have is that if the string is blank, I don't want to update, and I want the button to exit instead.

here's the code. At the moment the button does nothing if $UpdateFlag is blank.

How do I change the behaviour of the button so that it can update or exit based on $UpdateFlag?

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

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") and $UpdateFlag !='' ) {
  $updateSQL = sprintf("UPDATE offer SET status=%s WHERE offer_id=%s",
                       GetSQLValueString($UpdateFlag ,"text"),
                       GetSQLValueString($_GET['offer_id'], "int"));

  mysql_select_db($database_guitarswap_db, $guitarswap_db);
  $Result1 = mysql_query($updateSQL, $guitarswap_db) or die(mysql_error());


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

}

?

This topic is closed to new replies. Start a new post to keep the conversation going.

1 reply

David_Powers
Inspiring
February 28, 2010

Add an else statement to do what you want if the flag is empty.

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") and $UpdateFlag !='' ) {
  $updateSQL = sprintf("UPDATE offer SET status=%s WHERE offer_id=%s",
                       GetSQLValueString($UpdateFlag ,"text"),
                       GetSQLValueString($_GET['offer_id'], "int"));

  mysql_select_db($database_guitarswap_db, $guitarswap_db);
  $Result1 = mysql_query($updateSQL, $guitarswap_db) or die(mysql_error());

$updateGoTo = "Member2.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
    echo OK; // <-- This will cause a parse error
  }
  header(sprintf("Location: %s", $updateGoTo));

} else {

  //do something else

}

Note that your echo statement will cause a parse error. OK is a string, and should be in quotes. Also, it will prevent the redirect from working.