Jump to content

Drop Down


mattichu

Recommended Posts

heyy

 

I have a form which validates by posting the form to itself so that when the page reloads the values the user input are posted back into the text boxes.

 

How do I make it so that the posted drop down selection is the selected item on the reload??

 

:)

Link to comment
Share on other sites

How is posting the form to itself validation?

 

He didn't mean that the act of posting a form to itself is validation. But, he is posting the form back to itself because that is where he put the validation logic. This is a common practice so that when validation fails you can easily redisplay the form and, like in this case, make the form values "sticky" so the user's previous input will populate the fields. This is so the user doesn't have to enter all of the data back into a form because one field was invalid.

 

@mattichu

 

I typically create a function to create my select lists that takes an array of value/labels and an optional selected value. Here is an example working script

<?php

function createSelect($options, $selectedValue = false)
{
   $optionsHTML = '';
   foreach($options as $value => $label)
   {
    if($selectedValue!==false)
    {
	    $selected = ($selectedValue == $value) ? ' selected="selected"' : '';
    }
    $optionsHTML .= "<option value='{$value}'{$selected}>{$label}</option>\n";
   }
   return $optionsHTML;
}

//Options array. This can be built from a DB query
$colorOptions = array(
   0 => 'Red',
   1 => 'Blue',
   2 => 'Green',
   3 => 'Yellow',
   4 => 'Brown'
);

$selectedColor = isset($_POST['color']) ? $_POST['color'] : false;
$colorOptions = createSelect($colorOptions, $selectedColor);

?>
<html>
<body>
<?php print_r($_POST); ?>
<form action="" method="post">
Select a color:
<select name="color">
 <option>-- Select One --</option>
 <?php echo $colorOptions; ?>
</select>
<button type="submit">Submit</button>
</form>
</body>
</html>

Link to comment
Share on other sites

He didn't mean that the act of posting a form to itself is validation. But, he is posting the form back to itself because that is where he put the validation logic. This is a common practice so that when validation fails you can easily redisplay the form and, like in this case, make the form values "sticky" so the user's previous input will populate the fields. This is so the user doesn't have to enter all of the data back into a form because one field was invalid.

 

The reason I asked that question was to make sure that the OP is validating the user input correctly.

Link to comment
Share on other sites

The reason I asked that question was to make sure that the OP is validating the user input correctly.

 

Still have no idea what you mean. What "page" a user points to in order to perform the validation has no bearing on the actual validation - or the topic being discussed. And his query seemed perfectly self explanatory to me.

Link to comment
Share on other sites

Still have no idea what you mean. What "page" a user points to in order to perform the validation has no bearing on the actual validation - or the topic being discussed. And his query seemed perfectly self explanatory to me.

 

Okay let me start from the beginning since you are not understanding basic language. In the original post, all the OP states is that he/she is validating a form by posting it to itself. Sticky input values alone are not validation. I would like the OP to post the code he is using to validate the form so I may check it, since his post was extremely brief and misled me as to how he is actually validating the form.

Link to comment
Share on other sites

I would like the OP to post the code he is using to validate the form so I may check it, since his post was extremely brief and misled me as to how he is actually validating the form.

 

This topic isn't about validation, it's about dropdowns. Next you'll be expanding it into a debate about how inefficient code contributes to global warming. Now there's a thought.

Link to comment
Share on other sites

This topic isn't about validation, it's about dropdowns. Next you'll be expanding it into a debate about how inefficient code contributes to global warming. Now there's a thought.

 

My first post answered his question. I was trying to help him further. Apparently that is frowned upon here.

Link to comment
Share on other sites

Okay let me start from the beginning since you are not understanding basic language. In the original post, all the OP states is that he/she is validating a form by posting it to itself. Sticky input values alone are not validation. I would like the OP to post the code he is using to validate the form so I may check it, since his post was extremely brief and misled me as to how he is actually validating the form.

 

Let's look at the OP's post and break it down. Besides the greeting and the closing smiley there were two sentences in his post.

 

Here is the first sentence

I have a form which validates by posting the form to itself so that when the page reloads the values the user input are posted back into the text boxes.

That ends in a period. That is a statement, not a question. It could be information that is pertinent to his problem to come later or perhaps he was just proud of the fact that he built that and wanted us to be aware. But it is not a question asking for a response

 

Here is the 2nd sentence

How do I make it so that the posted drop down selection is the selected item on the reload??

Now that sentence is followed by a question mark (also called an interrogation point). Now, that is a question. I don't read anything in that question about validation. But, if you go back to the first sentence it all makes sense. He is posting the form to itself so he can do the validation and repopulate the form if validation fails. But, he is having problems figuring out how to repopulate/reset the select list to the value the user had in their form submission.

 

Now, let's look at your first response that you say answers his question

By using the selected attribute with the <option> tag. How is posting the form to itself validation?

Considering that this is the PHP forum and not the HTML forum, I think that is not an appropriate answer. I believe it is obvious the was asking about the 'logic' needed to determine how to set that parameter. but, if the OP was asking about how you set an option as selected, then he posted in the wrong forum (which I don't believe he did)

Edited by Psycho
Link to comment
Share on other sites

Psycho, enough with the little jabs please. I respect you, which is why I stopped the banter.

 

 

This really isn't the appropriate medium to address this, but I can't let that stand.

 

I never made a disparaging comment towards you in this thread. The "little jabs" you are referring to are your own words that you used towards me. If you are taking offense at those words being used, then perhaps you should not have used them towards someone. You were wrong from the very start of this thread and at every step along the way. Yet, you decided to make a disparaging remark towards me about my ability to understand "basic language". Rather, you were the one that didn't understand the "basic language" in the request which everyone else seemed to understand. Then, after all the defense of your position, when it is obviously wrong, you don't have the decency to at least say "oh yeah I goofed that one" or provide an apology, yet you have the audacity to say you respect me.

Link to comment
Share on other sites

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.