Good day fellow programmers, please i need your help. I have a hidden field that holds both the destination and the id of the destination, i need help on how to send both the id and and value of the destination to the database

The code for the form is

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

<table width="489" height="141" align="center" cellpadding="3" cellspacing="3" bordercolor="#B1C3D9" class="textf1">
          <tr>
            <th height="37" colspan="2" bgcolor="#FFFFFF" scope="col"><table width="466" class="textff">
                <tr bordercolor="#75A8C7">
                  <td width="458" bgcolor="#990100" scope="col"><div align="center"><span class="style94">ADD DESTINATIONS </span>FROM</div></td>
                </tr>
            </table></th>
          </tr>
          <tr>
            <td width="258" height="5" scope="col"><div align="left"><span class="style103">Select Destination From: </span></div></td>
            <td width="206" scope="col"><label>
                <div align="left">
                  <label>
                  <select name="destinationfrom" id="destinationfrom">
				      <?php
                        $query = "SELECT * FROM destinationfrom ORDER BY destination ASC";
						$result = mysql_query($query) or die("Error ".mysql_errno().": ".mysql_error()."<br><br>$query");
						$index = 0;
						while($row = mysql_fetch_assoc($result)) {
							$index++;
							$var =$row['hiddendestination']; 
					   ?>
                <option value="<?php echo $row['id'] ?>" selected="selected"><?php echo $row['destination'] ?></option>
				
				 
				 <?php }   
				  ?>
              </select>
                  
                  </label>
                  <input name="hiddenid" type="hidden" id="hiddenid" value="<?php echo $row['hiddenid'] ?>" />
              
                  <input name="hiddendestinationfrom" type="hidden" id="hiddendestinationfrom" value="<?php echo $row['hiddendestination'] ?>" />
                </div>
              </label></td>
          </tr>
          <tr>
            <td height="7" scope="col"><div align="left"><span class="style103">Enter Destination To: </span></div></td>
            <td scope="col"><div align="left">
                <label></label>
                <label></label>
                <input name="destination" type="text" class="textf1" id="destination"/>
            </div></td>
          </tr>
          <tr>
            <th height="7" scope="col">&nbsp;</th>
            <td scope="col"><input name="Submit" type="submit" class="style92" value="Submit" /></td>
          </tr>
        </table>

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

The form action is done by transactform and the action is

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

<?php
include "function.php";
$error= array();
$destinationfrom=isset($_POST['destinationfrom'])? trim($_POST['destinationfrom']):'';
$destination=isset($_POST['destination'])? trim($_POST['destination']):'';
$var= "select * from destinationfrom where id=$destinationfrom";

if(empty($destination)){
$error[]=urlencode("Field Required!");
}
if(empty($error)){
$query= "insert into destinationto(destinationid,destinationfrom,destination) values('$destinationfrom','$var','$destination')";
}
else {
header('Location:adddestinationto.php?action=adddestinationto'.' &error='.join($error, urlencode('<br>')));
 }
 if(isset($query)){
 $msg="<strong> Destination-to was added successfully... </strong>";
 $result= mysql_query($query);
 header('Location:adddestinationto.php?action=adddestinationto'.' & error='.$msg);
 

}

?>

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Recommended Answers

All 4 Replies

If you were my fellow programmer you would use code tags for better readability. And you would boil down your code to the problem without having us to spell our way through lots of irrelevant HTML. Be a good fellow and try again.

commented: well put +3
commented: Was thinking the same thing +2

Building on smantscheff's response, you'd also have an informative subject line instead of a vague plea for assistance, so I would know if I can help before I click the link.

Member Avatar for diafol

Without wanting to post a 'me too' post, I'll just say that forms usually need to be sent to another page to deal with the submitted fields. Reloading a page with a form and form handling script can cause resubmission - which is bad. The form tag has a default method attribute of 'get'. You don't seem to have used form tags at all. When you do, use the method="post" so you can use $_POST array in the form handling script.

Anyway, to reiterate, use code tags, cut out the needless code and use a meaningful title.

Don't lie ardav, you wanted to write a "me too" post ;)
On a serious note, good points. I'll have to look into that about submitting forms to the same page, I never heard there could be a problem with that.

Some small details:
1. On line 21 of the first code segment you declare a variable $var, but never use it again in that section of code.

2. Note that on lines 31 & 33 you reference the $row array. I'm not sure about PHP's scope rules (don't have time to look them up this sec), but you're either a) accessing a variable that no longer exists, or b) creating 2 hidden fields whose values are based on the last row you retrieved from the database. Unless I'm missing something, I don't think that's the goal you're trying to achieve. You probably need to use JavaScript to populate these fields with different "hiddenid" and "hiddendestination" values based on what users select from the drop-down menu.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.