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
https://forums.phpfreaks.com/topic/259119--/
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
https://forums.phpfreaks.com/topic/259119--/#findComment-1328383
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.