nitrex
nitrex

Reputation: 542

passing php to javascript onclick event using foreach loop

I am trying to submit a parameter using JavaScript on click event on a li item, inside a foreach loop, the answers are pulled out of the database and each answer have an id, I want to write the JavaScript code to redirect to another PHP page carrying the parameter, which I pull out with GET,

I pasted the code below for the foreach and the JavaScript function is called clickFunc(),

foreach ($questions_answers_list as $questionID ) {
    $questionID = $questions_answers_list[$answerCount]['questionID'];

    if ($questionID !== "$quesID") {
        $answerCount++;
        continue;
    } else {
        ?>

        <li class="answersList" onclick="clickFunc()">
        <?php echo $questions_answers_list[$answerCount]['answerArabic']; ?>
        </li>

        <?php
        $answerCount++;
    }
    break;
}

Thanks in advance!

Upvotes: 0

Views: 1759

Answers (1)

tymeJV
tymeJV

Reputation: 104795

Write the PHP ID you want as a parameter to your JS function:

<li class="answersList" onclick="clickFunc('<?php $myId ?>')">

And then the onclick

function clickFunc(id) {
    console.log(id); //should see ID here
}

Upvotes: 3

Related Questions