Best wey to pass data selected from an option tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryan9979
Forum Newbie
Posts: 3
Joined: Wed May 05, 2010 10:48 pm

Best wey to pass data selected from an option tag

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

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

Post 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>
Post Reply