HTML < select > menu with PHP "selected" option

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?

for me the basic data format is unpratical, i would recommend something like

$hours = [60 => '1 hour', 120 => '2 hours', ...];
foreach($hours as $minutes => $label){
  // create option tag

In this line:

$log_dayhours .= '<option value=' . ($dayhrs == $dhrsa[$i] ? 'selected="selected"' : '' ) . '>' . $dhrsb[$i] . '</option>';

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.

1 Like

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:

$log_dayhours .= '<option value=' . $dhrsa . ' ' . ($dayhrs == $dhrsa[$i] ? 'selected="selected"' : '' ) . '>' $dhrsb[$i] . '</option>';

That doesn’t work - it gives me an array error:

Array to string conversion in line...

I believe I’m on the right track (or I hope I am), but I can’t seem to quite make the full connection and make it work

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:

$log_dayhours .= '<option value=' . $dhrsa . ($dayhrs == $dhrsa[$i] ? 'selected="selected"' : '' ) . '>' . $dhrsb[$i] . '</option>';

Thanks so much!

You know that’s still the wrong code, yes? You’re still trying to include the entire array in that code.

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?

One of these two is an array reference. The other is not.

There is no way you have used this code line and it has generated valid HTML. Show us the HTML output of your code.

Well, here it is then - a picture of the code rendering in the browser, and the source:

and here’s the full code:

$dhrsa = array('0', '60', '120', '180', '240', '300');
$dhrsb = array(' ', '1 Hour', '2 Hours', '3 Hours', '4 Hours', '5 Hours');
$log_dayhours = '<select name=dayhours>';
for($i=0; $i<count($dhrsa); $i++) {
	$log_dayhours .= '<option value=' . $dhrsa[$i] . ($dayhrs == $dhrsa[$i] ? ' selected="selected"' : '' ) . '>' . $dhrsb[$i] . '</option>';
$log_dayhours .= '</select>';

It might not be correct code, but it works as I expected, so I’m not sure what to say.

Well i’ll point here:

What you told us your code is:
$log_dayhours .= ‘<option value=’ . $dhrsa . ($dayhrs == $dhrsa[$i] ? ‘selected=“selected”’ : ‘’ ) . ‘>’ . $dhrsb[$i] . ‘’;

What your code ACTUALLY is…
$log_dayhours .= ‘<option value=’ . $dhrsa[$i] . ($dayhrs == $dhrsa[$i] ? ’ selected=“selected”’ : ‘’ ) . ‘>’ . $dhrsb[$i] . ‘’;


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.

Sorry for the confusion!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.