This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i dont know what to say but, i have a banner with a session variable, if that session variable is set and have a value 1 then i have displayed that banner. i also have a button to hide that banner and i have called a javascript function onclick that button.
so i have created a simple javascript function and added some php code with php tag to unset the session variable. I can also use ajax to unset the session variable. but i was thinking to do that with that function why to do ajax call.
i have some code:
<?php if($_SESSION['SHOW_BANNER']==1){ ?>
<div id="banner">Banner goes here</div>
<button onclick="hideit()">Hide</button>
<?php } ?>
Here is jquery function:
<script>
function hideit()
{
<?php unset($_SESSION['SHOW_BANNER']); ?>
$("#banner").hide();
}
</script>
Each time when i reload the page, the session variable is unset.
Any Help..
You can't execute PHP code directly from JavaScript. PHP code is executed when page is rendering on server side, and if you have syntax like:
function hideit()
{
<?php unset($_SESSION['SHOW_BANNER']); ?>
$("#banner").hide();
}
It executes every time unset when page is loaded and result of this code is returned to JavaScript. Only way to unset session variable is to execute code on server side, you can do this with simply ajax call.
function hideit()
{
$("#banner").hide();
$.get("hideBanner.php");
}
And in hideBanner.php just execute
<?php unset($_SESSION['SHOW_BANNER']); ?>
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I was trying to unset a php session variable inside a jQuery if block. Whenever someone unchecks a checkbox it should unset a specific session variable. Here is my code below
PHP
session_start();
$_SESSION['myName'] = "John";
HTML
<form action="" method="post">
<input type="checkbox" id="myCheckbox"> Check to set session
</form>
jQuery :
$('form').change(function(){
if($('#myCheckbox').is(':checked')){
// Some statements here
}else{
<?php unset($_SESSION['myName']); ?>
}
});
But the code is not working here. It is not unsetting the php session variable. Please provide me a solution. Thanks.
Note : I can do it by using AJAX by loading the php code from another page. But I want it to be done by using jQuery only.
Or is there any solution in jQuery that I can unset session variable without using PHP code?
It will not work this way.
All PHP codes are executed before Javascript function .
To achieve this :
You have to do AJAX or form submission.
You just cant do it by only jQuery.
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.
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>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
/*
//Want to call a php function when on clicking a tag
// Want to call a php function when on clicking a tag
//Want to call a php function when on clicking a tag
// Want to call a php function when on clicking a tag
*/
<?PHP
//starting of a function
function callfun(id)
{
//prints the parameter of the function
echo id;
}
//call a php function when clicks on a paragraph
echo '<p id="xyz" onClick="callfun(id)">Click Me </p>';
?>
/*Want to call a php function when on clicking a tag
Want to call a php function when on clicking a tag
Want to call a php function when on clicking a tag
Want to call a php function when on clicking a tag
*/
You can use some technology to do this. One way is to call another script using ajax call for example. If you don't know what ajax is here is an explanation http://bg.wikipedia.org/wiki/Ajax
Another think you can try is just using javascript and translate your function in javascript.
PHP is not working like that.
I suggest you to watch some tutorials about the language technique.
First of all you need to understand the difference between the server side and the client side.
PHP is not running on the client side!
For your need you must use AJAX function which will call a request to the server.That request will execute the PHP function and return the answer to the clients browser.After that you can handle it for your needs.
i am trying to make something related to paging, where i have a array of around 400-500 values..
i want to show 9 values at a time and then after clicking on NEXT button i want to show next 9 more..
Here is the part of the code i am using to call JS function onclick of button and that function's job is to get the count from the browser and set it to next value.
<?php
session_start();
$_SESSION['count']=9;
echo "<script type='text/javascript'>
function changelistnext()
{ ";
$c=$_SESSION['count'];
$c=$c+1;
$e=$c+9;
$_SESSION['count']=$e;
echo "
alert($c)
alert($e)
}
</script>";
?>
<button id='next' value='next' onclick='changelistnext()'>Next</button>
But everytime i click on the next it shows the same value....
You can't mix JavaScript and PHP as if they were one and the same.
JavaScript is a client-side language, which is executed in your browser.
On the other hand, PHP is a server script, and sessions are also kept on server.
If you open your page's source code in a browser, you will see that the session stuff is not there - it was executed by PHP when the page was generated. You can't use sessions directly in JS.
The page contains essentially this:
<script type='text/javascript'>
function changelistnext()
{
alert(some number) // value of C when the page was generated on server
alert(some number) // ditto for E
}
</script>
You could use Ajax call to ask some script on your server to give you data for the next "page" - PHP script can access the Session variables and send what you want back to the browser.