stijn0713 Posted March 17, 2012 Share Posted March 17, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259119--/ Share on other sites More sharing options...
scootstah Posted March 17, 2012 Share Posted March 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259119--/#findComment-1328383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.