1

I have this strange issue, which is hard for me to describe to be honest.

For testing purposes I have set up a SQL db containing id (autoincrement), first name, last name. I have 2 entries there.

I call the db and list the entries in inputs like this:

while($row = $result->fetch_assoc()) {
    $id = $row['id'];
    $fname = $row['fname'];
    $lname = $row['lname'];
    echo "<div id='$id'>
        <form method='post'>
          <p>ID: <input type='text' name='id' id='id".$id."' value='$id' /></p>
          <p>New first name: <input type='text' name='fname' id='fname".$id."' placeholder='$fname' /></p>
          <p>New last name: <input type='text' name='lname' id='lname".$id."' placeholder='$lname' /></p>
          <input type='submit' name='update' class='update' value='ID = $id' />
        </form>
      </div>";
    }

Works perfectly fine and I get a page with both entries and a button to update them.

I am updating them with this function

$('.update').click(function(){
      var row = $(this).parent().parent().attr('id'); // I've stored the SQL id in the parent div

      // construct the input ids so i know where I am getting the data from
      var idSource = '#id'+row;
      var fnameSource = '#fname'+row;
      var lnameSource = '#lname'+row;

      // store the input values in variables
      var id = $(idSource).val();
      var fname = $(fnameSource).val();
      var lname = $(lnameSource).val();

      // use ajax to update sql table    
      $.ajax({
        url     : 'aiai.php',
        method  : 'post',
        data    : {id: id, fname: fname, lname: lname},
        success: function(response){
          console.log(response);
        }
     });
    });

I am doing this so that every entry can be edited & updated on it's own. An while this basically working I am getting a strange lag.

Example:

  • I load the page, update the first name, click the update button --> Works
  • Then I edit the same entry again, click the update button --> i am getting the old value again
  • When I refresh the page I get the name update I just saved
  • Lag continues until I refresh the page

I load the page, update the first name, click the update button --> Works
Then I edit the same entry again, click the update button --> i am getting the old value again
When I refresh the page I get the name update I just saved.
Lag continues until I refresh the page.
It's like that info gets cached in the browser.

BUT, and this confuses me:
When I hardcode the inputs where I am calling the values from, everything works perfect.
So when I use

var id = $('#id2').val();

... instead of

var id = $(idSource).val();

... I am not experiencing this lag.

Anyone got an idea what I am doing wrong?

cptmeti
  • 11
  • 1
  • 1
    What do you mean exactly with 'lag'? Where is the delay you are seen? I'm not native English speaker, but your problems seems to be more related with logic and not with time... – Sourcerer Jan 16 '21 at 19:47
  • There appears to be `PHP` in use too? Typically, the ID would not be an editable field. I would not show an input for this myself, the ID is in the button text. (The parent div has the id for back-end reference) Anyway, with Ajax use, I would think that `
    ` tags would be unnecessary as the update buttons also have a jQuery click handler. Consider the combination: clicking an update button submits the form (one update), along with the Ajax post (another update), assuming that `aiai.php` updates the same values too. If the `
    ` tags are removed, change the button to have `type="button"`
    – Paul T. Jan 16 '21 at 20:10
  • @PaulT. that totally worked, thanks a lot! I removed the `
    ` and changed the `submit` into a `button`. And of course decreased the parent where I get the ID from. Regarding the ID, you're right and I am not doing it this way in the actual project.
    – cptmeti Jan 17 '21 at 08:59
  • @Sourcerer totally might be a logical problem. What Paul wrote works, but I don't really understand why :) Anyhow, what I meant by lag is that every other time I update that entry without refreshing the page I don't see the new information, but the old one instead. It's like it is cached or something. But then again sometimes it would work, 100% works on page refresh and I also don't seem to get an error message. Didn't know how to describe it better, not a native speaker myself... – cptmeti Jan 17 '21 at 09:04
  • Obviously, your page is cached. You are not changing the URL and query parameters, and your server is answered with the cached one. Are you using PHP Server in development mode with the aiai module or is it deployed in a server? For a quick test, you can use the trick of adding something new to the URL (https://stackoverflow.com/questions/367786/prevent-browser-caching-of-ajax-call-result) – Sourcerer Jan 17 '21 at 11:18
  • `What Paul wrote works, but I don't really understand why...` to which part(s)? I don't mind explaining. – Paul T. Jan 18 '21 at 00:36

0 Answers0