Jump to content


Photo

MySQL, Forms, and the Select Form Option


  • Please log in to reply
8 replies to this topic

#1 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 05 June 2006 - 04:56 AM

$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>

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?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 June 2006 - 05:41 AM


$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>";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 06 June 2006 - 05:21 AM

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?

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 June 2006 - 05:27 AM

i don't understand how this ties into the months of the year, can you explain some more?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 06 June 2006 - 05:37 AM

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.

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 June 2006 - 05:48 PM

well, in your form, you would have your radio buttons:
<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>
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
//blah.php

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

value of yesorno is: true

if you selected the no radio button, it will display

value of yesorno is: false

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 07 June 2006 - 03:04 AM

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.

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 June 2006 - 04:07 AM

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

$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>


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 07 June 2006 - 07:09 AM

Awesome! It worked! Thank you very much for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users