Jump to content

Call PHP function from Javascript


penisland
Go to solution Solved by MDCode,

Recommended Posts

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 by penisland
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.