0

My problem is: I'm trying to submit an array of hidden input types, which are stacked into an array using jquery onclick, to a PHP file. However, when I try to count or even echo the passed variable in the php file (saveTest.php), no data appears or the count variable is zero. I've searched and I found this guy's question: pass an array from jQuery to PHP (and actually go to the page after submit)

I think I'm close to the above post but I'm still a newbie in jQuery so I don't understand much of the codes. This is my 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>";

?>

This is my php file (saveTest.php)

<?php
$absent = $_POST['absent'];
//echo "absnt" . $absent[] . "<br>";
echo count($absent);
?>
6
  • typo It's supposed to be "<input type='hidden' name = 'absent[]' value = '" + currentCellText + "'/>" Commented Nov 27, 2011 at 16:59
  • Could you post your whole code? It's a bit vague right now. Commented Nov 27, 2011 at 17:07
  • use Firebug to check what is actually being passed to the page Commented Nov 27, 2011 at 17:48
  • use print_r. value showing is 0 Commented Nov 28, 2011 at 0:34
  • tried to use method="get" to see what's being passed. it seems that there is no value being passed since only saveTest.php? is showing in the address bar. help please? :) Commented Nov 28, 2011 at 1:02

2 Answers 2

0

Add name to hidden field:

 $("#collect").append("<input type='hidden' name="absent[] value= '" + currentCellText + "'/>" + currentCellText);

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you want to submit a javascript array to a php script and then make use of it. You can make use of .each() function to loop through all the hidden values and adding them into the array. Then use $.post to submit the array to a php script.

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

You can then access the array in the php script using the post variable and do whatever you want with it:

$_POST['hidden_array']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.