Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将复选框数组解析到数据库时出现问题_Php_Jquery_Html - Fatal编程技术网

Php 将复选框数组解析到数据库时出现问题

Php 将复选框数组解析到数据库时出现问题,php,jquery,html,Php,Jquery,Html,我有一个应用程序,需要从列表复选框中提取项目,并将它们解析到我的数据库中。目前,我正在将它们收集到一个数组中,方法是循环遍历选项,在每个选项之前放置一个复选框,并在每个输入中使用名称example[]。问题是,我如何将该数组发送到jquery,然后发送到php解析文件,以便将其记录在数据库中,作为一个列表,用逗号删除,以便我可以将该列表调用到网站上的另一个字段中 这就是我所拥有的,但它不起作用 $chart_query = mysql_query("SELECT id, title FROM s

我有一个应用程序,需要从列表复选框中提取项目,并将它们解析到我的数据库中。目前,我正在将它们收集到一个数组中,方法是循环遍历选项,在每个选项之前放置一个复选框,并在每个输入中使用名称example[]。问题是,我如何将该数组发送到jquery,然后发送到php解析文件,以便将其记录在数据库中,作为一个列表,用逗号删除,以便我可以将该列表调用到网站上的另一个字段中

这就是我所拥有的,但它不起作用

$chart_query = mysql_query("SELECT id, title FROM songs WHERE team_name='$team_name' ORDER BY title ASC");
while($row = mysql_fetch_array($chart_query)){
    $song_id = $row['id'];
    $song_title = $row['title'];

    $song_chart .= '<div align="left" class="song_links"><input class="allcharts" type="checkbox" name="charts[]" value="'.$song_title.'" />&ndash;<a href="http://' . $dyn_www . '/template.php?id='.$song_id.'" style="color:white; text-decoration:none; margin-left:5px;">'.$song_title.'</a></div>';
}
php解析

if(isset($_POST['team_name']) != '' && isset($_POST['list_date']) != '' && isset($_POST['charts']) != ''){
    if(isset($_POST['team_name'])){
        $team_name = $_POST['team_name'];
             $team_name = stripslashes($team_name);
             $team_name = strip_tags($team_name);
             $team_name = mysql_real_escape_string($team_name);
             $team_name = str_replace("'", "&#39;", $team_name);
    }
    $sql = mysql_query("SELECT admin_id FROM myTeams WHERE team_name=$team_name");
    while($row = mysql_fetch_assoc($sql)){
        $admin_id=$row['admin_id'];
        if(isset($_SESSION['idx']) != $admin_id) {
            $msgToUser = '<br /><br /><font color="#FF0000">Only Team Administrators can do that!</font><p><a href="register.php">Join Here</a></p>';
            include_once '../msgToUser.php'; 
            exit(); 
        }
    }
    if(isset($_POST['list_date']))
    $list_date = $_POST['list_date'];
             $list_date = stripslashes($list_date);
             $list_date = strip_tags($list_date);
             $list_date = mysql_real_escape_string($list_date);
             $list_date = str_replace("'", "&#39;", $list_date);
             $list_date = "".$list_date."";
    }

    if(isset($_POST['charts']) != ""){ 
    $checkboxes = isset($_POST['charts']) ? (array)$_POST['charts'] : array();
    $charts = implode('<br />', $checkboxes);
        $charts = stripslashes($charts);
        $charts = strip_tags($charts);
        $charts = mysql_real_escape_string($charts);
        $charts = str_replace("'", "&#39;", $charts);

    $sql_charts = mysql_query("INSERT INTO list (date, team_name, song_list) VALUES ('$list_date','$team_name','$charts')") or die (mysql_error());
    echo "<span>List Uploaded</span>";
}

有人能告诉我哪里出了问题吗?它不会转到解析文件。

不涉及jquery部分,但假设它是一个普通表单提交:

HTML:

PHP:


当然,根据您将如何使用这些值,您可能不希望将它们存储为逗号分隔的列表。如果需要引用该列表中的各个值,最好使用子表分别存储每个值。

好的,jquery可以使用表单序列化函数获取复选框:

<form class="checkbox-form">
<input type="checkbox" class="chk" name="check[]" value="val1" />
<input type="checkbox" class="chk" name="check[]" value="val2" />
<input type="checkbox" class="chk" name="check[]" value="val3" />
<input type="button" class="send" value="Send" />
</form>
<script type="text/javascript">
function checkbox_callback(){
     $(".chk").each(function(index, domElem){
     //here you grab each checkbox element and do anything you want
     //(i think you want to do some validation here)
     });
}

//To send to php you can use ajax
$(".send").click(function(){
    //here you can call the checkbox function callback
    valid = checkbox_callback();
    if(valid){
       $.ajax({
            type: "POST",
            url: "yourphp.php",
            data: $(".checkbox-form").serialize()
       });
    }
});
</script>
  $str = implode(",", $_POST["check"]);
  $str = join(",", _POST["check"]; //is the same function.

你能告诉我们到目前为止你有什么吗?
<?php

$checkboxes = isset($_POST['cb']) ? (array)$_POST['cb'] : array();

$string = implode(',', $checkboxes);
$ready_for_sql = mysql_real_escape_string($string);

$sql = "INSERT INTO yourtable (checkboxfield) VALUES ('$ready_for_sql')";
$result = mysql_query($sql) or die(mysql_error());
<form class="checkbox-form">
<input type="checkbox" class="chk" name="check[]" value="val1" />
<input type="checkbox" class="chk" name="check[]" value="val2" />
<input type="checkbox" class="chk" name="check[]" value="val3" />
<input type="button" class="send" value="Send" />
</form>
<script type="text/javascript">
function checkbox_callback(){
     $(".chk").each(function(index, domElem){
     //here you grab each checkbox element and do anything you want
     //(i think you want to do some validation here)
     });
}

//To send to php you can use ajax
$(".send").click(function(){
    //here you can call the checkbox function callback
    valid = checkbox_callback();
    if(valid){
       $.ajax({
            type: "POST",
            url: "yourphp.php",
            data: $(".checkbox-form").serialize()
       });
    }
});
</script>
  $str = implode(",", $_POST["check"]);
  $str = join(",", _POST["check"]; //is the same function.