Sergio Sergio -4 years ago 144
Ajax Question

AJAX not redirecting to PHP file

I am trying to clone a particular div inside an html file through AJAX and then to a PHP file that would create a new html file with the newly cloned content and save the save it in a temp directory.

My ultimate goal to generate this temp file that I would extract its url and email it to a client. Eventually (after X amount of days), the file would be deleted.

But this is still thinking way ahead... the issue I am running into is that when i click the SEND button, it seems to cycle through the AJAX ok, but doesn't seem to ever hit the PHP file. Here is what I have so far.



$(document).ready(function(){

$("#send").click( function() {
var extract = $("#extract").html();
$.ajax({
type : "POST",
url : "php.php",
data : {id : extract},
success: function(data){
console.log("ok");
}
});
});

});

p, h1, img {
margin: 10px 0;
padding: 0;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="extract">
<h1>Header Content</h1>
<p>Bacon ipsum dolor amet shank pig filet mignon, pastrami hamburger andouille tenderloin sausage.</p>
<p>Flank cupim ribeye beef ribs shoulder. Beef cow frankfurter ball tip capicola turducken porchetta tenderloin pork loin picanha turkey fatback pork.</p>
<p><img src="http://www.placecage.com/300/450" alt=""></p>
<p>Swine tri-tip porchetta pig filet mignon picanha ham hock turkey burgdoggen strip steak flank turducken. Sirloin venison andouille kevin ball tip. </p>
</div>
<button type="button" name="button" id="send">SEND</button>





In my my php.php file (it lives in the directory level as the js file), I simply have an echo "ok"; and not happens on the screen. I get the console log, but no echo. What am I missing?

Thank you,

Sergio

Answer Source

Change your code to

$(document).ready(function(){

   $("#send").click( function() {
      var extract = $("#extract").html();
     $.ajax({
          type : "POST",
          url : "FULL_PATH_OF_PHP_FILE_php.php",
          data : {id : extract},
          success: function(data){
             console.log("ok");
             console.log (data);
          }
       });
   });

});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download