I am trying to pass a selection/value from a dropdown menu which was dynamically created to php file and store it in a session variable, but the php is not getting any parameter. This is my code in the Jquery file:
$('#fgdesignation').change(function ()
{
var project =$("#fgdesignation option::selected").text();
var url='backend.php';
// alert(project);
$.post(url, {fgdname : project});
});
and this is the php file:
if(isset($_POST['fgdname'])) {
$fgdname= $_POST['fgdname'];
$_SESSION['fgdname']=$fgdname;
}
Thank you!
Try this:
$('#fgdesignation').change(function () {
var project = $("#fgdesignation").filter(':selected').val();
var url = 'backend.php';
// alert(project);
$.post(url, {
fgdname: project
});
});
Off hand, I saw that you were using .text()
instead of .val()
Uncomment your alert()
and see if there is value being returned
EDIT I did $("#fgdesignation").filter(':selected').val();
assuming: <select id="fgdesignation'>
.
Don't forget to add
session_start()
at the top of your PHP file. That's it.
Try:
$('#fgdesignation').change(function ()
{
var project =$("#fgdesignation option:selected").val();
var url='backend.php';
// alert(project);
$.post(url, {fgdname : project}, function(response){alert(response);});
});
You were using .text()
instead of .val()
, which returns the value of the currently selected option, as well as having '::' as your selector when you only needed one.
And in your PHP file:
session_start(); // Important if you're not already doing it!
if(isset($_POST['fgdname'])) {
$fgdname= $_POST['fgdname'];
$_SESSION['fgdname']=$fgdname;
echo $fgdname;
}
If all goes to plan you should have an alert box with the value of what you selected in the select box and have it present in the session array. Give it a go.