0

This is really driving me nuts... ;)

I have a table an in each row a Delete button. Here is the Code:

while( $row=mysqli_fetch_row($result) ){
        echo'<tr id="'.$row[0].'" >';
        echo'<td id="colx">'.$row[1].'</td>';
        echo'<td id="CellTextBeschreibung">'.$row[5].'</td>';
        echo'<td id="CellTextLong">'.$row[3].'</td>';
        echo'<td id="CellTextLat">'.$row[4].'</td>';
        ?>
        <td><input type="button" onclick="ShowOnMap( <?php echo $row[0];  ?> )" value="Zeigen"></td>
        <td ><?php echo( $row[6]); ?></td>
        <td>
            <FORM id='deleteform' action='delete.php' method='post'>
                <INPUT type='hidden' name='mapid' value='<?php echo( $row[0]); ?>'>
                <INPUT type='hidden' name='userid' value='<?php echo( $row[2]); ?>'>
                <INPUT type='hidden' name='aktuuserid' value='<?php echo( $userID); ?>'>
                <INPUT id='subdel' type='submit' OnClick="return confirm('Löschen.Bist Du sicher?');" value='Löschen'>
            </FORM>
        </td>   
        <td id="colx"> <?php echo( $row[7]); ?>  </td>
        <td id="colx"> <?php echo( $row[8]); ?>  </td>
        </tr>
        <?php
    }

I'm trying to cach the Submit Button (FORM) with this Jquery code:

$("#subdel").click( function() {
    $.post( $("#deleteform").attr("action"), $("#deleteform").serializeArray(), function(info){alert(info); document.location.reload(true); });
    $("#deleteform").submit( function() {
        return false;   
    });
});

But it's not working... ... it loads the "delete.php" page and it is not returning to the origin page...

Although I have the SAME Code for SAVING (but not in a Table), this is not working...

I would appreciate any Help on this... Thank you in advance... :)

p.s.: Here is the working SAVE Function:

<FORM id='eingabeform' action='save.php' method='post'>
        <INPUT type='hidden' name='userid' value='<?php echo( $userID); ?>'>
        <INPUT type='hidden' name='username' value='<?php echo( $aktuuser); ?>'>
        <INPUT type='hidden' id='inputicon' name='icon' value='1'>  
        <INPUT type='hidden' id ='long' name='long' maxlength='20' >
        <INPUT type='hidden' id ='lat'  name='lat' maxlength='20' >
        <div>Beschreibung: <INPUT type='text' id ='longlattext'  name='longlattext' maxlength='300' ></div>
        <div>Link (bei Bearf):<INPUT type='text' id ='link'  name='link' maxlength='300' ></div> 
    <br>
        <INPUT id='submit' type='submit' value='Speichern'>     <INPUT id='abbrechen' type='Button' value='Schließen'  onclick="Hide('Lmenuinput')">
    </FORM>

and the associated Jquery code:

$("#submit").click( function() {
    $.post( $("#eingabeform").attr("action"), $("#eingabeform").serializeArray(), function(info){alert(info); if (!info=="Du musst auf der Karte einen Punkt anklicken" || !info=="Das ist kein Link! Check das bitte mal, ThX.. ;)") {document.location.reload(true); }});
    $("#eingabeform").submit( function() {
        return false;   
    });
});

As you can see, it's identical... and this one is working... the only difference is, that the above DELETE FORM is multiplied within the table, the SAVE FORM not... could that be related to this error??

here is the delete.php:

<?php

        include 'DBconn.php';
         // ******* TabelleKopf speichern *****************************
        $sql="DELETE FROM `map` WHERE mapID=".$_POST[mapid];
        if (!mysqli_query($con,$sql))
        {
            die('Fail: ' . mysqli_error($con));
        }
        echo "Success";
        mysqli_close($con);
    }

?>
JCgirl
  • 15
  • 5

2 Answers2

1

<input type="submit" /> will submit the form (and update the page) if you click it by default. So, if you want to prevent it from going to another page, just use another type of button: <input type="button" /> or <button /> or prevent the default event with e.preventDefault() implemented in jQuery:

$("#subdel").click( function(e) {
    $.post( $("#deleteform").attr("action"), $("#deleteform").serializeArray(), function(info){alert(info); document.location.reload(true); });
    e.preventDefault();
});

UPDATE: And I've just noticed that you use same IDs for HTML elements inside of a PHP loop. It means that you will have multiple elements having the same ID. It's very wrong. That's why it's not working. When there are multiple same-ID elements, jQuery will select only the first one! So your event handlers will work with only the first form. You may test a very simple example here: http://jsfiddle.net/AEm8B/

To make things work you may use classes instead of IDs. I will place here an example of your form refactored, but IDs should be changed to classes in the whole loop, because it semantically wrong: IDs should be unique.

<FORM class='deleteform' action='delete.php' method='post'>
                <INPUT type='hidden' class='mapid' value='<?php echo( $row[0]); ?>'>
                <INPUT type='hidden' class='userid' value='<?php echo( $row[2]); ?>'>
                <INPUT type='hidden' class='aktuuserid' value='<?php echo( $userID); ?>'>
                <INPUT class='subdel' type='submit' OnClick="return confirm('Löschen.Bist Du sicher?');" value='Löschen'>
</FORM>

And your jQuery code:

$(".subdel").click( function() {
    var $form = $(this).parents(".deleteForm");
    $.post( $form.attr("action"), $form.serializeArray(), function(info){alert(info); document.location.reload(true); });
});

// If you want use this construction instead of e.preventDefault()
// it's better bind it outside of click handler if you're not going 
// to submit it with traditional means.
$(".deleteform").submit( function() {
   return false;   
});*/

or with e.preventDefault (could be more preferable):

$(".subdel").click( function(e) {
        var $form = $(this).parents(".deleteForm");
        e.preventDefault();
        $.post( $form.attr("action"), $form.serializeArray(), function(info){alert(info); document.location.reload(true); });
    });

This should make it finally

Boris
  • 189
  • 4
  • Hi Boris, Thanks for answering... :) unfortunately this is not working... if i put an "alert("klicked"); in the Function, it will not fire up. It is like this Function doesen't exist. As mentioned in my first post, I have the same Function for saving: – JCgirl Apr 19 '14 at 09:28
  • ... and the Saving Function is working... the only difference ist that it is not multiplied in a table... could that be the reason?? p.s.: FYI: >>>>$("#eingabeform").submit( function() { return false; }); <<<<< is preventing from reload... I mean it should do it like in the "save" Function.. i'll post the Save Function also: – JCgirl Apr 19 '14 at 09:35
  • I've reviewed you code one more time and found out the detail which made things not working. Please, see my update to the answer – Boris Apr 19 '14 at 12:18
  • Ok, Thanx Boris... now it seems to react to the function... GREAT!!! ;))) I still have some issues (after submit it prints out the whole Javascript script), but now I can make new thinking steps, because i didn't know the ID <> Class issue... I will come back soon... THANKS AGAIN!! ;) – JCgirl Apr 19 '14 at 12:41
  • Nevermind, could you please mark it as a working answer? Thanks in advance :) – Boris Apr 19 '14 at 12:45
  • Of course...;) As soon as I find out, why it is printing out (alert) the whole php page (not the JS as assumed) so I can put a correct Solution to my problem here... ;) – JCgirl Apr 19 '14 at 13:21
  • That is connected with function(info){alert(info); document.location.reload(true); } your put as a success handler to $.post. The server response (info argument for alert() func.) for your post is the whole page. If you want something else being alerted, you should change it. – Boris Apr 19 '14 at 15:05
  • Arghh... still struggling with this. ;( yes, I know that function(info){alert(info); should give the Informations of the "delete.php" back, as it does without $.post... When I switch off the jquery Function, then I get a new Page (delete.php) with a echo "Success" or "Fail"... but now I get the Whole code text of the primary PHP, NOT the delete.php... I'm stuck again... ;( I'll post the delete.php in my first post... thx for any Help... ;) – JCgirl Apr 19 '14 at 15:48
  • Maybe it's because of document.location.reload(true); ? It makes refresh of the current page after it alerts result – Boris Apr 19 '14 at 15:58
  • If I strip it down to: $.post( $form.attr("action"), $form.serializeArray()); It will NOT fire up delete.php... If I then deactivate the Jquery Function, it will fire up the delete.php with the Success and Fail Messages... I have no clue... – JCgirl Apr 19 '14 at 16:06
  • AAHH!!!! My fault... ;))) I copied your Code, and "var $form = $(this).parents(".deleteForm");"... it should be ".deleteform"... thats because of my lousy programming style... yours is absolutely correct... ;) I can't thank you enough for helping me... THHAAANNK YOOUU!!! You saved my Day... ;) – JCgirl Apr 19 '14 at 16:11
0

You don't need to use .submit inside a .click

$("#submit").click( function() {
    $.post( $("#eingabeform").attr("action"), $("#eingabeform").serializeArray(), function(info){alert(info); if (!info=="Du musst auf der Karte einen Punkt anklicken" || !info=="Das ist kein Link! Check das bitte mal, ThX.. ;)") {document.location.reload(true); }});

});

$("#eingabeform").submit( function(e) {
   e.preventDefault();
});

ps: e.preventDefault(); is better than return false;, see event.preventDefault() vs. return false

Community
  • 1
  • 1
Tib
  • 2,553
  • 1
  • 27
  • 46