how to run php code onclicking an image [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I run PHP code when a user clicks on a link?
I have an image .
<image name="" src="">
I have a php code that needs to be run only after the image has been clicked.
<?php
$var = somthing;
if(condition)
{
sql stmts;
}
?>
like that.
Both are in the same php page. PLease help me to sort out this problem.
Thanks..

You can send a request with javascript. With jQuery that would look like this:
$.get("yourfile.php?function=imageClick", function(data){});
In your php somewhere at the top add:
if($_GET['function'] == 'imageClick'){
// do your php stuff
}

You can't run PHP code on the client side.
You can do that through an AJAX call: http://www.w3schools.com/ajax/default.asp

Related

Call a php function when an html link is clicked [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I want to call a PHP function when someone clicks an html link. I don't know how to do it. Please help.
Didn't get a code?
<?php
function sendCode(){
//code
}
?>
Do it like this:
Didn't get a code?
<?php
if(isset($_GET['sendcode'])){
sendCode();
}
function sendCode(){
//code
}
?>

Php parameterized function call from html [duplicate]

This question already has answers here:
How to call a PHP function on the click of a button
(13 answers)
Closed 7 years ago.
I have a problem with calling a phpfunction from a onclick() event.
<button onclick=delete($a) >click here</button>
<?php
Function delete($a)
{
----statements----
}
?>
Please help me solving this functiom call
You cannot call a php function from a javascript onclick event that way. php is strictly server side, the only way is to make an ajax call to a script that will run the function and return the result. Once the page is served, all trace of php disappears, there is no reference or anything of php left in the client side.

Use html to call/use php function [duplicate]

This question already has answers here:
Execute PHP function with onclick
(9 answers)
Closed 7 years ago.
I'm having some trouble getting my HTML and php to work together.
I've made a onClick that should run a PHP function i have in the top of the PHP file. But every time i click it, it says: ReferenceError: deleteSub is not defined. Can someone tell me what I've done wrong?
HTML:
<?php
if(mysql_num_rows($sql_select_delete) > 0){
while($row = mysql_fetch_assoc($sql_select_delete)){
?><div class="abo_box">
<label class="abo_navn" id="<? echo $row["id"]; ?>"><? echo $row['abo_name']; ?></label>
<label class="delete" onclick="deleteSub();">Delete</label>
</div>
<?php
}
}
?>
PHP:
function deleteSub(){
echo "deleted";
}
You can't call directly a PHP code like this
What you can do is to create a javascript function and you need to use Ajax witch will send a request to your re server and here you can call your PHP function
Remember : PHP is server side code and HTML, JS is client side code, the only way to make them work together is using HTTP requests.
you could do something like this. But to use server side information you should use AJAX and http requests. This example is just for changing DOM elements.
<p id="demo" onclick="myFunction()">Click me to change my text color.</p>
<script>
function myFunction() {
document.getElementById("demo").style.color = "red";
}
</script>

how to read header data with javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
How to read values from header url in javascript?
For example if I have something like:
www.something.com/details.html?test=something
I need to be able to read this data when I redirect to page details.html.
What would be a good approach for this? Maybe adding some php code to get data?
Are you talking about getting the value from a get variable and using it in javascript? If so I've used something similar to this before:
<script type="text/javascript">
function myfunction(){
var value = "<?php echo $_GET['test']; ?>"
}
</script>
And it's worked beautifully for me.

How to Add PHP Variables in a Jquery Notification [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variables from php to javascript
I am using this awesome jQuery Stick Notification Plugin which creates notifications upon clicking various buttons.
I used the following code to display the notification box...
$(".notify").click(function() {
$.sticky('The page has loaded!');
});
Can i make it display some PHP variables in place of static text message?
Hope i've made it clear enough.
Without making asynchronous HTTP calls, you will have to insert the PHP variable at server-side:
$(".notify").click(function() {
$.sticky('<?php echo htmlentities($message); ?>');
});
Wrap it with htmlentities() just in case $message contains some chars that make the JavaScript string invalid.
yes you can like
$(".notify").click(function() {
$.sticky('<?php echo "The page has been loaded"; ?>');
});

Categories