penisland Posted November 17, 2013 Share Posted November 17, 2013 (edited) I have this html code in a .php document: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <?php include 'UserIP/ipFunctions.php'; //Logs client's IP logIP(); //List of and invokes banned IPs bannedIPs(); ?> <title><?php print "Your IP address is ". $_SERVER['REMOTE_ADDR'];?></title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script> <script type="text/javascript" src="script.js"></script> <link rel="stylesheet" type="text/css" href="main.css"/> <script> function banUser() { <?php addIPToBannedList(); ?> alert("You have been banned!"); window.location.reload(); } </script> <script> $(function() { $( "#dragable" ).draggable(); }); </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-39055717-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </head> <body> <div id="banner1"> <a href="http://www.brevin.com/lawless"><div id="banner"></div></a> </div> <div id="main"> <h1 id="main_heading">Welcome to Brevin.com, Jim</h1> <p id="notUsername">Not correct name? Click <a href="javascript:resetUsername();">HERE</a> to change</p> <p id="notUsername">Do you want to get banned from Brevin.com? Click <a href="javascript:banUser();">HERE</a> to ban yourself!</p> <div id="pixel"></div> <div id="dragable" class="ui-widget-content"> <img src="tubaland.jpg" width="792" height="612" /> </div> </body> </html> I have a link (line 59) that calls a JS function banUser(). It will do what I want to when I click the link, but the PHP code (line 26) gets excecuted on page load. I only want it executed when I click the link. How would I do that. Edit: Added line numbers Edited November 17, 2013 by penisland Quote Link to comment Share on other sites More sharing options...
Solution MDCode Posted November 17, 2013 Solution Share Posted November 17, 2013 You will have to make an ajax call. PHP is a server side language and thus will not be able to be executed once the file is done loading. Quote Link to comment Share on other sites More sharing options...
KaiSheng Posted November 18, 2013 Share Posted November 18, 2013 Yes i agree that php is server side. However, you can query 2 times. 1 after the other, it will load the first one followed by the second one even though its preprocess. 2 query with 2 php open and close tags. <?php .... ?> <?php .... ?> it works, i tried it before. Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 18, 2013 Share Posted November 18, 2013 Yes i agree that php is server side. However, you can query 2 times. 1 after the other, it will load the first one followed by the second one even though its preprocess. 2 query with 2 php open and close tags. <?php .... ?> <?php .... ?> it works, i tried it before. I'm confused on the logic behind that. PHP is a server-side language so it will be processed no matter how many times you open and close. The only way to use it with JavaScript is to call another file, or the same file with a "if is JavaScript call" conditional. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.