Jump to content

<?php if ($_POST['inputfieldID'] == "value") { echo "selected"; } ?>


stijn0713

Recommended Posts

hello,

 

I was wondering what is the point of writing

 

for a dropdown: <?php if ($_POST[inputfieldID] == "value") { echo "selected"; } ?>

 

or for a checkbox/ radio button: <?php if ($_POST['cb/radioID'] == "value") { echo "checked"; } ?>

 

I guess it's for after submit, if there is any error, the values filled will still be echoed in the inputfield, or am i wrong?

 

But then if that is true, i have a php search box where i use AJAX object to to send all the input they filled in after submit. But after pushing submit the inputted values remain.

 

I guess when using AJAX to send the information i don't need  <?php if ($_POST[inputfieldID] == "value") { echo "selected"; } ?> in my html forms?

 

 

Little confused :) Can maybe somebody bring clearness :)

 

 

 

Link to comment
Share on other sites

for a dropdown: <?php if ($_POST[inputfieldID] == "value") { echo "selected"; } ?>

 

or for a checkbox/ radio button: <?php if ($_POST['cb/radioID'] == "value") { echo "checked"; } ?>

 

I guess it's for after submit, if there is any error, the values filled will still be echoed in the inputfield, or am i wrong?

 

Yes, that's correct. Although the syntax is not. For a dropdown box you'll have to check each individual option. Also, it should be 'selected="selected"' or 'checked="checked"'.

 

Here's an example:

<select name="dropdown">
<option value="1" <?php if($_POST['dropdown'] == 1) { echo 'selected="selected"'; } ?>>One</option>
<option value="2" <?php if($_POST['dropdown'] == 2) { echo 'selected="selected"'; } ?>>Two</option>
<option value="3" <?php if($_POST['dropdown'] == 3) { echo 'selected="selected"'; } ?>>Three</option>
</select>

<input type="checkbox" name="chk" <?php if(isset($_POST['chk'])) { echo 'checked="checked"'; } ?> />

 

You can use the ternary operator to make it a little cleaner.

<select name="dropdown">
<option value="1" <?php echo $_POST['dropdown'] == 1 ? 'selected="selected" : ''; ?>>One</option>
<option value="2" <?php echo $_POST['dropdown'] == 2 ? 'selected="selected" : ''; ?>>Two</option>
<option value="3" <?php echo $_POST['dropdown'] == 3 ? 'selected="selected" : ''; ?>>Three</option>
</select>

<input type="checkbox" name="chk" <?php echo isset($_POST['chk']) ? 'checked="checked"' : ''; ?> />

 

But then if that is true, i have a php search box where i use AJAX object to to send all the input they filled in after submit. But after pushing submit the inputted values remain.

 

I guess when using AJAX to send the information i don't need  <?php if ($_POST[inputfieldID] == "value") { echo "selected"; } ?> in my html forms?

 

Since AJAX doesn't refresh the page, you don't have to worry about it.

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.