ttnaddy16 Posted July 12, 2018 Share Posted July 12, 2018 My php code runs good but I don't want unchecked checkboxs to show up as undefined variables. Any ideas? Website Code- <!DOCTYPE html> <html> <body> <hr> <h2>Computer Repair Form</h2> </div> <div> <div> <form> <form action="submiiresults.php" method="post"> <div> <input type="text" name="firstname" placeholder="First Name" required/> </div> <div> <input type="text" name="lastname" placeholder="Last Name" required/> </div> <div> <input type="text" name="email" placeholder="Email" required/> </div> <div> <input type="text" name="subject" placeholder="Subject" required/> </div> <div> <div> <input type="checkbox" name="harddrivecrashed" value="Hard Drive Crashed"> <label>Hard Drive Crashed</label><br> <input type="checkbox" name="hasvirus" value="Has Virus"> <label>Has Virus</label><br> <input type="checkbox" name="needsoperatingsystem" value="Needs Operating System"> <label>Needs Operating System</label> <br><br> </div> <div> <input type="checkbox" name="needsmicrosoftoffice" value="Needs Microsoft Office"> <label>Needs Microsoft Office</label><br> <input type="checkbox" name="interestedinbackupservices" value="Interested In Backup Services"> <label>Interested In Backup Services</label><br> <input type="checkbox" name="wantsacustompcbuild" value="Wants A Custom PC Build"> <label>Wants A Custom PC Build</label> <br><br> <input type="submit" name="submit"> <input type="reset" value="Reset"> </form> <br><br> </div> </div> </form> </div> </body> </html> <!DOCTYPE html> <html> <body> <?php // Check if the form is submitted if ( isset( $_POST['submit'] ) ) { echo '<h2>Your Data Submitted!<h2/>'; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $subject = $_POST['subject']; $harddrivecrashed = $_POST['harddrivecrashed']; $hasvirus = $_POST['hasvirus']; $needsoperatingsystem = $_POST['needsoperatingsystem']; $needsmicrosoftoffice = $_POST['needsmicrosoftoffice']; $interestedinbackupservices = $_POST['interestedinbackupservices']; $wantsacustompcbuild = $_POST['wantsacustompcbuild']; // display the results //echo 'First Name:' . $firstname .' <br>Last Name:' . $lastname .' <br>Email Address:' .$email .' <br>Subject:' . $subject .' <br>Needs/Problems:<br>' . $harddrivecrashed .' <br>' . $hasvirus .' <br>' . $needsoperatingsystem .' <br>' . $needsmicrosoftoffice .' <br>' . $interestedinbackupservices .' <br>' . $wantsacustompcbuild; $formdata = fopen("formdata.txt", "a") or die("Unable to open file!"); $txt = "First Name:$firstname\r\n"; fwrite($formdata, $txt); $txt = "Last Name:$lastname\r\n"; fwrite($formdata, $txt); $txt = "Email Address:$email\r\n"; fwrite($formdata, $txt); $txt = "Subject:$subject\r\n"; fwrite($formdata, $txt); $txt = "Needs/Problems:$harddrivecrashed\r\n"; fwrite($formdata, $txt); $txt = "Needs/Problems:$hasvirus\r\n"; fwrite($formdata, $txt); $txt = "Needs/Problems:$needsoperatingsystem\r\n"; fwrite($formdata, $txt); $txt = "Needs/Problems:$needsmicrosoftoffice\r\n"; fwrite($formdata, $txt); $txt = "Needs/Problems:$interestedinbackupservices\r\n"; fwrite($formdata, $txt); $txt = "Needs/Problems:$wantsacustompcbuild\r\n"; fwrite($formdata, $txt); $txt = "\r\n"; fwrite($formdata, $txt); $txt = "\r\n"; fclose($formdata); exit; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/307495-how-to-handle-form-variables-that-arent-used/ Share on other sites More sharing options...
cyberRobot Posted July 12, 2018 Share Posted July 12, 2018 The undefined variables warning is likely caused by lines like the following: $hasvirus = $_POST['hasvirus']; If the "hasvirus" option isn't checked, the corresponding POST variable won't exist. To address the issue, change it to $hasvirus = (isset($_POST['hasvirus'])) ? $_POST['hasvirus'] : ''; 1 Quote Link to comment https://forums.phpfreaks.com/topic/307495-how-to-handle-form-variables-that-arent-used/#findComment-1559641 Share on other sites More sharing options...
cyberRobot Posted July 12, 2018 Share Posted July 12, 2018 Side note: the <label> tags aren't being used correctly. To fix the issue, change this <input type="checkbox" name="hasvirus" value="Has Virus"> <label>Has Virus</label><br> To this <label><input type="checkbox" name="hasvirus" value="Has Virus"> Has Virus</label><br> Once done, you can make sure it's working by clicking the "Has Virus" label. The corresponding checkbox will become checked. More information about the <label> tag can be found here:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label Quote Link to comment https://forums.phpfreaks.com/topic/307495-how-to-handle-form-variables-that-arent-used/#findComment-1559642 Share on other sites More sharing options...
ttnaddy16 Posted July 12, 2018 Author Share Posted July 12, 2018 Thank you so much! Appreciate the help! Quote Link to comment https://forums.phpfreaks.com/topic/307495-how-to-handle-form-variables-that-arent-used/#findComment-1559643 Share on other sites More sharing options...
Barand Posted July 12, 2018 Share Posted July 12, 2018 I'd go a step further and give all the checkboxes the same name EG name='problem[]' so they are posted as an array <label><input type="checkbox" name="problem[]" value="Hard Drive Crashed"> Hard Drive Crashed</label><br> <label><input type="checkbox" name="problem[]" value="Has Virus"> Has Virus</label><br> <label><input type="checkbox" name="problem[]" value="Needs Operating System"> Needs Operating System</label><br> <label><input type="checkbox" name="problem[]" value="Needs Microsoft Office"> Needs Microsoft Office</label><br> <label><input type="checkbox" name="problem[]" value="Interested In Backup Services"> Interested In Backup Services</label><br> <label><input type="checkbox" name="problem[]" value="Wants A Custom PC Build"> Wants A Custom PC Build</label><br> In your processing foreach ($_POST['problem'] as $problem) { echo $problem . '<br>'; } 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/307495-how-to-handle-form-variables-that-arent-used/#findComment-1559650 Share on other sites More sharing options...
cyberRobot Posted July 13, 2018 Share Posted July 13, 2018 14 hours ago, Barand said: I'd go a step further and give all the checkboxes the same name EG name='problem[]' so they are posted as an array Great suggestion! Note that you'll want to make sure the user checked at least one option before running Barand's code. That can be accomplished with the following: if(isset($_POST['problem'])) { foreach ($_POST['problem'] as $problem) { echo $problem . '<br>'; } } 1 Quote Link to comment https://forums.phpfreaks.com/topic/307495-how-to-handle-form-variables-that-arent-used/#findComment-1559666 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.