views:

239

answers:

2

I attempted to ask this question last week without a resolution. I am still unable to get this to work. What I would like to do is submit data entered through a WYSIWYG javascript editor to a JQuery script I have that will first tell the user if they are trying to submit an empty textbox The last thing I need it to do is tell the user if their data was entered successfully or not.

I am having a problem inside the JQuery script as nothing is being executed when I click the save button.

This editor uses javascript submit() that is tied to a small save icon on the editor. When the user presses the button on the editor, it fires the function I have in the form tag. That's about as far as I was able to get.

I think there is an issue with the form tag attributes because when I click anywhere on the editor, the editor jumps down off the bottom of the screen. I believe it has something to do with the onclick event I have in the form tag.

The first part of the JQuery script is supposed to handle form validation for the textarea. If that's going to be really difficult to get working, I'd be willing to let it go and just handle everything server side but I just need to get the data POSTed to the JQuery script so that I can send it to my php script.

Thanks for the help guys.

<form name="rpt" class="rpt" id="rpt" action="" onclick="doSave(); return false;">


function doSave()
{
  $(function()
  {
    $('.error').hide();
    $(".rpt").click(function()
    {
      $('.error').hide();
      var textArea = $('#report');
      if (textArea.val() == "")
      {
        textArea.show();
        textArea.focus();
        return false;
      }
      else
      {
          return true;
      }
     var dataString = '&report='+ report;
     alert (dataString);return false;

      $.ajax({
          type: "POST",
          url: "body.php?action=customer",
          data: dataString,
          dataType: 'json',
          success: function(data){
              $('#cust input[type=text]').val('');
              var div = $('<div>').attr('id', 'message').html(data.message);
              if(data.success == 0) {
                  $('#cust input[type=text]').val('');
                  $(div).addClass('ajax-error');
              } else {
                  $('#cust input[type=text]').val('');
                  $(div).addClass('ajax-success');
              }
              $('body').append(div);
            }
      });
      return false;
    });
  });
}
A: 
function X() {
    $(function() {
        //... things you want to do at startup
    });
};

Doesn't do what you want. Nothing is ever executed, because you never tell it to be executed.

You might want to try something like:

<script type="text/javascript">
    jQuery(function($) {
        //... things you want to do at startup
    });
</script>

Also, you want the onsubmit event of the form. You can use

$('#theform').submit(function() {
    //... perform the ajax save
});

In addition, you may want to look into the Form plugin.

Justice
I have a javascript tag in my html. Is this what you are talking about?
This calls the JQuery script
+2  A: 

There are a few things you need to change. Firstly this:

<form name="rpt" class="rpt" id="rpt" action="" onclick="doSave(); return false;">

isn't the jQuery way. Plus its not the click() event you want. Do this:

<form name="rpt" class="rpt" id="rpt" action="">

<script type="text/javascript">
$(function() {
  $("#rpt").submit(do_save);
});
</script>

The construction:

$(function() {
  ..
});

means "when the document is ready, execute this code". It is shorthand for and exactly equivalent to the slightly longer:

$(document).ready(function() {
  ..
});

This code:

$("#rpt").submit(doSave);

means "find the element with id 'rpt' and attach an event handler to it such that when the 'submit' event is executed on it, call the do_save() function".

And change doSave() to:

function doSave() {
  $('.error').hide();
  $(".rpt").click(function() {
    $('.error').hide();
    var textArea = $('#report');
    if (textArea.val() == "") {
      textArea.show();
      textArea.focus();
      return false;
    } else {
      return true;
    }
    var dataString = '&report='+ report;
    alert (dataString);return false;

    $.ajax({
      type: "POST",
      url: "body.php?action=customer",
      data: dataString,
      dataType: 'json',
      success: function(data){
        $('#cust input[type=text]').val('');
        var div = $('<div>').attr('id', 'message').html(data.message);
        if (data.success == 0) {
          $('#cust input[type=text]').val('');
          $(div).addClass('ajax-error');
        } else {
          $('#cust input[type=text]').val('');
          $(div).addClass('ajax-success');
        }
        $('body').append(div);
      }
    });
  });
  return false;
}

Note: return false is in the correct place now so it actually prevents the form submitting back to the server. action="" just means the form will submit back to its current location so you have to prevent that.

cletus
Hi Cletus, Thanks for the explanation on the code as well as the help! I tried your code but I am getting this.message: Statement on line 3: Undefined variable: do_saveI also removed the do_save() from the form tag and put the function call inside javascript tags at the end of my html page. Any idea what I could have done wrong?
Hey Cletus.. I must be getting a little sharper in my abilities because I found out what was causing the error. I have to change the do_save() in the html documant to doSave without the underscore. That now isn't throwing an error but I still would like to get the querystring passed. :)
Cletus, what I did was put an alert(); just under the function doSave() { but it won't come up. I have everything like you said I should. I am going to go back over it once again to be 100% sure.
Changed the doSave function name problem. Also, the final return false was at th ewrong level. it should be at the outermost level within the submit() event handler so it prevents submission back to the server.
cletus
Cletus, thanks again for the help. I wish I could say that it worked. I copied your changes exactly as you posted them but I still get the same result. I don't see any errors either in the error window and I think I forgot to mention last time, but when I click the submit button, the page is forwarding. Refresh in other words. I can however see the querystring in the blue browser bar at the very top of the screen. I also put an alert just under the doSave() function in the JQuery script and still no alert.
Cletus, Just for fun, I removed everything from the JQ script and put this in: function doSave() {alert('adfadfdfa');}I click the submit button and get nothing. The inline js code is exactly like you wanted in the html as well as the changes to the form tag. It isn't seeing the doSave function for some reason.
Are you using Firefox? If so, look at your error console. I suspect you have other Javascript errors halting execution.
cletus
I put only the doSave function back into the jquery file and there are no errors in the FF error window. I used the console as well and didn't see any errors.
When I hit the submit icon, the page is forwarding to a blank screen.
Cletus, not sure if you are still around but I have stripped everything out of the html file and left only the inline js tags that point to jq, the editor and the the new jq file that holds nothing more than the doSave function and I still get nothing. I'm stumped. Here is the query string in the URL bar after I press the submit icon on the editor