0

This question might seem a duplicate and There are many posts on how to pass jquery variables to php. Tried all of them none worked for me.

Here is what all I did.

From Jquery:

$.ajax({
       url: '/test.php',
       data: {"name":name,"phone":phone},
       type: 'post',
       success:function(data){
           console.log("Succses"); 
       }
    });

In test.php

session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['phone']=$_POST['phone'];

There is nothing complex in this code. It is so simple and am seeing the "Success" message on the console. When am trying to echo this sssion variable in another page, this is shown empty.

Any help is greatly appreciated.

3
  • Session on your local page will not exist until you reload the page because the session is controlled on the server-side. Commented Oct 12, 2016 at 13:16
  • Did you call session_start? Commented Oct 12, 2016 at 13:18
  • still doesnt work Commented Oct 12, 2016 at 13:31

1 Answer 1

3

Please use session_start() on the first line of your PHP code to make the session works for you for that page.

You might have following problems

  • Might be missing session_start()
  • You might getting blank values on $_POST

Do the debug for ajax call and don't forget to check print_r($_POST) has values in success or not.

Sign up to request clarification or add additional context in comments.

8 Comments

modifid with session_start(); still no luck
Try with change POST instead of post and make sure that you're getting data in $_POST
console.log(data) is returning empty. still confused why php is not returning data
is your url is right ? what print_r($_POST) this gives ?
So everything is ok for you now ?
|

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.