Jump to content

Archived

This topic is now archived and is closed to further replies.

yarub

MySQL, Forms, and the Select Form Option

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?

Share this post


Link to post
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]

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
i don't understand how this ties into the months of the year, can you explain some more?

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Awesome! It worked! Thank you very much for your help.

Share this post


Link to post
Share on other sites

×

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.