0

My insert ajax code will refresh the page but then not alert ("add successful"). Data does echo the right key trigger words ADD_OK i double checked. I have no errors. This seems odd to me because I used the exact same ajax code for both my add and delete.

Code Not Alerting But Adding to database

<script type="text/javascript"> 
    $(document).on('click','#insert_btn',function (){
      $.ajax({
            type:'POST',
            url:'add.php',
             datatype: "json",
            data: {
                first_name: $("#first_name").val(),
                last_name: $("#last_name").val(),
                position: $("#position").val(),
                date: $("#date").val(),
                updated: $("#updated").val(),
            }, 
            success: function(data){ 
               if (data=='ADD_OK') {
                  //  alert(data);  
                  location.reload();
                      alert("Add Successful");
                } else {
                     //alert('something wrong');
                }
                  }
             })
        }); 
</script>

Code Working doing both the delete and alert The same code for delete will refresh page and alert ("delete successful"). What would cause this?

<script type="text/javascript"> 
    $(document).on('click','.delete_btn',function (){
        if (confirm('Are you sure you want to delete this?')) {
        var id = $(this).attr("id").match(/\d+/)[0];
        var del_id = $('#example').DataTable().row( id ).data();
        var del_id = del_id[0];
        $.ajax({
            type:'POST',
            url:'delete.php',
            data: {del_id:del_id}, 
            success: function(data){ 
                if (data=='DEL_OK') {
                  location.reload();
                  alert("Delete Successful!");
                } else {
                 //    alert('something wrong');
                }
                  }
                     });
        }
        });
</script>

Add.php Code

<?php
$first_name = strtoupper($_POST['first_name']);
$last_name = strtoupper($_POST['last_name']);
$position = strtoupper($_POST['position']);
$date = $_POST['date'];
$updated = $_POST['updated'];  
   $stmt = $conn->prepare("INSERT INTO employees (first_name, last_name, position, date, updated) VALUES (?, ?, ?, ?, ?)");
   $stmt->bind_param('sssss', $first_name, $last_name, $position, $date, $updated);
   $add = $stmt->execute();

    //print_r($_POST);

   if($add) {
      echo "ADD_OK";
   }
  ?>

2 Answers2

1

Swap your location.reload() and alert(...) lines.

The alert will only run if the page hasn't started reloading yet. I imagine it takes longer on some pages for a number of reasons.

wheelmaker
  • 2,975
  • 2
  • 21
  • 32
  • unfortunately i had already tried that. I just double checked and did it again with no success. –  Sep 24 '18 at 04:03
  • in my delete ajax i had reload first then alert and that one worked which is puzzling. –  Sep 24 '18 at 04:05
  • 1
    I don't think it should work like that, if the page reloads it will stop processing the javascript from that page and starts the session all over again on the new page load... is your delete still working that way now? – wheelmaker Sep 24 '18 at 04:07
  • https://databasetable-net.000webhostapp.com/ Ya my delete will alert (delete successful) –  Sep 24 '18 at 04:08
  • your logic/theory could be right but it is puzzling why my delete works... –  Sep 24 '18 at 04:09
  • does it still reload? – wheelmaker Sep 24 '18 at 04:18
  • i believe so yes. it adds the data instantly to see. i added my add.php code too in original post. –  Sep 24 '18 at 04:19
  • the only difference between the two is the confirm. guess i will try adding a confirm. –  Sep 24 '18 at 04:29
  • https://stackoverflow.com/questions/16955019/how-to-reload-a-page-after-the-ok-click-on-the-alert-page I probably have to do a confirm box like in that link. –  Sep 24 '18 at 05:10
  • Maybe need to turn ascn to false, on my phone right now so will try when I get home https://stackoverflow.com/questions/23810813/ajax-post-not-working-without-alert-message –  Oct 04 '18 at 22:55
0

I added code for insert data please try and check this. Also, you try this to delete and change your code.

<script type="text/javascript"> 
    $(document).on('click','#insert_btn',function (){
      $.ajax({
            type:'POST',
            url:'add.php',
             datatype: "json",
            data: {
                first_name: $("#first_name").val(),
                last_name: $("#last_name").val(),
                position: $("#position").val(),
                date: $("#date").val(),
                updated: $("#updated").val(),
            }, 
            success: function(data){ 
               if (data.success == true) {
                  //  alert(data);  
                  alert("Add Successful");
                  location.reload();
                } else {
                    alert('something wrong');
                }
                  }
             })
        }); 
</script>
Subhash Patel
  • 674
  • 7
  • 16
  • same as mine. added but then no alert. –  Sep 24 '18 at 06:02
  • Please reload your script with window.location.reload(); in ajax command might help you. – Subhash Patel Sep 24 '18 at 06:09
  • i tried it again after clearing cache now it saying "something wrong" –  Sep 24 '18 at 06:09
  • window.location.reload() got rid of the something wrong error but still no alert. –  Sep 24 '18 at 06:10
  • do i need something like this maybe? ive been trying it out with no success so far: var r = confirm("Successful Message!"); if (r == true){ window.location.reload(); } –  Sep 24 '18 at 06:11
  • You check the data added your table or not. – Subhash Patel Sep 24 '18 at 06:11
  • I checked that Error comes but data added successfully. Please remove your echo from the add.php and check. – Subhash Patel Sep 24 '18 at 06:15
  • commented out echo ADD_OK in add.php file. trying it out now. –  Sep 24 '18 at 06:17
  • well somehow the computer doesnt think i echo out ADD_OK. thats my conclusion. but commenting it out didnt work either. –  Sep 24 '18 at 06:24
  • Please comment if condition code and alert(data) so we check on it whats the problem. – Subhash Patel Sep 24 '18 at 06:24
  • just realized there is an error. says 'ADD_OK' is undefined –  Sep 24 '18 at 06:28
  • alert(data) is turned on says ADD_OK –  Sep 24 '18 at 06:29
  • echo json_encode seems to get rid of the undefined error but still no alert message, –  Sep 24 '18 at 06:32
  • Please try out result method of the success: function(result){if(result == ADD_OK){alert('OK')}else{alert('wrong')}} – Subhash Patel Sep 24 '18 at 06:32
  • new way adds to table but no alert message. I tried using both encode json and the regular echo method too. –  Sep 24 '18 at 06:36
  • https://stackoverflow.com/questions/17895530/bootstrap-modal-ajax-not-working-on-click This could be it. i may have to use a full document.ready(Function() –  Sep 24 '18 at 06:40
  • https://stackoverflow.com/questions/24360626/ajax-success-callback-alert-not-working Maybe I need to change success to complete? On phone will try later. What you think. –  Sep 25 '18 at 13:12
  • Ok, @JeffBezos. I think we try to research on success response message what return in the response to ajax do debug the code to send post data in preserve log. – Subhash Patel Sep 26 '18 at 05:26
  • Yep. I think data response is empty for whatever reason. No clue what’d cause that... –  Sep 26 '18 at 15:50
  • i got it to alert update_ok and something wrong now. i just commended out //something wrong. weird! but guess a half working solution is better than nothing –  Sep 27 '18 at 17:41
  • I may need to set Async to false, will try when I get home. What you think? Would make sense. https://stackoverflow.com/questions/23810813/ajax-post-not-working-without-alert-message –  Oct 04 '18 at 23:39