Reputation: 9
I have no idea how to pass variable to another page using ajax. I wish to get data from Page1 and echo in Page2. Here is my code:
Page 1
<?=
formDropdown('employeeName', 'employeeName', 'Employee Name:', $empName,
isset($employeeName) ? '' : $hiddenV , 'All', '', 'onchange="employeeNameChange(this.value)"', 'form-control', '<br>');
?>
<script type="text/javascript">
function employeeNameChange(str) {
$.ajax({
type: "POST",
url: "indexPage.php",
data: "en=" + str,
success: function(data){
}
});
}
</script>
Page 2
$emp_name = $_POST['en'];
echo "Employee Name is ". $_POST['en'] ;
When I echo the message, output is "Employee Name is ". Why is that?
Upvotes: 0
Views: 94
Reputation: 5401
Assuming that the variable is defined, you can try this to check if it really contains something
function employeeNameChange(str) {
$.ajax({
type: "POST",
url: "indexPage.php",
data: "en=" + str,
dataType:"text",
success: function(data){
alert(data);
},
error:function(xhr, status, error){
alert(error);
}
});
}
And in indexPage.php
if(isset($_POST['en'])){
echo $_POST['en'];
}
This should alert the value of str
, or alert an error
Upvotes: 2