简体   繁体   中英

The isset 'Yes' : 'No' aspect is not working correctly n my PHP form

I am attempting to produce an example combined 'Order Calculator / Order Form' in one php form. The 'CALCULATOR' aspects work perfectly for my needs at this point. All 5 order options generate an accurate total, both in the live form page, as well as in the receiving email. The 'FORM' aspects also work well, except... the three checkbox options 'nuts', 'sprinkles' and 'syrup' always produce a 'Yes' in the receiving email, even when they are left unchecked in the form.

I have tried several combinations to remedy this, but am at a loss. Any help would be greatly appreciated.

HTML

<!DOCTYPE html>

<html>

<head>

<title>Ice Cream Order Form</title>

<style> 

.plus { display: none; } 

</style>

</head>

<body>

<form action="icecream_formHandler.php" method="post" enctype="multipart/form-data">

<h1>Customer Ice Cream Order Form and Order Calculator</h1>

<p>In this example, the customer would enter their info., make their selections, get an order 
total and then place their order through the email form.</p><br>

<input type="text" id="customerName" name="customerName" placeholder=" Enter Your Name"><br> 
<br>

<input type="email" id="customerEmail" name="customerEmail" placeholder=" Enter Your Email"> 
<br><br>

<input type="tel" id="customerPhone" name="customerPhone" placeholder=" Enter Your Phone 
Number"><br><br>

<select id="flavor" name="flavor">

<option selected="true" disabled="disabled">Select Your Flavor</option> 
<option value="Vanilla">Vanilla</option> 
<option value="Chocolate">Chocolate</option>
<option value="Strawberry">Mixed</option>

</select><br><br>

<select id="scoopsSelector" name="scoopsSelector" onchange="calcuMath()">

<option value='0' selected="true" disabled="disabled">How Many Scoops?</option> 
<option value="3">One Scoop - $3.00</option> 
<option value="5">Two Scoops - $5.00</option>
<option value="7">Three Scoops - $7.00</option>

</select><br><br>

<!-- This hidden input uses it's population to pass the <select> text instead of the <select> 
value to the php form handler  -->

<input type="hidden" id="scoops" class="scoops" name="scoops">

<script>

const scoopsSelector = document.getElementById('scoopsSelector')
scoopsSelector.addEventListener('input', function() {
let selectedOptText = scoopsSelector.options[scoopsSelector.selectedIndex].text
document.querySelector('.scoops').value = selectedOptText;
})    

</script>

<p>Toppings: (Optional)</p>

<select id="plus" class="plus"><option value="add" id="add">+</option></select>

<input type="hidden" id="nuts" name="nuts" value="0" ><input type="checkbox" 
onchange="this.previousSibling.value=3-this.previousSibling.value; calcuMath();">&nbspNuts 
$3.00<br><br>

<select id="plus" class="plus"><option value="add" id="add">+</option></select>

<input type="hidden" id="sprinkles" name="sprinkles" value="0"><input type="checkbox" 
onchange="this.previousSibling.value=3-this.previousSibling.value; 
calcuMath();">&nbspSprinkles $3.00<br><br>

<select id="plus" class="plus"><option value="add" id="add">+</option></select>

<input type="hidden" id="syrup" name="syrup" value="0"><input type="checkbox" 
onchange="this.previousSibling.value=4-this.previousSibling.value; calcuMath();">&nbspSyrup 
$4.00<br><br><br>

<select id="plus" class="plus"><option value="add" id="add">+</option></select>

<select id="dishSelector" name="dishSelector" onchange="calcuMath()">

<option value='0' selected="true" disabled="disabled">Cup or Cone</option> 
<option value="3">Cup - $3.00</option>
<option value="4">Cone - $4.00</option> 

</select><br><br><br>

<!-- This hidden input uses it's population to pass the <select> text instead of the <select> 
value to the php form handler  -->

<input type="hidden" id="dish" class="dish" name="dish">

<script>

const dishSelector = document.getElementById('dishSelector')
dishSelector.addEventListener('input', function() {
let selectedOptText = dishSelector.options[dishSelector.selectedIndex].text
document.querySelector('.dish').value = selectedOptText;
})    

</script>

TOTAL: $<a id="result"></a><br><br>

<script>

function calcuMath() {
var z;
var a = document.getElementById("scoopsSelector").value;
var b = document.getElementById("nuts").value;
var c = document.getElementById("sprinkles").value;
var d = document.getElementById("syrup").value;
var e = document.getElementById("dishSelector").value;
var sel = document.getElementById("plus");
var selection = sel.options[sel.selectedIndex].value;
var more = document.getElementById("add");
if (selection == "add") 
{ z = parseFloat(a) + parseFloat(b) + parseFloat(c) + parseFloat(d) + parseFloat(e); }

document.getElementById("result").innerHTML = "" + z + ""; }   
        
</script>

<input type="submit" id="submitButton" name="submitButton" value="Place Order">

</form>

</body>

</html>

PHP_formHandler

<?php

$name = $_POST['customerName'];
$email = $_POST['customerEmail'];
$phone = $_POST['customerPhone'];
$flavor = $_POST['flavor'];
$scoops = $_POST['scoops'];
$nuts = isset($_POST['nuts']) ? 'Yes' : 'No';
$sprinkles = isset($_POST['sprinkles']) ? 'Yes' : 'No';
$syrup = isset($_POST['syrup']) ? 'Yes' : 'No';
$dish = $_POST['dish'];
$total = floatval($_POST['scoopsSelector'] ?? 0) + floatval($_POST['nuts'] ?? 0) + 
floatval($_POST['sprinkles'] ?? 0) + floatval($_POST['syrup'] ?? 0) + 
floatval($_POST['dishSelector'] ?? 0);


$composition =

"\r\nCUSTOMER ICE CREAM ORDER" .

"\r\n\nName: ". $name .
"\r\nEmail Address: " . $email .
"\r\nPhone: " . $phone .
"\r\nNumber of Scoops: ". $scoops . 
"\r\nNuts: " . $nuts .
"\r\nSprinkles: " . $sprinkles .
"\r\nSyrup: " . $syrup .
"\r\nDish: " . $dish .
"\r\nTotal: $" . $total ;


//For this example, please disregard any non-applicable php beyond this point //

$subject ="Customer Ice Cream Order";

$fromname ="$name";
$fromemail = "$email";

$mailto = 'recieving@email.com';


$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = "\r\n";
$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $composition . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";

@mail($mailto, $subject, $body, $headers);


?>

you used value="0" as default value, in PHP 0 wont treat as empty that is the reasion isset returning yes . use value='' may solve the issue, please give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM