0

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'];   
?>
3
  • $ccenter = $_POST['ccenter']; or $ccenter = $_POST['name'];?? Commented Jun 7, 2016 at 7:40
  • should be $_POST['name'] instead of $_POST['center'] (because of key -> value association) and you should echo something back from php, else data will be undefined. Commented Jun 7, 2016 at 7:41
  • Any errors in console ? Did you try checking data by print_r($_POST) on our server side ? Commented Jun 7, 2016 at 7:44

2 Answers 2

2

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear that !
0

I recommend you to use javascript dataform objects:

var formData = new FormData();
formData.append("ccenter", "Sales Department");


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

2 Comments

Is FormData supported by all browsers?
@DamienPirsy Basic support (no file attaching) in: Chrome 7+, Firefox 4.0+, Internet Explorer 10+, Opera 12+, Safari 5+ Check it out: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.