Jump to content

Recommended Posts

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>

 

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'] : '';

 

  • Thanks 1

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

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>';
}

 

  • Like 1
  • Thanks 1
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>';
	}
}

 

  • Like 1
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.