开发者

Problems parsing a checkbox array into a database

开发者 https://www.devze.com 2023-04-04 09:42 出处:网络
I have an application where I need to take the items from a list checkbox\'s and parse them into my database. Currently I am gathering them into an array by looping through the options and placing a c

I have an application where I need to take the items from a list checkbox's and parse them into my database. Currently I am gathering them into an array by looping through the options and placing a checkbox before each option and making the name example[] in each input. The question is how can I send that array to jquery and then to a php parse file so that it will be recorded in the database as a list deliminated by a comma so that I can call that list out into another field 开发者_运维问答on the website?

So this is what I have, but it is not working

$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>';
}

The jquery...

function post_song_list(){
    var allcharts = $(".allcharts").each().val();
    var team_name = $("#team_name").val();
    var list_date = $("#list_date").val();

    if (allcharts = ""){
        alert("You must select at least one song");
    }else{
        $.post("scripts/post_song_list.php", {allcharts: allcharts, team_name: team_name, list_date: list_date}, function(data){
            $(".allcharts").val("");
            $("#team_name").val("#team_name");
            $("#list_date").val("");
        });
    }
}

The php parse...

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>";
}

Can someone tell me where I am going wrong? It will not go to the parse file.


Not covering the jquery part, but assuming it was a normal form submission:

HTML:

<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />
etc...

PHP:

<?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());

of course, depending on how you're going to be using those values, you may NOT want to store them as a comma-separated list. If you need to refer to individual values in that list, you'd be better off using a child table to store each value separately.


Well, jquery can grab the checkboxes by using the form serialize function:

<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>

On the php side just call implode or join function:

  $str = implode(",", $_POST["check"]);
  $str = join(",", _POST["check"]; //is the same function.
0

精彩评论

暂无评论...
验证码 换一张
取 消