laflair13 Posted February 27, 2015 Share Posted February 27, 2015 First off, I hope I explain this right. I am wanting to show fields if only the checkbox next to it is checked. I have it set up that the fields will not show if the variable is empty, but I would like for it not to show unless the checkbox is checked if there is a variable. Here is an image of what is in the admin Here is the code I have for the variable to be shown on the site <?php if (isset($manufacture) && !empty($manufacture)) : ?> <li class="element element-text"> <strong>Manufacture: </strong> <?php echo $manufacture;?> </li> <?php endif; ?> <?php if (isset($model) && !empty($model)) : ?> <li class="element element-text"> <strong>Model #: </strong> <?php echo $model;?> </li> <?php endif; ?> And here is the code I am using when I put the item in the admin section <div class="form-group"> <label class="col-md-3">Manufacture</label> <div class="col-md-8"> <input type="text" name="manufacture" value="" class="form-control" /> </div> <!-- /.col --> <input type="checkbox" name="manufactureCheck"><span style="float:right; font-size: 10px; margin-top: 4px">Check to show</span> </div> <!-- /.form-group --> <div class="form-group"> <label class="col-md-3">Model</label> <div class="col-md-8"> <input type="text" name="model" value="" class="form-control" /> </div> <!-- /.col --> <input type="checkbox" name="modelCheck"> </div> <!-- /.form-group --> Also, I know I will have to add if the box is checked to the database. Could I just use one row named "Checked"? As for the code to make it show, couldn't I use something like this? <?php if ( isset($manufacture) && !empty($manufacture) && !isset($manufactureCheck) && !empty($manufactureCheck)) : ?> <li class="element element-text"> <strong>Manufacture: </strong> <?php echo $manufacture;?> </li> <?php endif; ?> Sorry if I didn't explain this good enough, I am very new to php. Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/ Share on other sites More sharing options...
Psycho Posted February 27, 2015 Share Posted February 27, 2015 (edited) OK, I *think* I know what you are trying to accomplish. You have in essence two forms. One for the admin and one for the user. You want the admin to be able to edit the values and/or determine if the field is displayed to the user. Try out this working example <?php session_start(); //Set values passed via post //This demo uses session vaues instead of a DB if($_SERVER['REQUEST_METHOD']=="POST") { //Set values in session vars (simulate doing an UPDATE query) $_SESSION['manufacture'] = trim($_POST['manufacture']); $_SESSION['manufactureCheck'] = isset($_POST['manufactureCheck']) ? 1 : 0; $_SESSION['model'] = trim($_POST['model']); $_SESSION['modelCheck'] = isset($_POST['modelCheck']) ? 1 : 0; } //Get the 'saved' values (simulate a SELECT query) $manufacturerValue = $_SESSION['manufacture']; $manufacturerCheck = $_SESSION['manufactureCheck']; $modelValue = $_SESSION['model']; $modelCheck = $_SESSION['modelCheck']; //Determine if fields should be displayed based on whether they //have a value and if the corresponding checkbox is checked $showManufacturer = ($manufacturerCheck & !empty($manufacturerValue)); $showModel = ($modelCheck & !empty($modelValue)); ?> <html> <head> </head> <body> <b>Admin Form</b> <form action="" method="POST"> <div class="form-group"> <label class="col-md-3">Manufacture</label> <div class="col-md-8"> <input type="text" name="manufacture" value="<?php echo $manufacturerValue; ?>" class="form-control" /> </div> <!-- /.col --> <input type="checkbox" name="manufactureCheck" value="1" <?php echo ($manufacturerCheck) ? ' checked="checked"' : ''; ?>> <span style="">Check to show</span> </div> <!-- /.form-group --> <div class="form-group"> <label class="col-md-3">Model</label> <div class="col-md-8"> <input type="text" name="model" value="<?php echo $modelValue; ?>" class="form-control" /> </div> <!-- /.col --> <input type="checkbox" name="modelCheck" value="1" <?php echo ($modelCheck) ? ' checked="checked"' : ''; ?>> <span style="">Check to show</span> </div> <!-- /.form-group --> <button type="submit">Submit</button> </form> <br><br><br><br> <b>User Form</b> <form action="" method="POST"> <?php if($showManufacturer) { ?> <div class="form-group" style="display: <?php echo $manufacturerStyle; ?>;"> <label class="col-md-3">Manufacture</label> <div class="col-md-8"> <input type="text" name="manufacture" value="<?php echo $manufacturerValue; ?>" class="form-control" class="form-control" /> </div> <!-- /.col --> </div> <!-- /.form-group --> <?php } ?> <?php if($showModel) { ?> <div class="form-group" style="display: <?php echo $modelStyle; ?>;"> <label class="col-md-3">Model</label> <div class="col-md-8"> <input type="text" name="model" value="<?php echo $modelValue; ?>" class="form-control" /> </div> <!-- /.col --> </div> <!-- /.form-group --> <?php } ?> </form> </body> </html> Edited February 27, 2015 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1506955 Share on other sites More sharing options...
laflair13 Posted February 27, 2015 Author Share Posted February 27, 2015 Must have explained it wrong. Right now I have it to where if the fields are blank they will not display on front end. I am trying to have the fields not display even though there is info, but the box is not checked. I just want it displayed if the box is checked. So to simplify what I need: Checkbox checked = Show on frontend Checkbox NOT Checked = Don't show Field empty = Don't show ( Don't think I would need this if the checkbox is determining if displayed or not ) Heres an image example that might help you understand better. (This is the fields in the admin when I add an item) The code below has it to where the info will not show on user side because it is empty. <?php if (isset($manufacture) && !empty($manufacture)) : ?> <li class="element element-text"> <strong>Manufacture: </strong> <?php echo $manufacture;?> </li> <?php endif; ?> <?php if (isset($model) && !empty($model)) : ?> <li class="element element-text"> <strong>Model #: </strong> <?php echo $model;?> </li> <?php endif; ?> Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1506962 Share on other sites More sharing options...
Psycho Posted February 27, 2015 Share Posted February 27, 2015 Did you even try the code I provided? You state Checkbox checked = Show on frontendCheckbox NOT Checked = Don't show Field empty = Don't show ( Don't think I would need this if the checkbox is determining if displayed or not ) The statement of "show on frontend" would seem to indicate that the checkbox is not displayed to the user, but for some sort of administrator. If that is the case, then I think the code I provided illustrates that functionality. If that is not the case, then what is your expectation of the user experience? If the field it empty and the checkbox is unchecked, what happens when the user checks the checkbox? There's no value, so the field would still not display based on your requirements. And, if your expectation is that the end user will be checking/unchecking this field then you want JavaScript - not PHP. Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1506977 Share on other sites More sharing options...
laflair13 Posted February 27, 2015 Author Share Posted February 27, 2015 The end user wouldn't see the checkboxes, they would only see the variable if the checkbox is checked in the admin side. Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1506978 Share on other sites More sharing options...
Psycho Posted February 27, 2015 Share Posted February 27, 2015 The end user wouldn't see the checkboxes, they would only see the variable if the checkbox is checked in the admin side. Then I will ask again, did you even try the code I provided? It does exactly what you are asking. The page has two forms - one simulates what the administrator will see and the other simulates what the user would see. Make changes to the admin form and submit the page and you will then see how those changes are reflected in the user form. Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1506984 Share on other sites More sharing options...
laflair13 Posted March 3, 2015 Author Share Posted March 3, 2015 I tried your suggestion and it didn't work. I think it is because what the user sees is not a form, the variable of the box is echoed on the frontend if the box is checked Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1507322 Share on other sites More sharing options...
Psycho Posted March 3, 2015 Share Posted March 3, 2015 I tried your suggestion and it didn't work. I think it is because what the user sees is not a form, the variable of the box is echoed on the frontend if the box is checked Then, put the value in a span or div and set the display accordingly Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1507329 Share on other sites More sharing options...
laflair13 Posted March 3, 2015 Author Share Posted March 3, 2015 You mean something like this? <div style="display:<?= ($offer === 1) ? 'block' : 'none'; ?>"></div> What about using an islet like ($isChecked && isset($model) && !empty($model)) with a corresponding checkbox. I did a search but do not understand how I can have a checkbox with the same name as the textfield. (like below) <input type="text" name="model" value="" class="form-control" /> <input type="checkbox" name="model"> Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1507355 Share on other sites More sharing options...
laflair13 Posted March 5, 2015 Author Share Posted March 5, 2015 Can anyone else give some advice on this? Quote Link to comment https://forums.phpfreaks.com/topic/294941-having-fields-show-if-only-checkbox-is-checked/#findComment-1507652 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.