Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Newbie Prototype Question: Serialize With Other Parameters
Old 05-14-2008, 12:47 PM Newbie Prototype Question: Serialize With Other Parameters
Average Talker

Posts: 28
Trades: 0
I'm just jumping into Prototype, and one of the first things I'm running into is a small issue with passing parameters in an Ajax.Request.

I am essentially placing a small feedback box on the bottom of every page in my site, during our testing phase. Simple textarea with a submit button. Passing the parameters to the page that does the database insertion, I want to pass the value of the textarea, as well as the URL of the page that is calling it.

The script to send the page address is:
parameters: {url: location.href}
The script to send the feedback is:
parameters: $('txtFeedback').serialize(true)


How do I put the two together?

This approach seems to overwrite the first parameter hash with the second:
parameters: $('txtFeedback').serialize(true),
parameters: {url: location.href}

This approach seems to break it:
parameters: {$('txtFeedback').serialize(true), url: location.href}

I'm sure this is an easy one.

Cheers,
Cory
Cory Dee is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-14-2008, 02:20 PM Re: Newbie Prototype Question: Serialize With Other Parameters
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Simply do an concatenation of the textarea value on the param url.
This is taken from an old project, at the time when I was using prototype.
Here, I had _elemId and _note passed as variable to a function.
Simply use something like this:
Code:
var pars="var1=blah&var2=blih&textarea="+$('textarea').value+"&some=thing";
Code:
var url="/libs/xml/elemRating.php";
var pars="action=setRate&elemId="+_elemId+"&note="+_note+"&hash="+Math.random();
var myAjax = new Ajax.Request(
  url, 
  {
    method: 'post', 
    parameters: pars,
    onComplete: function(r){
      if(r.responseText!='NOK'){
        var _size=15*r.responseText;
        $('currentRating').style.width=_size+"px";
        $('currentRatingTxt').innerHTML=r.responseText;
        
        var _nbOrig=parseInt($('nbVotes').innerHTML);
        if(isNaN(_nbOrig)){
          _nbOrig=0;
        }
        $('nbVotes').innerHTML=_nbOrig+1;
      }
      else{
        //alert('Thank you for voting!');
      }
    }
  });
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 05-14-2008 at 02:23 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-14-2008, 03:29 PM Re: Newbie Prototype Question: Serialize With Other Parameters
Average Talker

Posts: 28
Trades: 0
They've actually updated the framework to take a hash array now, and supposedly it's best practice to use that over the ampersand delimited format. The solution I came up with is the same idea as what you're saying, though:

parameters: {feedback: $('txtFeedback').getValue(), url: location.href}

I guess great minds think alike
Cory Dee is offline
Reply With Quote
View Public Profile
 
Old 05-14-2008, 07:58 PM Re: Newbie Prototype Question: Serialize With Other Parameters
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Like I said, I don't use prototype anymore.
I'm more used to pure DOM coding, and at the time, I ran in "features" that I've found irritating, letting me use the "X library" [ http://cross-browser.com/ ] as a foundation.

Works nicely, and let me do what I need the way I want to do it.
I have to admit that I'm not very fond of the prototype syntax.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-15-2008, 07:46 AM Re: Newbie Prototype Question: Serialize With Other Parameters
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
You can also append them onto the url

Code:
var myAjax = new Ajax.Request(
  url + "?" + "var1=blah&var2=blih&textarea="+$('textarea').value+"&some=thing ,
Though the params{} is desireable

Ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 05-15-2008, 08:09 AM Re: Newbie Prototype Question: Serialize With Other Parameters
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
You can also append them onto the url
But only for a get request. For a POST request, you need to pass them in the "parameter:" parameter
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-15-2008, 08:23 AM Re: Newbie Prototype Question: Serialize With Other Parameters
Average Talker

Posts: 28
Trades: 0
Quote:
Originally Posted by tripy View Post
...I don't use prototype anymore.
I'm more used to pure DOM coding...

This was my first instinct when I started trying to do AJAX. Who needs all that overhead for features that you probably won't use 90% of? I had one piece working perfectly, but throw in another piece somewhere on the page and all hell would break loose - that's why I ended up caving.

Have you had good experiences with X? I had never heard of it until just now.
Cory Dee is offline
Reply With Quote
View Public Profile
 
Old 05-15-2008, 09:26 AM Re: Newbie Prototype Question: Serialize With Other Parameters
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Who needs all that overhead for features that you probably won't use 90% of?
Exactly my thought.
And do you tried to look at prototype sources ?
Unreadable, except for a few wizard that must live in other spheres of existences....

I had really good experiences with X. It stick close to the original DOM functions, and I feel perfectly at home with it.
I discovered it in my last job, where the management had choose to use it because of it's readability.
It just squish down those differences between browser when it comes to events, for examples. I consider it more as a handy set of functions than a framework.

As it's more low level than moo, jquery and the like, it passed mostly unseen, but I find this situation sad.
This framework merit a bit more attention, in my opinion.

Go take a look at the demo, and you can even see the source of each functions, with the dependencies.
I sometime just pick one, and left the rest out.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-15-2008, 10:32 AM Re: Newbie Prototype Question: Serialize With Other Parameters
Average Talker

Posts: 28
Trades: 0
Yea, the site was written that way, sort of "pick and choose what you want, leave the rest out." That fits my style perfectly, as I tend to link to toolbox type of files that do specific tasks, more so than an "everything in one file" type of approach.

I will definitely be reading into it over the next week or so.

Thanks for the headsup.
Cory Dee is offline
Reply With Quote
View Public Profile
 
Old 05-20-2008, 06:52 PM Re: Newbie Prototype Question: Serialize With Other Parameters
Junior Talker

Posts: 1
Trades: 0
Unfortunately, it is not currently possible to pass nested Hashes to the Ajax Parameters (see this bug http://dev.rubyonrails.org/ticket/7494 for prototype). There's been some work done on it, but from what I've read, the patches have not been incorporated into the Prototype codebase. The toQueryString() function simply doesn't process nested hash objects.

The workaround is to serialize to a string (not preferred, but the only way it will work in this case it seems).
markds75 is offline
Reply With Quote
View Public Profile
 
Old 05-26-2008, 12:19 PM Re: Newbie Prototype Question: Serialize With Other Parameters
Average Talker

Posts: 28
Trades: 0
Thanks for that Mark.
Cory Dee is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Newbie Prototype Question: Serialize With Other Parameters
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.52131 seconds with 12 queries