I''m relatively new at using Prototype ajax requests...and I''m having a difficult time figuring out the best way to "insert" data into the database and then return the inserted row''s PK back to the original page. Can anyone help me out here? I''m developing using .Net. This is an example of what I''m doing: var ajaxinsert = new Ajax.Request(''insertNote.aspx'', { method: ''get'', parameters: ''text=''+note.text+''&width=''+note.rect.width +''&height=''+note.rect.height+''&left=''+note.rect.left +''&top=''+note.rect.top+''&id=''+note.id+''&status=''+note.status, onSuccess: function(transport) { note.status = ''saved''; }, onFailure: function(transport) { note.status = ''failed''; } Basically I''m calling a page "inserNote.aspx" to do my insert into db. This works perfect so far...but I can''t find a good way to retreive the PK of the row I just inserted! I''ve tried using Ajax.Updater but that doesn''t seem to be a good method because I don''t want to make two ajax calls...I just want to make the one request and retreive the inserted ID... I am very very very close to getting my project working. This is my last missing key! Please someone shed some light on how to do this best, thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ken Snyder
2008-Apr-16 19:23 UTC
Re: Prototype Ajax.Request with Retrieving new PK from database
Ftt wrote:> ... > > var ajaxinsert = new Ajax.Request(''insertNote.aspx'', { > method: ''get'', > parameters: ''text=''+note.text+''&width=''+note.rect.width > +''&height=''+note.rect.height+''&left=''+note.rect.left > +''&top=''+note.rect.top+''&id=''+note.id+''&status=''+note.status, > onSuccess: function(transport) { > note.status = ''saved''; > }, > onFailure: function(transport) { > note.status = ''failed''; > } > > Basically I''m calling a page "inserNote.aspx" to do my insert into db. > This works perfect so far...but I can''t find a good way to retreive > the PK of the row I just inserted! I''ve tried using Ajax.Updater but > that doesn''t seem to be a good method because I don''t want to make two > ajax calls...I just want to make the one request and retreive the > inserted ID... > ... > >As long as the ajax request is asynchronous, the browser can make the request to the server, the server can process the request and insert the record then return back the value of the inserted id. Have the aspx page echo the id (or 0 on failure) and define your onSuccess function along these lines: onSuccess: function(transport) { var id = parseFloat(transport.responseText); if (id) { note.id = id; note.status = ''saved''; } else { note.status = ''failed'' } } The onSuccess should be fired after the aspx page is done processing and will receive the output of the aspx page in transport.responseText - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the quick response, Ken! I will try that out and hopefully I''ll be done with my app. You''re a life saver. On Apr 16, 2:23 pm, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ftt wrote: > > ... > > > var ajaxinsert = new Ajax.Request(''insertNote.aspx'', { > > method: ''get'', > > parameters: ''text=''+note.text+''&width=''+note.rect.width > > +''&height=''+note.rect.height+''&left=''+note.rect.left > > +''&top=''+note.rect.top+''&id=''+note.id+''&status=''+note.status, > > onSuccess: function(transport) { > > note.status = ''saved''; > > }, > > onFailure: function(transport) { > > note.status = ''failed''; > > } > > > Basically I''m calling a page "inserNote.aspx" to do my insert into db. > > This works perfect so far...but I can''t find a good way to retreive > > the PK of the row I just inserted! I''ve tried using Ajax.Updater but > > that doesn''t seem to be a good method because I don''t want to make two > > ajax calls...I just want to make the one request and retreive the > > inserted ID... > > ... > > As long as the ajax request is asynchronous, the browser can make the > request to the server, the server can process the request and insert the > record then return back the value of the inserted id. Have the aspx > page echo the id (or 0 on failure) and define your onSuccess function > along these lines: > > onSuccess: function(transport) { > var id = parseFloat(transport.responseText); > if (id) { > note.id = id; > note.status = ''saved''; > } else { > note.status = ''failed'' > } > > } > > The onSuccess should be fired after the aspx page is done processing and > will receive the output of the aspx page in transport.responseText > > - Ken Snyder--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Tobie Langel
2008-Apr-17 03:52 UTC
Re: Prototype Ajax.Request with Retrieving new PK from database
Quick tip: you can pass an object to parameters, Prototype will serialize it for you. var ajaxinsert = new Ajax.Request(''insertNote.aspx'', { method: ''get'', parameters: { text: note.text, width: note.rect.width, height: note.rect.height, left: note.rect.left, top: note.rect.top, id: id, status: note.status }, onSuccess: function(transport) { note.status = ''saved''; }, onFailure: function(transport) { note.status = ''failed''; } }); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the tip! I''ve made the change. I have one last issue that maybe someone can help me with. Once I make the ajax.request, the request doesn''t finish before moving on in my code and returning the wrong note.id. Every new note starts with a note.id = 1. Once the "save" button is clicked performs the ajax.request and save it to the DB. Then I want to return the new PK and change the page''s note.id = newPK. The problem is that I can''t get the ajax.request to finish before moving on through my function. Here''s my updated code that does save correctly and return the new PK correctly. It just doesn''t return it in time. Notice at the bottom I have "return note.id". This still always returns a value of 1. I have in my onSuccess note.id = transport.responseText. This does change the note.id correctly..just not in time! Help again! Thanks guys. (also, I tried changing the onSuccess to onComplete and that didn''t work either.) function saveNoteDb(note){ note.Save(); var url = ''saveNote.aspx''; var ajaxinsert= new Ajax.Request(url, { method: ''get'', parameters: { text: note.text, width: note.rect.width, height: note.rect.height, left: note.rect.left, top: note.rect.top, id: note.id, status: note.status}, onSuccess: function(transport) { if (transport.responseText > 0) { //note saved note.status = ''saved''; note.id = transport.responseText; } else { note.status = ''failed''; } } }); return note.id; } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ken Snyder
2008-Apr-17 14:52 UTC
Re: Prototype Ajax.Request with Retrieving new PK from database
Ftt wrote:> Thanks for the tip! I''ve made the change. I have one last issue that > maybe someone can help me with. Once I make the ajax.request, the > request doesn''t finish before moving on in my code and returning the > wrong note.id.... > >It seems like you''re not quite understanding the asynchronous concept of ajax (without it, ajax would be jax). Any "moving on in your code" needs to be done after onSuccess is called. The "return note.id" at the end of saveNoteDb is wrong. You''ll need to create two functions--see example below. -Ken function saveNoteDb(note){ ... var ajaxinsert= new Ajax.Request(url, { method: ''get'', parameters: {...}, onSuccess: function(transport) { if (transport.responseText > 0) { //note saved note.status = ''saved''; note.id = ''note'' + transport.responseText; } else { note.status = ''failed''; } showNoteSaveStatus(note); } }); } function showNoteSaveStatus(note) { $(note) .addClassName(note.status) .update(note.status.capitalize()); }; // i don''t know your html structure, but in this example: // if saved with an id of 100, the note node would now // have a class of "saved" an innerHTML of "Saved" // and an id="note100" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, I read up some more on prototype and I do understand all the ajax concepts etc. My initial problem is that I was attempting to work off of existing code that wasn''t written right. So that led me down the wrong path. With the help from you guys I''ve now finished my project and tested it pretty thoroughly with no bugs. If you couldn''t already tell I was trying to replicate flikr by using photonotes.js. Well, I had to heavily modify photonote''s to get it working how I wanted it. After a couple days I''ve now got it working so thanks a million. On Apr 17, 9:52 am, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ftt wrote: > > Thanks for the tip! I''ve made the change. I have one last issue that > > maybe someone can help me with. Once I make the ajax.request, the > > request doesn''t finish before moving on in my code and returning the > > wrong note.id.... > > It seems like you''re not quite understanding the asynchronous concept of > ajax (without it, ajax would be jax). Any "moving on in your code" > needs to be done after onSuccess is called. The "return note.id" at the > end of saveNoteDb is wrong. You''ll need to create two functions--see > example below. > > -Ken > > function saveNoteDb(note){ > ... > var ajaxinsert= new Ajax.Request(url, { > method: ''get'', > parameters: {...}, > onSuccess: function(transport) { > if (transport.responseText > 0) { > //note saved > note.status = ''saved''; > note.id = ''note'' + transport.responseText; > } else { > note.status = ''failed''; > } > showNoteSaveStatus(note); > } > }); > > } > > function showNoteSaveStatus(note) { > $(note) > .addClassName(note.status) > .update(note.status.capitalize()); > > }; > > // i don''t know your html structure, but in this example: > // if saved with an id of 100, the note node would now > // have a class of "saved" an innerHTML of "Saved" > // and an id="note100"--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---