Radio button is not passing value

Asked

Viewed 214 times

0

I use 3 radio Buttons to select the input voltage of an equipment (110V, 220V or Automatic).
If I put only 2 radio Buttons, both work normally. If I put 3, the first one doesn’t work properly. He apparently does not pass the value assigned to him for the post. As "twig breaker" I put 4 radio Buttons, the first hidden (by not working) and the others the values I wanted to pass.

I’ve tried to exchange values, names and things like that, but the problem is always with the first radio button if I have more than 3.

        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Entrada:</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="1">110V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="2">220V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="3">Automática</label>
            </div>
        </div>

That way, if I select the first radio of value 1, the value will not be sent to the post

        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Entrada:</label>
            <div class="radio" hidden="true">
                <label><input type="radio" name="entrada" id="entrada" value="4">110V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="1">110V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="2">220V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="3">Automática</label>
            </div>
        </div>

That way, I hide the first button (since it doesn’t work) and let the others, which work normally.
This practice is not ideal, in my view, and I have tried to copy the code of the other buttons (everything is the same) and even so the first one does not work.

The radio Buttons, along with other things are within a form with post sending to a file. php containing the following:

$serial = $_POST['serial'];
$marca = $_POST['marca'];
$modelo = $_POST['modelo'];
$potencia = $_POST['potencia'];
$entrada = $_POST['entrada'];
$patrimonio = $_POST['patrimonio'];

$stmt = $conn->prepare("UPDATE Nobreak SET Marca_Nobreak = '$marca', Modelo_Nobreak = '$modelo', Potencia_Nobreak = '$potencia', Cod_EntradaNoBreak = '$entrada', Patrimonio_Nobreak = '$patrimonio' WHERE Serial_Nobreak = '$serial'");
$stmt->execute();

Full form:

      <form method="POST" action="alterar.php">
        <input name="tipo" type="text" class="form-control" hidden="true" id="tipo" value="nobreak">
        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Serial:</label>
          <input name="serial" type="text" class="form-control" id="serial" readonly="readonly">
        </div>
        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Marca:</label>
          <input name="marca" type="text" class="form-control" id="marca">
        </div>
        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Modelo:</label>
          <input name="modelo" type="text" class="form-control" id="modelo">
        </div>
        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Potência:</label>
          <input name="potencia" type="text" class="form-control" id="potencia">
        </div>
        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Entrada:</label>
            <!-- Sem esse input a mais ele não funciona -->
            <div class="radio" hidden="true">
                <label><input type="radio" name="entrada" id="entrada" value="4">110V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="1">110V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="2">220V</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="entrada" id="entrada" value="3">Automática</label>
            </div>
        </div>
        <div class="form-group">
          <label for="recipient-name" class="col-form-label">Patrimônio:</label>
          <input name="patrimonio" type="text" class="form-control" id="patrimonio">
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-primary" title="Salvar">Salvar</button>
        </div>
      </form>
  • How you’re doing form post?

  • @dvd changed the question with the post information

  • The question was how is doing the post on the client side. Is doing some validation?

  • @dvd Ahhh, I think I get it. I’m new at this, rs. I’m not doing any validation, the code is basically just that

  • Put the full form in the question to give a light.

  • @dvd I put the full form. thanks for the force

  • Next, here it worked perfect, even without the gambit... must be giving some kind of conflict in the code. For testing purposes, try changing the name of the radios as well as the $_POST to see if there are any different results. Change the Names of entrada for entrada_ and $entrada_ = $_POST['entrada_'];...

  • @dvd did exactly what you said. I changed the name of the radios, the post and also my SQL query because of the new variable, but the error persists. Since I only left three radios, the first one doesn’t work and the other two work. I put the gambiarra again and the first one doesn’t work and the next three works. I have no idea what it might be :((

  • One question: this id="entrada" is for q?

  • @dvd for nothing, I was using for some tests and did not take. I took now and still the problem persists. I did a test now with print, to know what the first radio returns and it always returns the value that was already displayed (which was returned previously from the bank), but I saw no reason for this

  • This will show a alert with form variables.... see if in Alert the "input" value is correct.

  • @Dude dvd, I brought the sql table, the pages and installed the shaman at home. worked normally... must be something in my work environment, I’ll try to reinstall things there, I think I don’t even need to continue... thanks for the force, man, really

Show 7 more comments

1 answer

-2

I don’t know much about php, but try removing the id of 'input type="radio"', leaving only the name. In the first piece of code you submitted, the form-group div is being closed just after the first label, but in full code it seems to be normal. I took a test on this site http://phpfiddle.org/ leaving inputs only with name and worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.