0

I want to pass a php variable into a jquery function parameter, when i added an id to each loop it only return the first one. How to do it right ?

php code :

foreach ($record as $key):  ?>
         <tr>
         <td><a href="" onclick="update_amarta(<?php echo $key->SiteID ?>)"> NOT YET</a></td>
    </tr>
    <?php endforeach; ?>

Javascript :

<script type="text/javascript">

  function update_amarta(id) {
    alert(id);

  };

</script>
5
  • 1
    how much records in your foreach loop? please post your whole php code Commented Oct 25, 2018 at 12:38
  • 1
    is there any error ? Commented Oct 25, 2018 at 12:40
  • 1
    If $key->SiteID is a string you'll need to wrap the ouptut value in quotes. In that case you should see an error in the console when you attempt to click the element. Commented Oct 25, 2018 at 12:41
  • any errors in your browser's console when you run this? Commented Oct 25, 2018 at 12:45
  • 1
    Please, quit using alert() for troubleshooting., use console.log() instead. Commented Oct 25, 2018 at 12:52

1 Answer 1

1

Hi @highcal please use this PHP code inside inverted comma's. something like that

<a href="" onclick="update_amarta('<?php echo $key->SiteID ?>')"> NOT YET</a>

Hope this will work for you. Thank You.

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

1 Comment

you might want to explain why you think your answer will help (although of course it is based on an assumption about the data type). That would be much better for those reading it.

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.