Home > Enterprise >  Need to convert a value output in php
Need to convert a value output in php

Time:06-06

I have a form with checkbox input, and a hidden 'sibling' input attached to it, so that I can generate 2 different values, '0' or '3' depending on whether or not the box is checked.

Unchecked = '0', Checked = '3'.

In the form itself, and on the form page itself, I do not want to change those values.

I need them to remain '0' and '3' for a (calculator) javascript function within the form.

The values represent actual 'price' values, $0.00 and $3.00 depending on whether or not the customer choses to include dressing on their salad order.

However... on the php page which handles the form output and 'mailer', instead of that checkbox outputting '0' or '3'.... I need it to produce a 'No' or a 'Yes' for the kitchen staff.

Unchecked = 'No', Checked = 'Yes'.

I was thinking there must be some kind of simple '0 = No' and '3 = Yes' type of 'conversion' code I can use on the php side.

I have tried the following, but it always produces only a 'Yes' on the receiving end. Never a 'No', even when the box is unchecked.

Any advice would be appreciated

$dressing = isset($_POST['dressing']) ? 'Yes' : 'No';

"Do you want dressing on your salad? " . $dressing ;

Just for reference, I have included the code for the 'attached' input fields below. But again, I don't THINK I need or want to mess with that. I have a feeling the problem/solution will be on the php handling side, but I'm at a loss.

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

CodePudding user response:

The simplest solution is probably just to pass the checkbox itself to your PHP code; then you can use code similar to what you already have. So give it a name:

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

and check that value in your PHP instead:

$dressing = isset($_POST['dressingcheckbox']) ? 'Yes' : 'No';

"Do you want dressing on your salad? " . $dressing ;

CodePudding user response:

It always produces Yes because you check for value being set, or in other words existing. It always exists and is set to either 0 or 3. You can read about it in PHP docs

If the php side is at fault then checking for the right value should fix it. In your case

$dressing = $_POST['dressing'] === '0' ? 'No' : 'Yes';

This might not work if types don't match because === checks for both value and type equality. In case that happens easiest way to check why is to do

var_dump($_POST['dressing']);

To check if value is of the type and value we expect.

This all assumes that you can't or don't want to change 0 or 3. From experience I can tell you should really send only information about checkbox being checked and all the logic about what this means should be in one place. Otherwise you get duplicated logic and have to make changes in 100 places to change dressing price from 3 to 5. It gets even worse when you have scripts, multiple views or save it in database.

  • Related