Jump to content

This is interested. Haven't noticed until now. Ajax won't show row the first time.


helloworld001
Go to solution Solved by helloworld001,

Recommended Posts

So I have a simple script that adds and returns mysql data without refreshing the page.  Everything works as it should.  The only issue that I've noticed is that if I login as a user and insert a record(text) the FIRST time, it won't return that record in the row below. Not until I refresh the page.  Now if I delete that record and insert a new record, it will then automatically return the record without the page refresh, as it should.

 

Can you tell me why this is happening?

 

Here is my js code.

<script>
	$(document).ready(function(){
	
		var recordId	=	$("#record-id").val();
		
		function showRecord(){
			$.ajax({
				type: "POST",
				url:  "bid-actions.php",
				data: "recordId="+recordId+"&action=showRecord",
				success:function(data){
					$(".show-records").html(data);
				}
		  });
		}

		showRecord();

		$(document).on('click','#record-submit',function() {
			
			var message		=	$("#message").val();
			var recordId	=	$("#record-id").val();
			
			$.ajax({
				type: "POST",
				url:  "bid-actions.php",
				data: "message="+message+"&recordId="+recordId+"&action=insertRecord",
				success:function(data){
					
					showRecord();
					$("#message").val('');
					
				}

			});
			
		});
   });
</script>

  Html code.

<div id="record-submit-form">
	<input type="hidden" id="record-id"  value="<?php echo $record_id; ?>">
	<textarea id="message"></textarea>
	<input type="button" id="record-submit" value="Submit">
</div>

<div class="show-records">
</div>
Edited by helloworld001
Link to comment
Share on other sites

Can't really tell with what you've provided. But, I notice that the showRecord() function works by checking the value of the field "record-id". If you create a new record, then the ID of that record would need to be populated into that field in order to run the showRecord() function on it. I'm guessing that has something to do with the problem.

Link to comment
Share on other sites

  • 3 weeks later...
  • Solution

Can't really tell with what you've provided. But, I notice that the showRecord() function works by checking the value of the field "record-id". If you create a new record, then the ID of that record would need to be populated into that field in order to run the showRecord() function on it. I'm guessing that has something to do with the problem.

 

Well the forum is back online. 

 

The problem is fixed now.  My code is correct.  It's just that I was hiding the output using php if statement.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.