0

I'm pulling some data from MySQL using PHP and need to pass an ID as a parameter to a onClick() method.

while($row = mysql_fetch_array($result))
{   
  echo '<a href=# onclick="showFilm('<?php echo $row['ID'] ?>')">' . 
    $row['Title'];
}

I'm getting a syntax issue on the echo statement. What am I doing wrong?

3 Answers 3

1

Yes you're trying to open a php tag inside a line of php. Do this

echo '<a href="#" onclick="showFilm(' . $row['ID'] .')">' . $row['Title'] . '</a>';

There's also a closing </a> you missed. Ideally you wouldn't use onclick but that's not what you asked about and it won't break with it there. Oh, and the value of href should be quoted.

So, since you ask, let's say you wanted to use jQuery instead of onclick

echo '<a href="#" class="showfilm" data-filmid="' . $row['ID'] .'">' . $row['Title'] . '</a>';

You would need to include the jquery library. See this http://docs.jquery.com/How_jQuery_Works and you would need your own script before </body> or in a separate js file.

<!-- the rest of your html blah blah -->
<script src="jquery.js"></script>
<script src="your-other-js-funcs.js"></script>
<script>
$(function(){
    $('.showfilm').on('click', function(e){
        e.preventDefault(); // stops the link doing what it normally 
                            // would do which is jump to page top with href="#"
        showFilm($(this).data('filmid'));
    });
});
</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks popnoodles - that was it - quite obvious looking at it :p
k. you missed the </a> too
"Ideally you wouldn't use onclick" what would you suggest? I'm quite new to PHP so not sure of any other ways.
It's not a PHP thing. See macek's answer, but he/she is assuming you are familiar with including the jQuery library and the dom ready state. Actually his/her answer isn't right for your script. I will edit mine...
lol still got two downvotes for this. some very odd characters around.
0

Another way:

<?php while($row = mysql_fetch_array($result)) { ?>   
    <a href=# onclick="showFilm('<?php echo $row['ID'] ?>')"> <?php echo $row['Title'] ?> </a>
<?php } ?>

Dont forget to close the a tag at the end. Didn't see it in your code.

2 Comments

unobtrusive javascript, please; help steer newbies toward best practices.
steering towards best practices can confuse newbies. one step at a time.
0

Your HTML

<?php while($row = mysql_fetch_assoc($result)): ?>
    <a href="#showFilm" data-film-id="<?php echo $row['ID'] ?>">
        <?php echo $row['Title'] ?>
    </a>
<?php endwhile ?>

Your jQuery

$('a[href=#showFilm]').click(function(event) {
    showFilm( $(this).attr('data-film-id') );
    event.preventDefault();
});

6 Comments

Where does the OP mention jQuery?
Provide a vanilla JavaScript example then. The OP is obviously very new, so help steer them in the right direction.
macek, the HTML in this answer is wrong. It does not explain to the user how to fix their problem. If they paste that in instead they will still see a PHP error.
I didn't include the implicit loop.
but they are using braces and echoing in one line not closing and reopening php tags. Best practice when modifying another's work is to format how they format.
|

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.