Jump to content

Recommended Posts

Hi!!!

 

I am trying to write an app which updates a database from the chosen person in a selectbox (clientdd) however I was having a problem whereby the page loads then when you press submit the form goes blank - you then press again and it processes the information you entered before. I decided it must have been my messy code so I decided to rewrite it again, I am now experiencing the problem again!!

 

Could somebody please help - I would like the database to update when you press submit - something dodgy is going on!!

 

The code is below:

<?php require_once('../Connections/timedb.php'); ?>
<?php
mysql_select_db($database_timedb, $timedb);
$clientcurr = $_POST['clientdd'];
if (!empty($_POST['clientdd'])) {
$query_custdb = "SELECT * FROM tblcustomer WHERE 'clientname' =  '". $clientcurr."'";
}
else { $query_custdb = "SELECT * FROM tblcustomer";  }
$custdb = mysql_query($query_custdb, $timedb) or die(mysql_error());
$row_custdb = mysql_fetch_assoc($custdb);
$totalRows_custdb = mysql_num_rows($custdb);

mysql_select_db($database_timedb, $timedb);
$query_jobdb = "SELECT * FROM tbljobs";
$jobdb = mysql_query($query_jobdb, $timedb) or die(mysql_error());
$row_jobdb = mysql_fetch_assoc($jobdb);
$totalRows_jobdb = mysql_num_rows($jobdb);
// TIME CONVERSION STUFF (A WORKING FUNCTION!!!!//////
function hourstodecimal ($timeinhours) {
  $timeparts = explode(':', $timeinhours);
  return ($timeparts[0] + ($timeparts[1]/60));
}

function decimaltohours ($timeindecimal) {
  $hours = floor($timeindecimal);
  $minutes = str_pad( (($timeindecimal - $hours)*60), 2, '0');
  return $hours.':'.$minutes;
}
///////////////////////////////////////////////////////
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="2">
    <tr>
      <td width="118">Client Name </td>
      <td width="168"> <select name="clientdd" id="clientdd" onchange=\"reload(this.form)\">
           <?php do { ?>
           <option value="<?php echo $row_custdb['clientname']?>" selected="<?php $row_custdb['clientname'] ?>"> <?php echo $row_custdb['clientname']?></option>
           <?php
} while ($row_custdb = mysql_fetch_assoc($custdb));
  $rows = mysql_num_rows($custdb);
  if($rows < 10) {
      mysql_data_seek($custdb, 0);
  $row_custdb = mysql_fetch_assoc($custdb);
  }
?>
                 </select></td>
    </tr>
    <tr>
      <td>Job Number </td>
      <td><label>
        <select name="select2">
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Hours Worked </td>
      <td><label>
        <input type="text" name="textfield" />
      </label></td>
    </tr>
    <tr>
      <td>Remaining Hours</td>
      <td><label>
        <input type="text" name="textfield2" />
      </label></td>
    </tr>
    <tr>
      <td colspan="2"><label>
        <input type="submit" name="Submit" value="Submit" />
      </label></td>
    </tr>
  </table>
</form>
</body>
</html>
<?php
mysql_free_result($custdb);

mysql_free_result($jobdb);
?>

Link to comment
https://forums.phpfreaks.com/topic/68260-having-to-press-submit-button-twice/
Share on other sites

That didnt seem to sort it neither.. May be worth noting that I have added another table called jobdb which fills jobnumdd dropdown and when I click submit that one doesnt dissappear when the others do, here is the code with jobnumdd code in if you want to look, however if you feel it is of no help the other code probably easier to look thru:

 

<?php require_once('../Connections/timedb.php'); ?>
<?php
//////////// UPDATE DB STUFF ///////////////
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_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE tblcustomer SET clientname=%s, hoursworked=%s, hoursleft=%s, hoursallocated=%s WHERE clientid=%s",
                       GetSQLValueString($_POST['clientname'], "text"),
                       GetSQLValueString($_POST['hoursworked'], "text"),
                       GetSQLValueString($_POST['hoursleft'], "text"),
                       GetSQLValueString($_POST['hoursallocated'], "text"),
                       GetSQLValueString($_POST['clientid'], "int"));

  mysql_select_db($database_timedb, $timedb);
  $Result1 = mysql_query($updateSQL, $timedb) or die(mysql_error());
  print 'Query Ran: '.$updateSQL.' Result: '.$Result1;
}

mysql_select_db($database_timedb, $timedb);
/////////////////////////////////////



// SELECT CORRECT RECORD FROM DB - DEPENDING ON USER IN DROPDOWN//////
if (!empty($_POST['clientdd'])) {
$query_custdb = "SELECT * FROM tblcustomer WHERE 'clientname' =  '". $_POST['clientdd']."'";
}
else { $query_custdb = "SELECT * FROM tblcustomer";  }
$custdb = mysql_query($query_custdb, $timedb) or die(mysql_error());
$row_custdb = mysql_fetch_assoc($custdb);
$totalRows_custdb = mysql_num_rows($custdb);
///////////////////////////////////////////////////////////////////////

// SELECT JOB NUMBERS FOR APPROPRIATE CLIENT SO TIME CAN BE LOGGED/////
mysql_select_db($database_timedb, $timedb);
$query_jobdb = "SELECT * FROM tbljobs";
$jobdb = mysql_query($query_jobdb, $timedb) or die(mysql_error());
$row_jobdb = mysql_fetch_assoc($jobdb);
$totalRows_jobdb = mysql_num_rows($jobdb);
////////////////////////////////////////////////////////////////////////

// WORK OUT TIME LEFT FOR CURRENTLY SELECTED USER 
$hoursremaining = decimaltohours ($row_custdb['hoursallocated'] - $row_custdb['hoursworked']);


// TIME CONVERSION STUFF (A WORKING FUNCTION!!!!//////
function hourstodecimal ($timeinhours) {
  $timeparts = explode(':', $timeinhours);
  return ($timeparts[0] + ($timeparts[1]/60));
}

function decimaltohours ($timeindecimal) {
  $hours = floor($timeindecimal);
  $minutes = str_pad( (($timeindecimal - $hours)*60), 2, '0');
  return $hours.':'.$minutes;
}
//////////////////////////////////////////////////////////////////////
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="<?php echo $editFormAction; ?>">
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="2">
    <tr>
      <td width="118">Client Name </td>
      <td width="168"> 
  
  <select name="clientdd" id="clientdd">
      <?php do { ?>
      <option value="<?php echo $row_custdb['clientname']?>" selected="<?php $row_custdb['clientname'] ?>"> <?php echo $row_custdb['clientname']?></option>
      <?php
} while ($row_custdb = mysql_fetch_assoc($custdb));
  $rows = mysql_num_rows($custdb);
  if($rows < 10) {
      mysql_data_seek($custdb, 0);
  $row_custdb = mysql_fetch_assoc($custdb);
  }
?>
      </select></td>
    </tr>
    <tr>
      <td>Job Number </td>
      <td><label>
     <select name="jobnumdd" id="jobnumdd">
           <?php do { ?>
           <option value="<?php echo $row_jobdb['jobnumber']?>" selected="<?php $row_jobdb['jobnumber'] ?>"> <?php echo $row_jobdb['jobnumber']?></option>
           <?php
} while ($row_jobdb = mysql_fetch_assoc($jobdb));
  $rows = mysql_num_rows($jobdb);
  if($rows < 10) {
      mysql_data_seek($jobdb, 0);
  $row_jobdb = mysql_fetch_assoc($jobdb);
  }
?>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Hours Worked </td>
      <td><label>
        <input name="flhourstoday" type="text" id="flhourstoday" value="0:00" />
      </label></td>
    </tr>
    <tr>
      <td>Remaining Hours</td>
      <td><label>
        <input name="flremhours" type="text" id="flremhours" value="<?php echo $hoursremaining; ?>">
      </label></td>
    </tr>
    <tr>
      <td colspan="2"><label>
        <input type="submit" name="Submit" value="Submit" />
      </label></td>
    </tr>
  </table>
  <p>
    <input type="hidden" name="MM_update" value="form1" />
    <input type="hidden" name="clientid" value="<?php echo $row_custdb['clientid']; ?>" />
    <input type="hidden" name="clientname" value="<?php echo $_POST['clientdd']; ?>" />
    <input type="hidden" name="hoursleft" value="<?php echo $row_custdb['hoursleft']; ?>" />
    <input type="hidden" name="hoursallocated" value="<?php echo $row_custdb['hoursallocated']; ?>" />
    <input type="hidden" name="hoursworked" value="<?php echo $row_custdb['hoursworked'] + hourstodecimal($_POST['flhourstoday']); ?>" />
  </p>
</form>
</body>
</html>
<?php
mysql_free_result($custdb);

mysql_free_result($jobdb);
?>

  <option value="<?php echo $row_custdb['clientname']?>"> 
  <?php echo $row_custdb['clientname']?>
  </option>

I have a sneaky feeling that it may be something to do with this bit of code here as if I remove it the field dont go empty however, I do need to somehow fill the selectbox.

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.