user5005768 user5005768 - 5 months ago 9
Ajax Question

How to pass Data From JQuery AJAX to PHP Without submit

I want to pass a data

data: ( {name: 'ccenter', value: 'Sales Department' } )
using AJAX JQuery into PHP page Without any submission.
I try with
session_name(); session_start();
But It didn't work.variable
$ccenter
still remains undefined.I think i have missed some code to do so.please help me.Thanks

Here Is my Jquery AJAX Code:

$(function () {

$.ajax({
url: 'empid_list.php',
type: "post",
async:true,
data: ( {name: 'ccenter', value: 'Sales Department' } ),
dataType: 'html',
success: function(data) {
$('#employeeid').html(data);
}
});
});


And Here Is my PHP File code:

<?php

session_name('session1');
session_start();
$ccenter = $_POST['ccenter'];

?>

Answer

Your data format is wrong ,must be {data-name:data-value}

data: {name: 'ccenter', value: 'Sales Department' } 

So you should call by data-name

$ccenter = $_POST['name'];