0

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!

3
  • What is your question? Do you need help with javascript, php, or both? Commented Dec 29, 2014 at 21:07
  • What exactly are you asking? Commented Dec 29, 2014 at 21:08
  • thank you i wanted to pass the id to javascript,then redirect to another page with the id in the url, Commented Dec 29, 2014 at 21:18

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

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.