I’m attempting to create an HTML <select> drop down menu that will add a “selected” tag in the <option> with PHP once the user hits Submit if there is a problem with the submitted results. What I’ve attempted to do so far is this:
$dayhrserr = " class=tderror"; // turns the select box red if there is an error
$dayhrs = $_POST['dayhours']; // will be preparing this in a statement once the root issue is fixed
$dhrsa = array('60', '120', '180', '240', '300');
$dhrsb = array('1 Hour', '2 Hours', '3 Hours', '4 Hours', '5 Hours');
$log_dayhours = '<select name=dayhours'.$dayhrserr.'><option value=></option>';
for($i=0; $i<count($dhrsa); $i++) {
$log_dayhours .= '<option value=' . ($dayhrs == $dhrsa[$i] ? 'selected="selected"' : '' ) . '>' . $dhrsb[$i] . '</option>';
}
$log_dayhours .= '</select>';
This creates the drop down menu, but it doesn’t actually populate any of the value= tags… they all show up as <option value=>1 Hour</option> for example. I can’t figure out how to edit this to make those value= sections be populated by the data in the first array - one thing I’ve tried is to add the $dhrsa into the for loop after the $log_dayhours .= '<option value=' section, but that just gave me an array error.
Could someone give me a pointer on what I should do to correct this?
You don’t actually specify anything for the value, which is why it doesn’t come out. You have a ternary operator to either output selected="selected" or not, but you don’t output your array variable anywhere. You need to put a reference to $dhrsa[$i] into that line for the option value, just like you have for the text display.
Show us the code you used, someone will have a look at what might be wrong with it.
You don’t actually specify anything for the value, which is why it doesn’t come out.
Right - I realized shortly after I wrote all that code that the value section of the option isn’t being populated. So I attempted to fix it doing something like this:
Firstly, You’ve confused yourself with quotes - or rather, the lack thereof.
Secondly, your line is missing a .. Take a closer look towards the end of your string.
Thirdly, $dhrsa is an array. what value are you trying to give it?
It will do. You can’t just stick the entire $dhrsa array into each line. You need to specify which element of that array you want in the value, exactly as you do in the ternary operator just after it on the same line of code, and in the code just after it where you display the option text from the other array.
Thanks Droopsnoot and m_hutley - your advice kept me on the right track. Sorry this took so long to respond to, but I figured out where I was going wrong:
Well, actually, no - I don’t. This code works, it gives me the dropdown menu as expected, and will put the selected option in the option block for a previous selection. So I’m not sure how it could be the wrong code?
Also your ‘old’ code will still choke slightly, because your selected has no spacing between it and the value. You appear to have corrected it in the ACTUAL code.
Oh man - you’re right. My apologies there. That second bit in your post is what my code is now, yes, and that works. I guess I grabbed old code by mistake. The code I’m actually using is in my last post there.