tags:

views:

63

answers:

5

Ok basically my problem is what i stated in the title. I'm using jquery tabs and after i submit 1 form and i go about adding another news post instead of doing the ajax submit it just posts to the page.

<script type="text/javascript">

    //if submit button is clicked
    $('#submit').live("click",function () {  
     //Get the data from all the fields
     var title = $('input[name=title]').val();
     var newspost = tinyMCE.get('newspost').getContent();
     var type = $('input[name=type]').val();

     //Simple validation to make sure user entered something
     //If error found, add hightlight class to the text field 

     //organize the data properly
     var data = 'title=' + title + '&post=' + newspost + '&id=' + type;
     //start the ajax
     $.ajax({
      //this is the php file that processes the data and send mail
      url: "pages/news.php", 

      //GET method is used
      type: "POST",

      //pass the data   
      data: data,  

      //Do not cache the page
      cache: false,

      //success
 success: function() {  
     $('#news_form').html("<div id='message'></div>");  
     $('#message').html("<h2>News Article has been Submitted!</h2>")  
     .append("<p>Now visable on main page</p>")  
     .hide()  
     .fadeIn(1500, function() {  
       $('#message').append("<img id='checkmark' src='images/check.png' />");  
     });  
      }  
     });

     //cancel the submit button default behaviours
     return false;
    }); 
 </script>
<div id="news_form">
<form id="form1" name="form1" method="post" action="">
  <p>Title:
    <label>
      <input name="title" type="text" id="title" size="40" />
    </label>
  </p>
  <p>
    <label>
      <textarea name="newspost" id="newspost" cols="45" rows="5"></textarea>
    </label>
  </p>
  <p><input type="hidden" name="type" value="postadd" />
    <label>
      <input type="submit" value="Submit" id="submit">
    </label>
  </p>
</form>
</div>
+1  A: 

Returning false won't prevent the default action; try event.preventDefault instead.

T.J. Crowder
Same thing happend. When i went to submit a second news posting it just posted to the page instead of doing the ajax submit.
Tim H.
Hmmmm, can you post a complete example? Ideally a pared-down one that only has what you need to replicate the problem?
T.J. Crowder
I'll setup a test directory on my site so you can see for yourself..give me a few mins
Tim H.
http://wildhivefarm.com/test username=demo password=demo if anyone cares to see it working....
Tim H.
I was going to delete this answer (as it's wrong), but I'll leave it so the link remains. But see ScottE's answer, I expect he's sussed it.
T.J. Crowder
A: 

For completeness you might consider the following:

event.preventDefault();
event.stopPropagation();
if ($.browser.msie) {
    event.originalEvent.keyCode = 0;
    event.originalEvent.cancelBubble = true;
    event.originalEvent.returnValue = false;
}

Although I'm in a bit af a quandry as I would normally encourage feature detection ablve browser sniffing (if ($.browser.msie) {...)

Also, noticed your first line:

$('#submit').live("click",function () {

Although it probably won't make a difference, I can't see how you require live(). Surely .click() ought to suffice?

James Wiseman
I added that, and now when i go for the second submit it just doesn't do anything.
Tim H.
put what into the body of .fadein ?
Tim H.
A: 

Try changing your submit button to a regular button. You don't need the default submit behavior if you're handling it with AJAX.

<input type="button" value="Submit" id="submit">

Or, if you want to keep it as-is, you should put the return false in your click function, not inside the AJAX success function.

Shawn Steward
Same problem..still stalls after the second one
Tim H.
Are you sure there's no JavaScript errors that are preventing future script from executing?
Shawn Steward
+1  A: 

Have you tried listening for the .submit() event on the form itself?

Also, make sure you don't have duplicate ids on the page.

ScottE
Ack! I missed that! It's hooked to the submit button's click event. That's got to be the problem.
T.J. Crowder
so would it $('#form1').submit( function ?
Tim H.
Yup, that's the event.
ScottE
put it in, worked the first time, then when i clicked it for the second time it did what it normally do, just regular post to the page instead of the ajax call
Tim H.
figured it out. I have TinyMCE as an editor..that seems to be messing it up
Tim H.
So it was throwing an error, preventing your scripts from running?
ScottE
A: 

I have tinyMCE installed an my editor..once i removed it i was able to post how ever many times in a row i wanted..

Tim H.