Jump to content
Adamhumbug

Ajax function not running

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

  • Thanks 1

Share this post


Link to post
Share on other sites
Posted (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 by Adamhumbug

Share this post


Link to post
Share on other sites

Glad that did it!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.