0

Here's my jquery code

<script>
      $(function () {

        $('#deleteform').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'get',
            url: 'delete.php',
            success: function () {
              alert('Worked');
            }
          });

        });

      });
    </script>

And my PHP code (I'm just trying to test it out, so I added a simple function)

<?php
  header("Location: http://www.google.com/");
?>

And nothing happens when I click the button (when the form submit) except that "Worked" alert box. But whatever I put in that PHP file (delete.php), nothing happens. What am I doing wrong? My "delete.php" file will have a script to delete data in a XML file, just in case it changes something. (for now Im trying with a simple php line)


EDIT

The real PHP code that will go in the PHP file is this :

<?php
$xml = simplexml_load_file("signatures.xml");
$name = $_POST['nom'];
$signs = $xml->xpath('//Signature[Nom = "'.$name.'"]');
$xml -> SignaturesParent -> removeChild($signs);
?>

Nothing happens when I try that.

Danimp
  • 99
  • 1
  • 8
  • Ajax will receive the 301 Status from your PHP. What do you want it to do with that? – Twisty Aug 11 '15 at 18:22
  • I'm trying to delete data from a XML file using the code I added in my post Edit. Thanks! – Danimp Aug 11 '15 at 18:24
  • Re: Edit, maybe you should write the changed xml back to a file??? See http://php.net/manual/en/simplexmlelement.asxml.php Redical I know! – RiggsFolly Aug 11 '15 at 18:31
  • Why not add `echo "Child Deleted";` from your PHP at the end. Then in your ajax call, you can look for that. `success: function(data){ if(data == "Child Deleted"){ alert('Worked'); } else { alert("Failed"); };` Plus what @RiggsFolly said too. – Twisty Aug 11 '15 at 18:31
  • 1
    Also where is `$_POST['nom'];` coming from? – Twisty Aug 11 '15 at 18:33
  • The $_POST['nom'] comes from a text field in my html file. What I need is that is the name in the text field (nom) equals to "John" for example, and that I click on the Delete button, it scans the XML file to check if there's a Signature name="John" node in my XML file, and if so, delete it. – Danimp Aug 11 '15 at 18:57
  • self-duplicate / x/y / pressure-repost of: [Deleting a specific node based on dropdown selection in XML](http://stackoverflow.com/q/31945165/367456) – hakre Aug 16 '15 at 14:21

1 Answers1

2

Try this.

The ajax call now alerts whatever is sent to it from the delete.php

The ajax call does a POST and not a GET so that it matches the fact that you are using $_POST[''] and send some data i.e. smith you are going to have to change that to something that actually exists in your XML file

The delete.php actually returns something

The delete.php saves the changed xml document back to disk to a file with a different name, so you can see if it actually did anything. just while you are tesing.

<script>
  $(function () {

    $('#deleteform').on('submit', function (e) {

      e.preventDefault();
      $.ajax({
        type: 'POST',
        url: 'delete.php',
        data: {nom:"smith"},
        success: function (data) {
          alert(data);
        }
      });
    });
  });
</script>



<?php
$xml = simplexml_load_file("signatures.xml");
$name = $_POST['nom'];
$signs = $xml->xpath('//Signature[Nom = "'.$name.'"]');
$xml -> SignaturesParent -> removeChild($signs);

$result = $xml->asXML("signatures2.xml");
echo $result ? 'File Saved' : 'File Not Saved';

?>
Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thanks a lot. But it gives me that error in a alert box:
    Fatal error: Call to undefined method SimpleXMLElement::removeChild() in /PATH TO FILE/delete.php on line 6
    – Danimp Aug 11 '15 at 18:57
  • That was code I assumed you had already checked, but apparently not, so read this to see how to remove an element http://stackoverflow.com/questions/262351/remove-a-child-with-a-specific-attribute-in-simplexml-for-php/16062633#16062633 – RiggsFolly Aug 11 '15 at 19:03
  • Thanks! I'll take a look again at that link to find out whats wrong with my code. – Danimp Aug 12 '15 at 11:10