Selim
Selim

Reputation: 197

Pass an array through .load

I have this jQuery code. What it does is that it gets values from (selected) radio buttons and then passes it in the 'c' array.

There are 40 items thus the b<40 condition in the for statement. The next step that I'm doing is to pass it on PHP using .load(). As everybody can see in the code below, I am only in c[3]. Because on the way in I thought, maybe there's a way to pass this array without encoding them one by one.

So, is there a way to pass the array to PHP using .load() without encoding them one by one or am I just lazy?

$(document).on('click', '#submitexam', function() {
    var examinee = $('#examinee').val();
    var q1 = $('#QU1').attr('id');
    var c = ["null"];
    var b;
    for (b = 1; b < 40; b++) {
        c.push($('input[name=Q' + b + ']:checked').next('label:first').html());
        console.log(c[b]);
    }

    $('.bod').load('process.php', {
        examinee: examinee,
        a1: c[1],
        a2: c[2],
        a3: c[3],
        q1: q1
    })
});

Upvotes: 0

Views: 47

Answers (1)

Susheel Singh
Susheel Singh

Reputation: 3854

.load() is only used for getting data from the server.

You are using a form, so you need to post the data to server using $.post or $.ajax.

serialize() function is used to take all data from the form and convert it to string.

jquery

$(document).on('submit', '#submitexam', function(e) {
    e.preventDefault();
    $.post($(this).attr("action"), $(this).serialize()).done(function(data) {
        console.log(data);
    });
});

html

<form action="process.php" method="post" id="submitexam">
    all your radio buttons place it here and at the end instead of button use
    <input type="submit" value="submit"/>
</form>

Upvotes: 1

Related Questions