简体   繁体   中英

Getting input type checkbox through PHP POST

Take a look at this code, is from a form in html which has a POST method and is trying to send some info to a file named validating-cart.php

<form action="actions/validating-cart.php" method="POST" id="carrito-form">     <!--COMBO CARRITO-->
   <legend class="carrito-legend">COMBO 2</legend>
   <input type="hidden" name="nameCombo" id="nameCombo" value="COMBO 2">
   
   <button class="reiniciar-btn" type="button">Borrar carrito</button>
   <div class="error-msj"></div>

   <ul>
    <li><label><input type="checkbox" name="art0" checked="checked" disabled="disabled" value="1 Panal de huevos AAA">1 Panal de huevos AAA</label></li>
    <li><label><input type="checkbox" name="art1" checked="checked" disabled="disabled" value="1 Pollo campesino">1 Pollo campesino</label></li>
    <li><label><input type="checkbox" name="art2" checked="checked" disabled="disabled" value="1 Libra de queso cuajada">1 Libra de queso cuajada</label></li>
    <li><label><input type="checkbox" name="art10" checked="checked" disabled="disabled" value="1 Lata de maiz tierno">1 Lata de maiz tierno</label></li>
    <li><label><input type="checkbox" name="art11" checked="checked" disabled="disabled" value="1 Lata de salchicha viena">1 Lata de salchicha viena</label></li>
  </ul>
  
  <output>$65.000</output>
  <input type="hidden" name="costo" id="precio" value="$65.000">
</form>

Through PHP file, I'm trying to retrieve through POST the input hidden elements as well the input check box with the value, what's weird is my PHP file is not recognizing what's in the input checkbox value, and it says an error like 'Array to string in conversion'. I don't know maybe is because this li items are created from JS with a JSON.

It doesn't matter how they are created, problem is that disabled form elements aren't sent by the browser at all (which you would recognize if you used Network tab of the inspection tool of your browser @see screenshot below). Run this form.php script to see how <input type="any" disabled="disabled"> is sent ( or rather is not sent ).

<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<form action="form.php" method="post">
    <input type="text" name="txt_disabled" disabled="disabled" value="Default Foo disabled"/><br>
    <input type="text" name="txt_readonly" readonly="readonly" value="Default Foo readonly"/><br>
    <input type="checkbox" name="box_disabled" disabled="disabled" checked="checked"> disabled <br>
    <input type="checkbox" name="box_readonly"  onclick="return false;" checked="checked"> readonly (with JS) <br>
    <input type="submit" name="formsent" value="Send">
</form>

<div>
    <b>$_POST after form send</b><br>
    <?php
    if (isset($_POST['formsent'])){
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
    }
    ?>
</div>
</body>
</html>

Network inspection in the browser shows clearly, that all fields with disabled attribute are not included in the POST's payload, literally they even don't leave the browser during form send, so you cannot fetch them anyway in PHP. You cannot bypass this with disabled fields, that's by design.

Instead, you can achieve the disable functionality using readonly attr instead of disabled , but as you can see also you will need to use some CSS styling to look readonly elements to look the same as they were disabled.

For anyone, who don't want to run the sample script here's its output on the screenshot:

浏览器中的脚本输出

Optional approach

... is using hidden fields with same name as disabled fields, however I'd check it in different browsers to make sure:

<form action="form.php" method="post">
    <input type="text" name="txt_disabled" disabled="disabled" value="Default Foo disabled"/><br>
    <input type="hidden" name="txt_disabled" value="Default Foo disabled"/>

    <input type="checkbox" name="box_disabled" disabled="disabled" checked="checked"> disabled <br>
    <input type="hidden" name="box_disabled"  checked="checked">

    <select name="select_disabled" disabled="disabled">
        <option value="1">One</option>
        <option value="2" selected="selected">Two</option>
        <option value="3">Three</option>
    </select><br>
    <input type="hidden" name="select_disabled" value="2">

    <input type="submit" name="formsent" value="Send">
</form>

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