javascript - Trying to open a php file in background using ajax on link click -


i want open php file in background on link click instead of showing user link php file.

i realize way ajax.

but not working, instead of opening new php page in background instead nothing happens, php code not execute.

my javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script type="text/javascript">     $('a#run-php-script').click(function(e){         e.preventdefault();         $.ajax({             type: 'post',             url: 'http://mywebsiteaa.com/myfile.php?addid=<?php echo $add->id); ?>'         });         return false;     }); </script> 

my html

<a id="run-php-script" href="#">clickit</a> 

please note php script works when put php link html href tag. problem reveals user php file url , won't want user see nor want open in new tab if @ possible instead in background.

i tried get instead of post, still not working.

please thanks


update -- per user request have changed button. had forgotten wrap tags. still doesn't work.

new js

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() {      $('#run-php-script').click(function(){         $.ajax({             type: 'post',             url: 'http://mywebsiteaa.com/myfile.php?addid=<?php echo $add->id); ?>',         success: function(){             alert('success');         },         error: function(){             alert('failure');         }         });         return false;     }); }); </script> 

new html

<input id="run-php-script" type="button" name="somebtn" value="clickit" /> 

when click button still nothing happens. php page not open in background.

i added success code script above. though says "success" popup, still actual php page doesn't open in background , execute, nothing happens.

please thanks

two things going wrong here you:

  • the link has target="_blank" attribute, clicking on open href in new tab, should remove it.
  • the event handler doesn't prevent default action link

you should add preventdefault() call handler if not want browser handle link @ all

e.g.

$('a#run-php-script').click(function(e){     e.preventdefault();     ... }); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -