通过jQuery onclick将额外的数据传递给PHP

时间:2016-12-28 16:33:54

标签: php jquery variables post onclick

我正在进行测验,并希望在用户提交答案时将其他变量传递给PHP(类似于隐藏的输入字段)。这是我的代码,当用户点击"提交"时,我尝试传递变量' test'在第3行:

function processQuestion(choice) {
         name = 'vote';
         $.post('poll_vote.php', {test: 2}, function() {
             alert(value);
         });
        getVote((val[0][quiz[currentquestion]['choices'][choice]]));
         if (quiz[currentquestion]['choices'][choice] == quiz[currentquestion]['correct']) {
             $('.choice').eq(choice).css({
                 'background-color': '#50D943'
             });
             $('#explanation').html('<strong>Correct!</strong> ' + htmlEncode(quiz[currentquestion]['explanation']));
             score++;
         } else {
             $('.choice').eq(choice).css({
                 'background-color': '#D92623'
             });
             $('#explanation').html('<strong>Incorrect.</strong> ' + htmlEncode(quiz[currentquestion]['explanation']));
         }
         currentquestion++;
         $('#submitbutton').html('NEXT QUESTION &raquo;').on('click', function () {
             if (currentquestion == quiz.length) {
                 endQuiz();
             } else {
                 $(this).text('Submit').css({
                     'color': '#fff'
                 }).off('click');
                 nextQuestion();
             }
         })
     }

在poll_vote.php中,我有:

$vote = $_REQUEST['vote'];
$pollid = $_POST['test'];
echo '<script type="text/javascript">alert("' . $pollid . '")</script>';

看来&#39;测试&#39;没有被传递。理想情况下,我希望它被认为是&#34; 2&#34;用PHP。

0 个答案:

没有答案