How to call a php function on a html text click? - javascript

I want to call php function while call that function I need to pass parameter, when click on some text, It has to call the method called test();
How to do this.
I don't know anything about ajax.I have taken this code to try it.To try I have used simple code it is also not working.
This is Ajax.html file
<!DOCTYPE html>
<html>
<head>
<body>
<p>Click Me</p>
<script>
$(document).ready(function() {
$("p").click(function(){
$.ajax({
url:"script.php", //the page containing php script
type: "POST", //request type
success:function(result){
alert(result);
}
});
});
})
</script>
</body>
</head>
</html>
script.php contains,
<?php
function test(){
echo "Hello";
}
?>
how to call php function when user click on some text.where I have to call that test() method. Can Any one help me?

Either you need to call when page called
<?php
test()//mind it
function test(){
echo "Hello";
}
?>
Or use any param like name of action
<?php
if(isset($_GET['action']) && $_GET['action']==1){
test()//mind it
}else{
otherFunc()//mind it
}
function test(){
echo "Hello";
}
?>
Set action in url
url:"script.php?action=1",

Related

Refresh a div with PHP variable using Jquery

I would like to refresh a div every second with a PHP variable using Jquery.
I have a simple PHP file with a variable date:
<?php
$date = date('d/m/Y H:i:s');
?>
I have a HTML file with the following code:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
function request() {
$.ajax({
url: "date.php",
dataType: "text",
cache: false,
success: function(data) {
var json = $.parseJSON(data);
$('#result').html(json.date);
}
});
}
setTimeout(request, 1000);
});
</script>
</head>
<body>
<div id="result">
</div>
</body>
</html>
But the result is a blank page. I can not make it work. I would like your help.
I would like to refresh every second a PHP variable using Jquery.
You need to print date variable:
<?php
$date = date('d/m/Y H:i:s');
echo $date;
?>

PHP: echo as condition of if statament?

I need to put echo as condition of if statement. So, for example:
<html>
...
...
<body>
<form onsubmit"<?php $c=false; if(echo "<script>example();</script>";){ $c=true; } echo "\""; if($c){ echo "action=\"./\"";} method="post">
...
</form>
...
...
<script>
function example()
{
alert("This is only an example");
if(..) return true;
return false;
}
</script>
</body>
</html>
So I need to create a function in JavaScript (this top is only an example, but, in realty, it is more complicated) and call it in the if statement.
Update
The code should be execute "action" of the form only if the Javascript function is true.
PHP runs in the server. JavaScript runs in the client. So php can't use a js variable or its function to server side action. What u can do is , u can call JS function from PHP like
echo "<script> myFunction() </script>";
But in the same same case, u can use php values for JS actions,
eg
<script>
var x = <?php echo $myVar ?>
if(x == true){
alert("yahooo");
}
</script>

How to alert box from ajax php function

I have function frm_trigger_entry_update this is the php function which is run in background i mean this is ajax php function.
In this function i have write some jquery or javascript function which will alert some text message.
In bellow snippet code you can see my function code.
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
}
?>
I have tried bellow snippet logic but its not work for me mean the alert box is not showing after calling this function.
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
?>
<script>
jQuery(document).ready(function($){
alert("my message");
});
</script>
<?php
}
?>
So how can alert the box in this php function any one have any idea.
Use JS and Php Separately.
First ajax call from your JS file:
$.ajax({url: "frm_trigger_entry_update function's Url",
success: function(result) {
alert(result);
}
});
In php Function, from where you should send your message:
function frm_trigger_entry_update($atts) {
echo "my message";
}
Consider following is your ajax call
$.ajax({url: "URL to frm_trigger_entry_update",
success: function(result)
{
alert(result);
}
});
Your PHP function
<?php
function frm_trigger_entry_update($atts)
{
echo "my message";
}
?>
Try this:
echo "<script>alert('test');</script>";
Note : The best practice to do this with Ajax because function is in
server side so you should call it to server from Client using Ajax
Create one file eg: "frm_trigger_entry_update.php"
IN "frm_trigger_entry_update.php" put your function
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
echo $atts;
}
// Call to that function
frm_trigger_entry_update("ATTRIBUTE_VALUE");
Write Ajax on your HTML
$.ajax({
type: 'get',
url: 'PATH to frm_trigger_entry_update.php',
success: function(data) {
alert(data);
}
});
You will get output Alert as ATTRIBUTE_VALUE
In your php function you need to return the output :
<?php
function frm_trigger_entry_update($atts)
{
return "<script>
jQuery(document).ready(function($){
alert('my message');
});
</script>";
}
And then where you want to apply this script you can display the output of your function :
<?php
...
$script = frm_trigger_entry_update($ats);
echo $script;
However in my opinion, this is not a good practice, you should put your javascript in a js function, in a js file and include your js file in your document or call it when needed.
Calling a php function via ajax is really impossible because ajax uses a url for php file and not a php function.
Though your code works and will alert if you call it via php.The code below will alert what ever string you put in the parameter of your function.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
?>
<script>
jQuery(document).ready(function($){
alert("<?php echo $atts;?>");
});
</script>
<?php
}
//calling the function and passing a simple string
frm_trigger_entry_update("Alert Message");
?>
</body>
</html>

Logout Button with PHP Classes

Right now I'm using jquery to run a php script which isn't working, i know that php is a server side script and you can't run the script from jquery, in that case what is the best way to call a php function within a class from the browser?
This is the class where the logout function is defined
class Profile
{
private function logout()
{
$_SESSION = array();
session_destroy();
}
This is the php page where i want to call the function upon click of the logout button
<?php
include_once('profileclass.php');
$user = new Profile($name, $id);
?>
<html>
<head> </head>
<body>
<input type='button' value='logout' id='logout'>
<script>
$(function() {
$('#logout').click(function() {
<?php $user->logout(); ?>
}
});
});
</script>
</body>
</html>
Do not mix PHP with Javascript, since PHP code is executed before the browser renders the html on page. Instead, use $_POST global variable, which will contain the data submitted to your page.
Use:
<?php
include_once('profileclass.php');
$user = new Profile($name, $id);
if ($_POST['logout'] == "<some_value>") {
$user->logout();
}
?>
<html>
<body>
<form method="post">
<input type='submit' value='<some_value>' name='logout'>
</form>
</body>
</html>
UPDATE:
Also, make your logout method public, so that it can be accessed outside your Profile class:
class Profile {
public function logout() { // change is in this line
$_SESSION = array();
session_destroy();
}
}
Frontend
$('#logout').click(function() {
$.ajax({
url: '/logout.php', // Script where logout calls
type: 'post'
});
});
Backend (logout.php)
$user = new Profile();
$user->logout();

Is it possible to use ajax to load a single php function?

Example:
I have written a php script like below:
<?PHP
function _hello(){ "Hello world!"; }
function _bye(){ "Good Bye!"; }
function _night(){ "Good Night!"; }
?>
<html>
<head>!-- required files -- </head>
<body>
<div id="thisDIV">
<? echo _hello(); ?>
</div>
<div>
This is another Division.
</div>
</body>
</html>
Is it possible that I just want to reload the function _hello() in the division (id=thisDIV) by using javascript? Or ajax, Jquery ajax or whatever.. How can I make it? Thanks!
You could add a querystring to your URL and then tie that to what function you want to run:
eg: http://myurl.com/page.php?function=hello
In page.php:
$function = $_GET['function'];
if($function == "hello") {
hello();
}
etc etc

Categories