[jQuery] Re: Convert from AjaxRequest to jQuery/ajax

2009-02-08 Thread pedalpete

I believe the code you are looking for would be something like this

[code]
$('form').submit(function(){
var cell1 = $('#cell1').html();
var div1= $('#div1).html();
var span1=$('#span1).html();
$.ajax({
 type: GET,
 url : 'urlToGet.php',
 data: 'cell1='+cell1+'div1='+div1+'span1='+span1,
 success: function(response){
 // whatever you want to do with the response
}
});

});
[/code]
On Feb 8, 4:23 pm, Tim Johnson t...@johnsons-web.com wrote:
 For some time I've been using a little ajax library called AjaxRequest.
 Because I'm working with code generation tools, I'd like to make a
 first - step switch to jQuery as simple as possible.
 A sample AjaxRequest function follows:
   function CheckForm0(theform) {
         AjaxRequest.submit(
           theform
         ,{
           'onSuccess':function(req){
                var res = req.responseText.split('\n')
                document.getElementById(cell1).innerHTML = res[1];
                document.getElementById(div1).innerHTML = res[2];
                document.getElementById(span1).innerHTML = res[3];
                alert(res[0]);
                },
           'onError':function(req){ alert(ERROR:  + req.statusText);}
           }
         );
         return false;
   } // end function
 // Form tag follows:
 form method=post
 action=http://bart.johnson.com/cgi-bin/baker/reb/test-ajax.r;
 onsubmit=return CheckForm0(this);
 // Note that method, action, and onsubmit parameters are in the tag
 // and I would prefer that they remain there. Also the form is referenced
 // by 'this' in the onsubmit parameter and passed to the Ajax function.

 I'd appreciate pointers to jQuery examples that would help me with the
 transition. I'd like an approach that requires as few changes to the form
 tag as possible.

 Thanks
 Tim


[jQuery] Re: Convert from AjaxRequest to jQuery/ajax

2009-02-08 Thread Tim Johnson

On Sunday 08 February 2009, pedalpete wrote:
 I believe the code you are looking for would be something like this
Hi pedalpete:
Thanks you for the quick response.
 [code]
 $('form').submit(function(){
 var cell1 = $('#cell1').html();
 var div1= $('#div1).html();
 var span1=$('#span1).html();
 $.ajax({
  type: GET,
  url : 'urlToGet.php',
  data: 'cell1='+cell1+'div1='+div1+'span1='+span1,
  success: function(response){
  // whatever you want to do with the response
 }
 });
As you see, the type and url are hard coded into the function.
Furthermore, if I understand the interface to the function, 'form'
as also hardcoded

In the case of the example that I submitted, the form is passed
as the 'this' keyword, allowing this function to have more than one caller.
Thanks again, more examples are welcome, but perhaps I would have
to dig deep and learn to write a wrapper.

cheers
tim
 });
 [/code]

 On Feb 8, 4:23 pm, Tim Johnson t...@johnsons-web.com wrote:
  For some time I've been using a little ajax library called AjaxRequest.
  Because I'm working with code generation tools, I'd like to make a
  first - step switch to jQuery as simple as possible.
  A sample AjaxRequest function follows:
    function CheckForm0(theform) {
          AjaxRequest.submit(
            theform
          ,{
            'onSuccess':function(req){
                 var res = req.responseText.split('\n')
                 document.getElementById(cell1).innerHTML = res[1];
                 document.getElementById(div1).innerHTML = res[2];
                 document.getElementById(span1).innerHTML = res[3];
                 alert(res[0]);
                 },
            'onError':function(req){ alert(ERROR:  + req.statusText);}
            }
          );
          return false;
    } // end function
  // Form tag follows:
  form method=post
  action=http://bart.johnson.com/cgi-bin/baker/reb/test-ajax.r;
  onsubmit=return CheckForm0(this);
  // Note that method, action, and onsubmit parameters are in the tag
  // and I would prefer that they remain there. Also the form is referenced
  // by 'this' in the onsubmit parameter and passed to the Ajax function.
 
  I'd appreciate pointers to jQuery examples that would help me with the
  transition. I'd like an approach that requires as few changes to the form
  tag as possible.
 
  Thanks
  Tim




[jQuery] Re: Convert from AjaxRequest to jQuery/ajax

2009-02-08 Thread pedalpete


Sorry Tim, I didn't understand it that way.

You should still be able to do this fairly simply.

You don't have to specify the url, you can pass it as a variable.
But you would need to put your $.ajax function into another function,
and call it when clicked. You can also build the data variables in the
other functions.

For instance
[code]
$('form#1').submit(function(){
   var url=$('form#1).attr('action');
   // then get your data variables and do whatever you want to do with
them
var type= get;
submitForm(url, data);

});

function submitForm(url, data){
$.ajax({
 type: type,
 url: url,
 data: data,
  success: function(){

}
})

}

[/code]

I hope that helps, and that I understand what you were trying to do
better than my first answer.


[jQuery] Re: Convert from AjaxRequest to jQuery/ajax

2009-02-08 Thread Tim Johnson

On Sunday 08 February 2009, pedalpete wrote:
 Sorry Tim, I didn't understand it that way.
 :-)
 You should still be able to do this fairly simply.
 Looks like I will also have to use a wrapper to get the form
 using the 'this' keyword, since I work with multiple forms.
 I think I can get what I need by something like this:
 Illustrated by:
   function CheckForm(f) { // triggered by onsubmit=CheckForm(this);
 var formname = f.name;
 var url = f.action;
 var meth = f.method;
 // illustrated by
 alert('Action: ' + f.action + '\nMethod ' + f.method + \nName:  +
   f.name);
 // I'll review your examples, this gives me a start
 // more code ..
  return false;
  }
  //Thanks
  //tim
 You don't have to specify the url, you can pass it as a variable.
 But you would need to put your $.ajax function into another function,
 and call it when clicked. You can also build the data variables in the
 other functions.

 For instance
 [code]
 $('form#1').submit(function(){
var url=$('form#1).attr('action');
// then get your data variables and do whatever you want to do with
 them
 var type= get;
 submitForm(url, data);

 });

 function submitForm(url, data){
 $.ajax({
  type: type,
  url: url,
  data: data,
   success: function(){

 }
 })

 }

 [/code]

 I hope that helps, and that I understand what you were trying to do
 better than my first answer.