Jquery添加到数组然后提交和传递数据不工作


jquery add to array then submit and pass data not working

我的问题是:我试图提交一个隐藏的输入类型的数组,这是堆叠成一个数组使用jquery onclick,到一个PHP文件。但是,当我尝试计数甚至回显php文件(saveTest.php)中传递的变量时,没有数据出现或计数变量为零。我搜索了一下,发现了这个人的问题:将数组从jQuery传递给PHP(并在提交后实际转到页面)

我想我接近上面的帖子,但我仍然是jQuery的新手,所以我不理解很多代码。这是我的jquery:

$(function(){
  $("td").click(function(){
    if($(this).hasClass("on"))
 {
    alert("Already marked absent");
 }
else
{
    $(this).addClass("on");
    var currentCellText = $(this).text();
    $("#collect").append("<input type='text' hidden = '" + currentCellText + "'/>" + currentCellText);
 }
});
$("#clicky").click(function(){
    $("td").removeClass("on");
    $("#collect").text('');
    $("#collect").append("Absentees: <br>")
});
});
<?php
session_start();
include 'connectdb.php';
$classID = $_SESSION['csID'];
$classQry = "SELECT e.csID, c.subjCode, c.section, b.subj_name, e.studentID, CONCAT(s.lname, ', ' , s.fname)name
FROM ENROLLMENT e, CLASS_SCHEDULE c, STUDENT s, SUBJECT b
WHERE e.csID = c.csID
AND c.csID = '" . $classID . "'
AND c.subjCode = b.subjCode
AND e.studentID = s.studentID
ORDER BY e.sort;";
$doClassQry = mysql_query($classQry);
echo "<table id='tableone'>";
    while($x = mysql_fetch_array($doClassQry))
    {
        $subject = $x['subj_name'];
        $subjCode = $x['subjCode'];
        $section = $x['section'];
        $studentArr[] = $x['name'];
        $studentID[] = $x['studentID'];

    }
    echo "<thead>";
    echo "<tr><th colspan = 7>" . "This is your class: " . $subjCode . " " . $section . " : " . $subject . "</th></tr>";
    echo "</thead>";
    echo "<tbody>";
    echo "<tr>";        
    for($i = 0; $i < mysql_num_rows($doClassQry); $i++)
    {
        if($i % 7 == 0)
        {               
            echo "</tr><tr><td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";             
        }
        else
        {
            echo "<td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";
        }
    }
    echo "</tr>";       
    echo "</tbody>";
echo "</table>";

?>

这是我的php文件(saveTest.php)

<?php
$absent = $_POST['absent'];
//echo "absnt" . $absent[] . "<br>";
echo count($absent);
?>

为隐藏字段添加名称:

<>之前$("#collect").append("" + currentCellText);

看起来你想提交一个javascript数组到php脚本,然后利用它。您可以使用.each()函数循环遍历所有隐藏的值并将它们添加到数组中。然后使用$。Post将数组提交给PHP脚本。

<script src="jquery.js"></script>
<script>
$(function(){
    $('#btn_submit').click(function(){
        var array_hidden = [];
        $('input[type=hidden]').each(function(index){
            var current_value = $.trim($(this).val());
            array_hidden[index] = current_value;
        });
        $.post('arraysubmit.php', {'hidden_array' : array_hidden}, function(data){
            $('#results').html(data);
        });
    });
});
</script>
<?php for($x=0; $x<=10; $x++){ ?>
<input type="hidden" name="name[]" value="Name<?php echo $x; ?>">
<?php } ?>
<input type="button" id="btn_submit">
<div id="results"></div>

你可以在php脚本中使用post变量访问数组,并对它做任何你想做的事情:

$_POST['hidden_array']