Jump to content

Jquery/ajax practices


dianikol85

Recommended Posts

Hi to all. I want your opinion about a jquery practice

 

So here is the thing

 

I have a database and one table lets call it test_table and it has three fields : id, lastName, firstName.

 

In the index.php ilets say we create a table

 

| id | First Name | Last name | +/-

 

and three button:

1) Add new row

2) Save the row

3) Update the row

 

So far so good. When i click the "Add new row" button via jquery we create a new row at the end of the table with 4 fields which contain a counter for the Id, two text inputs for the names and the last one contans two buttons the "Save the row" and

the "Update the row". When the rows has been added i hide the "Update the row" button. So i fill the text inputs with data and then i click "Save the row" and with ajax i insert the data in the database. so when the insert action has completed i hide th e "Save the row" button and show the "Update the row" button. In order to do the update for this particular row when i did the insert i echo back the max id of the table and save it in a hidden input somewhere in the added row so i can access it with $(this).

 

This process can be repeated for every new row when i click the Add button.

 

 

What do you think about this technique. Do you suggest something more efficient than that? i would be very happy if you have a better solution although mine works very well i think.

 

 

Thank you in advance and looking forward for your replies :blink::D

Link to comment
Share on other sites

- ID should be generated automatically by the database's "autonumber" field. You shouldn't need a counter for that. You only need ID's when updating a row.

 

- Don't forget to attach a random string as a querystring to your AJAX url so the browser doesn't cache the result, and returns "Success" everytime even if there was an error. Also good practice to "Disable" any buttons during the AJAX submission to prevent any double-entries.

 

- Think it would be nice to update the current table with newly added records once they've been inputted. Maybe another AJAX page that returns all records as "<table>" in html format, or just an array/json can do the job as well.

Link to comment
Share on other sites

- ID should be generated automatically by the database's "autonumber" field. You shouldn't need a counter for that. You only need ID's when updating a row.

 

- Don't forget to attach a random string as a querystring to your AJAX url so the browser doesn't cache the result, and returns "Success" everytime even if there was an error. Also good practice to "Disable" any buttons during the AJAX submission to prevent any double-entries.

 

- Think it would be nice to update the current table with newly added records once they've been inputted. Maybe another AJAX page that returns all records as "<table>" in html format, or just an array/json can do the job as well.

 

 

1) I use the counter just to have an order to the table. No other reason.

 

2)Disabling the buttons during Ajax is a nice one. ;)

 

3) All the insertions are instantly. So it feels like awindows app.

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...