Jump to content

Is it possible to create a bound column


php_beginner_83

Recommended Posts

Hi All

 

On my webpage I want to use a pull down menu.  This menu I want to be populated with values from a table in my mysql database.  However, what I want to do is have my pull down menu have a bound column like in Microsoft Access.  So for example, say I have two sets of values from the table in my database, ID and NAME.  In my pull down menu, I want to display the NAME values but actually want to use the ID value associated with the NAME value.  It's the ID value I need but if I display this in the pull down menu, the user will have no clue what it means.

Does anyone have any suggestions or advice about how this can be done??

 

Thank you.

Link to comment
Share on other sites

<?php
  $sql = 'SELECT id,name FROM someTable';
  $result = mysql_query($sql) or die(mysql_error());
  echo '<select name="some_field">';
  while($row = mysql_fetch_assoc($result)){
    printf('<option value="%s">%s</option>',htmlspecialchars($row['id']),htmlspecialchars($row['name']));
  }
  echo '</select>';
?>

Link to comment
Share on other sites

Will that display the NAME values in the pull down menu but actually give me the ID value?

 

I want to put this in a <form action='upload.php' method='post'>....</form> and when I press the submit button all values from the form can be used to add a record to a database.  So even though the NAME value is displayed, the ID value is what will be passed to my upload.php code for further processing.

 

Thanks for your help.

Link to comment
Share on other sites

Why not try it and see?  Part of programming is typing code, running code, and examining the output.

 

You took the time to type out a well-thought question.

 

rhodesa took the time to type out a well-thought response.

 

You then immediately come back and ask him if it will do what you asked in the original post.  This implies that:

1) You did not try it at all

2) You don't think rhodesa has the reading comprehension to understand your original post

3) (or maybe) You think your OP wasn't that clear after all.

Link to comment
Share on other sites

Yes, the ID will get passed as the value. If you want to test it, put this at the top of the PHP page it's submitting to:

<?php
  print '<pre>'.print_r($_POST).'</pre>';
  exit;
?>

That will print the array of all the data being submitted by the form

Link to comment
Share on other sites

Like I said roopurt if I knew what I was doing I wouldn't be asking for help.  Perhaps my screen name would of given it away....php_BEGINNER_83.

I'm glad however to see that your idea of posting in a forum is to berate and belittle someone who is genuinely trying to get help to solve a problem.

 

rhodesa, thanks for the help.  I'm trying to learn php on my own so I really appreciate it when more experienced programmers take the time to constructively offer solutions.

Link to comment
Share on other sites

If you want to know what is being output by PHP it's not sufficient to examine the output as rendered by a browser.  You need to use the 'View source' feature of whichever browser you're using to view the actual HTML source PHP sent to the browser.

 

Had you done that, you would have seen:

<select name="some_field">
  <option value="1">Joe</option>
  <option value="2">Larry</option>
  <option value="3">John</option>
  <option value="4">Betty</option>
</select>

 

Now if you don't understand what that output means, there's a fine XHTML reference over at w3schools:

http://www.w3schools.com/tags/default.asp

http://www.w3schools.com/tags/tag_select.asp

http://www.w3schools.com/tags/tag_option.asp

 

If you understand that the text within value="" is what is sent back to PHP when the form is submitted, then it's pretty clear IDs are sent back while NAMEs are displayed.

 

The intent of my posts was not to offend you, but to let you know there was more you could have done to check if the solution was correct yourself.  Using PHP to create HTML that will then be rendered in a browser is a pretty core concept to the language; so is using the View source feature of the browser to examine the output.  Now since you were articulate and asking a question most likely related to forms, I had assumed you knew how to do those things.  Heck, you were even asking about a bound control, which is what other languages sometimes call such a feature. 

 

Anyways I was wrong and my apologies for offending you.

Link to comment
Share on other sites

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.