Jump to content

MySQL, Forms, and the Select Form Option


yarub

Recommended Posts

[code]$query = mysql_query("select * FROM table WHERE id='$id'");
$row = mysql_fetch_array($query);

<select name='month'>
  <option value='Jan'>January
  <option value='Feb'>February
  <option value='Mar'>March
  <option value='Apr'>April
  <option value='May'>May
  <option value='Jun'>June
  <option value='Jul'>July
  <option value='Aug'>August
  <option value='Sep'>September
  <option value='Oct'>October
  <option value='Nov'>November
  <option value='Dec'>December
</select>[/code]

How can I pull what the user has from my database? With the text type you just use value='$whatever'. With textarea you just put it after the first tag and before the close tag. How can I do it with this?
Link to comment
Share on other sites

[code]

$months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

$query = mysql_query("select * FROM table WHERE id='$id'");
$row = mysql_fetch_array($query);

echo "<select name='month'>";

foreach ($months as $val) {
   echo "<option value='$val'";
   if ($val==$row['month']) { echo " selected"; }
   echo ">".$val;
}
echo "</select>";
[/code]
Link to comment
Share on other sites

Perfect. Thank you very much. Got another question. I'm now trying to do it with a radio button, but I'm trying to do it a bit differently. Here's basically what I want it to look like...

O Yes
O No

Yes is what displays to the person, but I want "true" to be the value. For no, I want "false" to be the value. Are you able to do that as well?
Link to comment
Share on other sites

Well, the months and years thing has been solved. I got that working. Now I'm working with something else. I'm just using a forum where yes sends a value of true and no sends a value of false. However, I'm having problems getting it to work right as I can't get it to pull the current value that is set and be able to submit a new value if it's changed.
Link to comment
Share on other sites

well, in your form, you would have your radio buttons:
[code]
<form action='blah.php' method='post'>
   <input type="radio" name='yesorno' value='true'>yes
   <input type="radio" name='yesorno' value='false'>no
   <input type="submit">
</form>
[/code]
notice how both radio buttons have the same name, but different values. when you click the submit button, it goes to blah.php and you now have the variable $_POST['yesorno'] to play with
[code]
//blah.php

if($_POST['yesorno']) {
   echo "value of yesorno is: ". $_POST['yesorno'];
}
[/code]
if you selected the yes radio button, it will display

[b]value of yesorno is: true[/b]

if you selected the no radio button, it will display

[b]value of yesorno is: false[/b]
Link to comment
Share on other sites

That part I understand. I'm asking how to do it the same way as the select form. I want to display it so that I can edit it at the same time if I need to. I want to make it so I can pull what the user has from the database into an administration panel, and be able to keep it as YES/NO or change it to YES/NO and then submit it. Does that make sense? Like the way you did it with the select one.
Link to comment
Share on other sites

well, here's one way to do it....

[code]
$query = mysql_query("select * FROM table WHERE id='$id'");
$row = mysql_fetch_array($query);

//assuming that the column that holds the true/false value is called 'boolval'
if ($row['boolval']=='true') {
   $truechecked = " checked";
   $falsechecked = "";
} else {
   $falsechecked = " checked";
   $truechecked = "";
}

//<form ...>
  echo"<input type='radio' name='yesorno' value='true' $truechecked>yes";
  echo"<input type="radio" name='yesorno' value='false' $falsechecked>no";
//</form>
[/code]

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.