0

Trying to select the clicked div's id with jQuery and insert it to mysql so I can load the right content on the next page but the insert does not get any value. I get the right id by the alert function and the insert is working fine with an actual number but I need the one that the user clicked on.

$(function() {
        $('.current_content').click(function() {
            alert($(this).attr('id'));

            <?php

            $content_id_insert = "INSERT INTO selected_content (content_id) VALUES(?>$(this).attr('id')<?php)";

            $conn->query($content_id_insert);

            ?>

            document.location = 'content.php';
            
        })

})

And the div:

<div class="current_content" id="<?php echo($row['id']); ?>">
    ...
</div>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You need to use AJAX for this. PHP isn't available once the page has loaded. This also would be open to SQL injections. Look into prepared statements with parameterized queries. – user3783243 Nov 02 '20 at 13:52
  • Div elements are not designed to be interactive. They don't show up in the focus order. Screen readers don't announce them as interactive. Putting click events on them is an accessibility problem. Use a button if you want something for the user to click on. – Quentin Nov 02 '20 at 13:52

0 Answers0