猿问

如何在php中提交时获取包含多个选项的行的值?

我必须准备一个包含多行产品的购物车,每一行都应该有诸如“产品品牌”“产品重量”之类的选项按钮……用户应该通过单击其复选框来选择产品。表格下方只有一个按钮(添加到购物车)用户应通过选中其复选框来选择产品,并在选择许多产品后,通过单击添加到购物车按钮将它们添加到购物车。


我尝试过的:


我用选项创建了行,并在同一行中给了每个元素,一个数组作为“名称”,但没有解决。


请参阅以下示例代码以了解我的工作。


<form method="post">

  <table>

    <tr>

      <td>

        <input type="checkbox" name="">

      </td>

      <td>

        <select>

          <option value="car">car</option>

          <option value="bike">bike</option>

        </select>

      </td>

      <td>

        <select>

          <option>

            <option value="blue">blue</option>

            <option value="red">red</option>

          </option>

        </select>

      </td>

    </tr>

    <tr>

      <td>

        <input type="checkbox" name="">

      </td>

      <td>

        <select>

          <option value="cycle">cycle</option>

          <option value="skate">skate</option>

        </select>

      </td>

      <td>

        <select>

          <option>

            <option value="male">male</option>

            <option value="female">female</option>

          </option>

        </select>

      </td>

    </tr>

    <tr>

      <td>

        <input type="checkbox" name="">

      </td>

      <td>

        <select>

          <option value="dog">dog</option>

          <option value="cat">cat</option>

        </select>

      </td>

      <td>

        <select>

          <option>

            <option value="bat">bat</option>

            <option value="ball">ball</option>

          </option>

        </select>

      </td>

    </tr>

  </table>

  <input type="submit" name="submit">

</form>

绝地无双
浏览 158回答 2
2回答

qq_遁去的一_1

首先,您需要action在表单元素上设置一个,包含表单数据应该被发布到的位置。请参阅https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form其次,您需要向name表单元素添加属性,以便稍后由后端脚本获取它们。请参阅https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select最后,在您的后端(大概是 PHP)中,您可以使用这些值$_POST(或为您提供的任何抽象,例如https://symfony.com/doc/current/components/http_foundation.html#accessing-request-data)最后一点,请确保您验证进入后端的所有数据!

拉丁的传说

将所需字段的名称 attr 添加为数组。形式会像。<form method="post"><table><tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" name="attribute[1]['checked']">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <select name="attribute[1]['product']">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="car">car</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="bike">bike</option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <select name="attribute[1]['product_prop']">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="blue">blue</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="red">red</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </td></tr><tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" name="attribute[2]['checked']">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<select name="attribute[2]['product']">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="cycle">cycle</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="skate">skate</option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<select name="attribute[2]['product_prop']">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="male">male</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="female">female</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </td></tr><tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" name="attribute[3]['checked']">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp;<select name="attribute[3]['product']">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="dog">dog</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="cat">cat</option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <select name="attribute[3]['product_prop']">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="bat">bat</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="ball">ball</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </td></tr></table><input type="submit" name="submit" value="submit"></form>提交表单后,您将在数组中获得选中的行值。使用循环获取值。喜欢。<?phpif(isset($_POST['submit'])){&nbsp; &nbsp; $attr = $_POST['attribute'];&nbsp; &nbsp; /*echo "<pre>";&nbsp; &nbsp; print_r($attr);*/&nbsp; &nbsp; foreach($attr as $key=>$value){&nbsp; &nbsp; &nbsp; &nbsp; //get the value of checked row - product, product_prop etc&nbsp; &nbsp; &nbsp; &nbsp; if(isset($value["'checked'"]) && $value["'checked'"] != ''){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $product = $value["'product'"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $product_prop = $value["'product_prop'"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add your code here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}?>
随时随地看视频慕课网APP
我要回答