Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Need to convert a value output in php

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
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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 ;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda