-2

my problem is as follows (Question updated):

I've got two pages:

1.php and 2.php

I'm trying to get a PHP variable (in this very case, a SESSION variable) from 2.php to 1.php through an AJAX request.

This is the script in 1.php

jQuery('#refresh').click(function(e){
    e.preventDefault();
    jQuery.ajax({
        type:'POST',
        url: '2.php',
        data: { sessionVar: '<?php $PHPvariable ?>' },
        success:function(response){
            alert(sessionVar);
        }
    })
});

For your better understanding, this script is called from within the 1.php file, so to properly inject PHP into the js. As you can see I'm trying to retreive the $PHPvariable variable declared in 2.php.

Am I doing it correctly??

Alex van
  • 187
  • 1
  • 3
  • 12
  • 4
    Please fix your code. there is no snippet there. – Ivan Jul 29 '14 at 07:47
  • Without refresh current page. No choice, you should use javascript. – Debflav Jul 29 '14 at 07:48
  • Yes, I got that, but how? :) – Alex van Jul 29 '14 at 07:49
  • you can do it using js see [link](http://stackoverflow.com/questions/19119274/how-can-i-capture-php-session-variables-in-javascript-without-page-refresh) – Amit Kumar Jul 29 '14 at 07:51
  • not using an appropriate tool for a certain job, because not knowing it at a certain point is no excuse, you'd better learn it. At least it's better for you, otherwise you will remain with the knowledge of only making `1.php` and `2.php` till the end of your entire life... – Royal Bg Jul 29 '14 at 07:53
  • So NOT knowing how-to is forbidden. And asking for help even worse... I'm not asking you to do my job. I tried all day long yesterday, and on the web I couldn't find eanugh clues to get it to work... In addition to that I'm Italian and sometimes I find hard to understand English tutorials... I'm trying to learn things by my self, and you are judging me. Thank you! – Alex van Jul 29 '14 at 08:01
  • 1
    Yes, because your question is `I'm using wood to beat nails, but one nail is too big, and I cannot beat it enough to get there, I tried to beat them with a hammer, but unfortunately I couldn't manage, because I do not know how to use hammer. Maybe there's another way`; No, you'd better use a hammer, instead of heavier wood. Having hard time to understand english terms is also good thing, because you will improve your english while trying to understand the terms. So, I'm judging you for the thing you want to find a workaround instead of using the right tool – Royal Bg Jul 29 '14 at 08:10
  • As you can see, I've updated the question, and tried to clarify it as much as possible... – Alex van Jul 29 '14 at 08:48
  • you have to alert not the data you have posted (sessionvar), but the response. And to display something, you need to echo it, `` has nothing to print – Royal Bg Jul 29 '14 at 09:13

2 Answers2

0

Example with JQuery

In 1.php

$.ajax({
  url: "2.php"
})
.done(function( data ) {
  // data should return your variable
});

See here for more : jQuery.ajax()

Martial
  • 1,446
  • 15
  • 27
0

what you can do is make jquery call php file which will get your session value:

function GetSessionValue(sesname){

        $.post("getsession.php", {sesname: ""+sesname+""}, function(data){
            if(data.length >0) {
               //do what you want with your session value
            }
        });

}

and in your php file write only

$sesname = $_POST['sesname'];
echo $_SESSION[$sesname];