Adamhumbug Posted July 10, 2020 Share Posted July 10, 2020 I have a modal: My Modal The user should be able to click on the items on the right to add them to the left. The code for the buttons is: <div data-id="1" class="transaction-item btn btn-primary d-block mb-2">Adult Membership (£85)<br>£85.00</div> i have some ajax that is looking for the button press: $('.transaction-item').click(function(){ var itemId = $(this).data("id"); console.log(itemId); $.ajax({ type: 'post', data: {'ajax' : 'three', "id" : itemId}, success: function(resp){ $('#transaction-container').html(resp) } }) }); Then this happens: case 'three': exit(newTransactionItem($conn, $_POST['id'])); break; Then this should build the item to go in function newTransactionItem($conn, $itemId){ include 'includes/dbconn.php'; $stmt = $conn ->prepare(" SELECT item, direction, value FROM transaction_items where id = ? "); $stmt -> bind_param("i", $itemId); $stmt -> execute(); $stmt -> bind_result($item, $dir, $val); $out=""; while($stmt -> fetch()){ if($dir == "IN"){ $class = 'alert-primary'; }else{ $class = 'alert-dark'; } $out.=" <div class='alert $class'> <span class='mr-3'>1 x</span> <span class='font-weight-bold'>$item</span> <span class='ml-3 border-left'>$notes</span> <span class='float-right'>£$val</span> </div> "; } return $out; } Currently, nothing is happening when i click the button with that class. I have a console log in the ajax which is not running. Literally nothing is happening on button click. As always, your help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/311057-ajax-function-not-running/ Share on other sites More sharing options...
maxxd Posted July 10, 2020 Share Posted July 10, 2020 Is the div in the DOM on page load or is it added dynamically later? You may have to use .on() to bind the event to the element. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311057-ajax-function-not-running/#findComment-1579472 Share on other sites More sharing options...
Adamhumbug Posted July 10, 2020 Author Share Posted July 10, 2020 (edited) 1 minute ago, maxxd said: Is the div in the DOM on page load or is it added dynamically later? You may have to use .on() to bind the event to the element. It is dynamic - you think onClick() on the where the div is created? Edited July 10, 2020 by Adamhumbug Quote Link to comment https://forums.phpfreaks.com/topic/311057-ajax-function-not-running/#findComment-1579473 Share on other sites More sharing options...
Adamhumbug Posted July 10, 2020 Author Share Posted July 10, 2020 Yes that was the answer. Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/311057-ajax-function-not-running/#findComment-1579474 Share on other sites More sharing options...
maxxd Posted July 11, 2020 Share Posted July 11, 2020 Glad that did it! Quote Link to comment https://forums.phpfreaks.com/topic/311057-ajax-function-not-running/#findComment-1579505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.