tags:

views:

77

answers:

2

Hi, there is a strong possibility that I'm just being stupid here as this is my first attempt at using jQuery's ajax functionality so please bear with me.

$(document).ready(function(){
  $("input.update").click(function(){
    var str = $(this).parent().serialize();
    $(this).parent().parent().block({ message: "<span class=\"loading\"><img src=\"<?php echo $siteUrl ?>/admin/template/images/loading.gif\" alt=\"loading...\" /><p>Updating...</p></span>" });
    $.ajax({
      type: "POST",
      url: "forms/update.php",
      data: str,
      success: function(){
        $("div.edit_box").unblock();
        $("div.edit_box").append("<span class=\"success\">This has been updated!</span>");
      }
    });
   return false;
   });
});

I have tried to get this working for a little while now and can't figure out why it doesn't pick up the form. All it does is just to do a normal php post without throwing any errors... Any help would be appreciated!

Forgot to add that I'm using blockUi on this as well, hence block/unblock.

+2  A: 
("input.update").click(function(){

should be

$("input.update").click(function(){
RioTera
Sorry, there was a mistake there, the $ is actually there already in the script but not in the example for some reason.
phil
are you using some tool to debug the javascript?
RioTera
also, if you want to submit the form via ajax you can use the submit event or the ajax form plugin http://malsup.com/jquery/form/
RioTera
I use FireBug to debug mostly, this doesn't kick up a fuss about anything when submitting. I was hoping to avoid including another script when jQuery's native ajax function should do the job, I would use it as a last resort. I'll look into the submit event.
phil
A: 

Since it seems you're only using the 'success' callback of post you could use the .post method, which is a bit easier on the eyes. Also you can put those block calls inside ajaxStart and ajaxStop. To me it's neater. The $(this).parent().parent().block seemed wrong to me, I change it to reference the same element that is used for unblocking. I'd also be checking the output of the php script, to make sure that whatever you are 'updating' actually updated (just echo from php in xml and you'll see it on your console log)

$(function() {
    // apply click handlers to anchors
    $("input.update").click(function(e){
     // stop normal link click
     e.preventDefault();

     var str = $(this).parent().serialize();

     // send request
     var action = "forms/update.php";
     $.post(action, {data:str}, function(xml) {
      console.log(xml);
      $("div.edit_box").append("<span class=\"success\">This has been updated!</span>");

     })
    });

    // Adds a wait indicator to any ajax requests
    $(document.body).ajaxStart(function() {
       $("div.edit_box").block({ message: "<span class=\"loading\"><img src=\"<?php echo $siteUrl ?>/admin/template/images/loading.gif\" alt=\"loading...\" /><p>Updating...</p></span>" });

    }).ajaxStop(function() {
       $("div.edit_box").unblock();
       $("div.edit_box").append("<span class=\"success\">This has been updated!</span>");

    });
   });
aland
This has done the job thanks! Have you any idea why the original script wasn't working? Also I used the double parent because there are multiple boxes of the same class on the same page and I don't want them all to update their content at once.
phil
I'm not sure exactly, I thought the this.parent.parent thing looked a bit weird but I don't know that it's incorrect. Also, I've never actually used the .ajax call before, so there might be some problem in that.Glad it helped! I'm only just learning ajax/php myself so it's all a bit new.Since you said you're using Firebug you could use console.log($(this).parent().parent()) to see if it's really returning what you think.. I use console.log all the time because my understanding of javascript is a bit shaky.
aland
Also, do you use FirePHP? It's an extension of firebug that lets you output to the console from php! Pretty funky, eg -$firephp = FirePHP::getInstance(true);$firephp->log($_SESSION, "What's happening in my php session?");Just make sure you remove all those calls when you upload the PHP script to production server! :)
aland