Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I have this weird problem, and firebug is not showing any error that make things harder to track, whenever I insert new data, it submits twice.

Here's my ajax:
JavaScript
$('#submit').click(function(){
        
        if($('#titleID').val().length <=0 || $('#genreIDID').val().length<=0){
              $("#InsertSuccessPrompt").css({"display":"none"});     
              $("#InsertEmptyPrompt").css({"display":"inline","color":"Red"});
                 return false;    
        }
        else{
              $.ajax({
              type: 'POST',
              url:'http://localhost/AnimeInventory/index.php/maincontroller/insertForm',
              data:{title : $('#titleID').val(),genreid : $('#genreIDID').val()},
              dataType:'JSON',
              success: function(){
            }
          });
           $("#InsertEmptyPrompt").css({"display":"none"});     
           $("#InsertSuccessPrompt").css({"display":"inline","color":"Green"});
           return false;
}
});


Here's my php code:
PHP
<?php $genreList = array('1'=>'Comedy','2'=>'Action','3'=>'Sci-fi');
     
      echo form_open('mainController/insertForm','id="JSInsertEvent"');
      echo '<table><tr><td>';
      echo form_label('Title').'</td><td>';
      echo form_input('title','','id="titleID"').'</td></tr><tr><td>';
      echo form_label('Genre').'</td><td>';
      echo form_dropdown('genreid',$genreList,'','id="genreIDID"').'</td></tr><tr><td>'; 
      echo form_submit('Submit','Submit Button','id="submit"').'</td><td>';
      echo '<span id="InsertSuccessPrompt" style="display:none">Inserted</span>';
      echo '<span id="InsertEmptyPrompt" style="display:none">Please fill up the title box</span>';
      echo form_close().'</td></tr>';
      ?>


Maybe someone here already faced this problem before, Thanks,
Posted

The problem is that your submit button sends the form to the server after onclick handled.
You should add event.preventDefault(); to your code to stop that behavior...
And as you already use jQuery, you may learn about submit() - http://api.jquery.com/submit/[^]
 
Share this answer
 
Comments
KatsuneShinsengumi 14-May-14 14:09pm    
I already tried the submit() instead of click(), it didint work on mine. Are you sure about the event.preventDefault()? I mean, isnt the reason of return false is to do that? thanks
Kornfeld Eliyahu Peter 14-May-14 16:04pm    
Return false at the end of the function does not do that.
You can stop propagation of events with return false only if it's inline, like:
<input type="submit" önclick="doThis(); return false;" />
KatsuneShinsengumi 15-May-14 0:56am    
Oh I see, now I'm using the preventDefault(); it's still doing the same, I also tried to use ('#myForm').on('submit',function());,.. it end up not working at all.
Hey guys I got it now!

I declare my script on the footer and on the insert php file. So the #submit was called twice. So the insert happened twice,.

Thanks for the help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900