Page 1 of 1

Best wey to pass data selected from an option tag

Posted: Thu May 06, 2010 9:24 am
by ryan9979
I am looking for some advise on the best way to pass a piece of data to a second page that comes from a option group.
I think an example will explain better than i can.

Code: Select all

<form name="alter" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form">Choose a name to delete. <select name="delete">
<?php
    $user_id = $_SESSION['user_id'];
    $result = mysql_query("SELECT * FROM contact_name WHERE user_id = '$user_id'");
    while ($row = mysql_fetch_array($result)) {
    echo "<option>" . $row['fname'] . " " . $row['lname'] . "</option>";
    } ?>
    	</select>
    	</div>
       </form>
        </div>
        <div class="form"><input name="submit" type="submit" value="Delete" /><input name="reset" type="reset" value="Reset" /></div>
        </form>
when the user submits the form i want to be able to pass the value

Code: Select all

$row['contact_id']
for the selected name but not show it on the form. Any thoughts?

Thanks in advance.

Re: Best wey to pass data selected from an option tag

Posted: Thu May 06, 2010 10:17 am
by flying_circus
Something like this?

Code: Select all

<?php
  $contact_id = isset($_POST['delete']) ? $_POST['delete'] : '';
?>

<form name="alter" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <div class="form">Choose a name to delete.
    <select name="delete">
    <?php
      $user_id = mysql_real_escape_string($_SESSION['user_id']);
      $result = mysql_query("SELECT * FROM `contact_name` WHERE `user_id` = '$user_id';");
      
      while ($row = mysql_fetch_array($result))
        echo "<option value=\"{$row['contact_id']}\">{$row['fname']} {$row['lname']}</option>";
    ?>
    </select>
  </div>
  <div class="form">
    <input name="submit" type="submit" value="Delete" />
    <input name="reset" type="reset" value="Reset" />
  </div>
</form>