Setting a PHP $_SESSION['var'] using jQuery
You can't do it through jQuery alone; you'll need a combination of Ajax (which you can do with jQuery) and a PHP back-end. A very simple version might look like this:
HTML:
<img class="foo" src="img.jpg" />
<img class="foo" src="img2.jpg" />
<img class="foo" src="img3.jpg" />
Javascript:
$("img.foo").onclick(function()
{
// Get the src of the image
var src = $(this).attr("src");
// Send Ajax request to backend.php, with src set as "img" in the POST data
$.post("/backend.php", {"img": src});
});
PHP (backend.php):
<?php
// do any authentication first, then add POST variable to session
$_SESSION['imgsrc'] = $_POST['img'];
?>
How to assign value of jQuery to PHP SESSION in javascript
It's not possible to directly assign session value. using AJAX to assign the session value. Please review below code
var designation = $("#Designation").val();
$.ajax({
url: "session.php",
type: "POST",
data : {'designation' : designation },
success: function(html){
}
});
Then create the session.php file paste the below code
session.php
session_start();
$_SESSION['lclDesignation']=$_POST['designation'];
jQuery AJAX - Set PHP Session variable
Try this...
Use "session_start" before check
session_start();
if (isset($_SESSION['promoBar'])) {
echo $_SESSION['promoBar'];
}
else {
$_SESSION['promoBar'] = "closed";
echo "does not exist";
}
?>
How to echo PHP session variable While using Jquery?
You need to change 'data-title' attribute in 'input' field
data-title="MB | <?php echo $_SESSION['UserName'];?> | <?php echo $_SESSION['TeamName'];?> | "
instead of
data-title="MB | "
Get a PHP $_SESSION variable from Jquery?
You'll need to inject it, something like this:
var sessName = '<?php echo $_SESSION['name']?>';
The file containing this script must be executed by the php interpreter (i.e. a .php file)
EDIT: Conceding to Radu's point, it would be safer execute for unsanitized data:
var sessName = <?php echo json_encode($_SESSION['name']) ?>;
Setting session variable with jQuery (without passing via Post or Get)
$_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".
Setting session variable in php through jquery click event
Here's what I would do (cut some code out for quickness):
HTML (you don't require javascript void code if using jQuery):
<ul id="set-display">
<li><a href="" id="list">Listview</a></li>
<li><a href="" id="grid">Gridview</a></li>
</ul>
jQuery:
$('#set-display a').click(function(e) {
var type = $(this).attr('id');
var view = (type === 'grid') ? 'gridview' : 'listview';
$.post('library/fx.behaviour.php', { display: view });
// you can use the view or type variable to trigger your other code
});
PHP:
session_start(); // if not called already
// if POST display not empty and is expected string then use it, otherwise default to 'listview'
$_SESSION['display'] = ( ! empty($_POST['display']) && in_array($_POST['display'], array('listview', 'gridview'))) ? $_POST['display'] : 'listview';
I used $_POST['display']
rather than setdisplay
for naming consistency. It might also be worth checking your jQuery call gets a valid 200 response. In Chrome you can do this by going to the Inspector, selecting the network tab, click the button, and watch for the request.
Related Topics
How to Get Page Number on Dompdf PDF When Using "View"
Having Two Different Sessions in Same Domain
Retrieve Data from Db and Display It in Table in PHP .. See This Code Whats Wrong with It
Yii2 Translation Does Not Work
Stored Procedures, MySQL and PHP
Difference Between Mt_Rand() and Rand()
Increase PHP Script Execution Time
Php: Do Arrays Have a Maximum Size
Laravel 5.1: Keep Uploaded File as Old Input
How to Use Array_Unique on an Array of Arrays
Flip Associative Array and Store New Values in Subarrays to Prevent Losing Duplicated Values
PHP in Background Exec() Function
Convert Exponential Number to Decimal in PHP
How to Get Data in Function Extend Controller