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.
Related
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']); ?>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
So I have a page where I'm requesting users to enter a key via an <input> tag. I would like to store this information in $_SESSION['sessionKey'] when the user clicks a button. Since I'm trying to keep the key moderately secure, I want to avoid passing this information via GET or POST.
Most of what I've found online shows this done by using GET/POST and I'm having difficulty finding information on a method that would not use this approach. I did find this question, which suggests to put JavaScript in a file that has an extension of .php, and from there, use the PHP tags to obtain the $_SESSION variables. I followed this approach like so...
javascript.php
<?php
require ("common/startandvalidate.php");
?>
$(document).ready(function() {
$("#submitButton").click(function(){
<?php $_SESSION['sessionKey']?> = $("#sessionKeyInput").value;
});
});
mainPage.php
<head>
<script src="javascript.php"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<input id="sessionKeyInput" placeholder="Session Key" />
<input id="submitButton" value="Submit" type="button" />
When I look at the page through Inspector (in Chrome) I see the error message:
Undefined index: sessionKey
the require("common/startandvalidate.php"); contains session_start(), so the session is being initiated...
I think it's not working because $_SESSION['sessionKey'] has never been declared before, so even though I want to assign a value to it, it's trying to see the variable contents, which is undefined. How could I go about assigning this value to the $_SESSION variable?
Sorry if this is a simple question, any resources are appreciated!
$_SESSION is a PHP array, you cannot set it on the client side via Javascript. The value has to be sent to the server to be stored in the array.
The question you pointed to shows how you can retrieve the data from $_SESSION, but it won't work for storing data.
The error you see in the console "Undefined index: sessionKey" simply means that the Javascript array named $_SESSION has no key named "sessionKey".
Javascript runs on the client and doesn't have access to the server, and therefore the session. If you want to put a user-entered value in the session, you need to pass it to the server. The most secure way to do that is an SSL-protected HTTP POST.
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
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"; ?>');
});
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
i have a couple of variables that i want to save or not save to a database depending on whether or not i have set them as hidden/visible in a javascript function, i have searched for a while but cant find anything. Thanks!
That's absolutely impossible since PHP runs on the server while JavaScript and CSS are client-side.
The only thing you can do is checking if the element is visible using JavaScript and sending that data to your PHP script, e.g. via a hidden <input> field.
You can also have this alternative. NOTE that the page has to refresh before this take effect (i.e. before PHP has knowledge of what was going on)
//Your JavaScript
function setHidden()
{
document.getElementById('elementForVar1').visibility = 'hidden';
//use this to indicate field/variable is hidden. PHP will use this later
document.getElementById('elementForVar1HiddenField').value = 1;
}
declare hidden fields in your form to store the states of the variables
<form name="xxx">
<input type="hidden" id="elementForVar1HiddenField" name="elementForVar1HiddenField" value="0" />
</form>
Your javascript simply set the value of the hidden field to 1 indicating var1 is hidden
<?php
if($_POST['elementForVar1HiddenField'] == 1)
//variable was hidden
?>