Jump to content

Recommended Posts

Hey guys i am new around here and i need some help with php.

 

So basically i want to make a submit form and i found a problem for myself

here's some images to understand what i am talking about.

 

so here's what i've made at first. This is meant for new data.

 

e5ijqt.jpg

 

<p><label>Status :</label>
                            <select name="status" >
                            	<option value=""></option>
                            	<option value="Ongoing">Ongoing</option>
                            	<option value="Completed">Completed</option>
                            </select>

 

basically what I want is when I edit the data, that selection returns the value that was stored in the database rather than just null value. See image below to understand what i am trying to do.

 

350ugrs.jpg

 

i dont really get how to do it with select.

 

Please help me and thanks before.

Sorry if my question was already asked before. Tried to search but didnt really found what i am looking for.

You will need server-side code to build the select list instead of hard-coding it.

 

<?php

$statusOptionsList = array('', 'Ongoing', 'Completed');

$statusOptionsHTML ='';
foreach($statusOptionsList as $option)
{
    $selected = ($userValueFromDB==$option) ? ' selected="selected"' : '';
    $statusOptionsHTML .= "<option value=\"{$option}\"{$selected}>{$option}</option>";
}

?>

<select name="status">
<?php echo $statusOptionsHTML; ?>
</select>

ah that's it "selected" thats new for me, didn't know that attributes exist

 

I've tried it and it works well thanks.

 

$selected = ($userValueFromDB==$option) ? ' selected="selected"' : '';

 

is that boolean expression the same right this ?

 

if ($userValueFromDB==$option)
                  $selected = "selected";
                  else
                  $selected = "";

 

sorry again for stupid question. I rarely see boolean expression used.

 

Anyway thanks for the help.

You are correct in how you think that statement is interpreted, but it is the comparison that is boolean (true or false) and it is the same comparison either way.

 

The term for the solution I provided is called the ternary operator. Many people refer to it as a shorthand if/esle statement, but that is an understatement in my opinion.

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.