将复选框值发送到PHP文件


post checkbox values to a php file

我试图写一个脚本与jQuery复选框id被选中的帖子(php删除脚本)。

我有这样的东西

   <form>
    <input type="checkbox" name="id" id="id" value='1'>
    <input type="checkbox" name="id" id="id" value='2'>
    <input type="checkbox" name="id" id="id" value='3'>
    <input type="button" name="DELETE" id="DELETE">
     </form>

所以我想张贴这些值(id)到一个php文件delete.php,我怎么能做到这一点?

HTML

<form>
    <input type="checkbox" name="id" value='1'>
    <input type="checkbox" name="id" value='2'>
    <input type="checkbox" name="id" value='3'>
    <input type="button" name="DELETE" id="DELETE">
</form>
JQUERY

<script>
$(document).ready(function(){
    $("#delete").click(function(){
        var data = $('input:checkbox:checked').map(function(){
             return this.value;
        }).get();

        var dataString = "imgList="+ data;
       $.post('delete.php',dataString,function(theResponse){
       //// Check theResponse
       });               

   });      
});
</script>
PHP

$imgList = $_REQUEST['imgList'];
$i = 0;
$token = strtok($imgList, ","); 
$imgArray = array();
while ($token != false){
    $imgArray[$i] = (string)$token;
    $token = strtok(",");
    $i++;
} 

我假设您希望通过使用jQuery ajax调用post到php

来实现这一点

<form>
    <input type="checkbox" name="id[]" id="id" value='1'>
    <input type="checkbox" name="id[]" id="id" value='2'>
    <input type="checkbox" name="id[]" id="id" value='3'>
    <input type="button" name="DELETE" id="DELETE">
</form>
Javascript with jQuery

var selected_values = $("input[name='id[]']:checked");

直接post to delete.php

<?php
    // $_POST['id'] will return an array.
    $selected_values = $_POST['id'];
?>

有很多方法可以做到。将表单集合发送到PHP页面。确保您的输入和其他元素具有惟一的id。

<form action="yourUrl/Method" Method="POST"> 
 <input type="checkbox" name="id" id="id1" value='1'>
    <input type="checkbox" name="id" id="id2" value='2'>
    <input type="checkbox" name="id" id="id3" value='3'>
    <input type="button" name="DELETE" id="DELETE">
</form>

然后在PHP中访问请求对象并使用这些值。

单个复选框

<form action="checkbox-form.php" method="post">
    Do you need wheelchair access?
    <input type="checkbox" name="formWheelchair" value="Yes" />
    <input type="submit" name="formSubmit" value="Submit" />
</form>
在PHP脚本中,我们可以从$_POST数组中获得提交的选项。如果$_POST[' form轮椅']为" Yes ",则复选框已选中。如果复选框没有被选中,$_POST[' form轮椅']将不会被设置。

下面是一个PHP处理表单的例子:

<?php
      if(isset($_POST['formWheelchair']) && 
      $_POST['formWheelchair'] == 'Yes') 
      {
          echo "Need wheelchair access.";
      }
     else
      {
          echo "Do not Need wheelchair access.";
      }  
  ?>

$_POST['formSubmit']的值被设置为' Yes ',因为输入标签中的value属性是' Yes '。

您可以将该值设置为' 1 '或' on '而不是' Yes '。确保检入PHP代码也相应更新。

更多信息见以下文章。

http://www.html-form-guide.com/php-form/php-form-checkbox.html

判断复选框是否被选中php $_GET

希望有所帮助