1

JS in (start.php)

$(document).ready(function()
{
    $('#btn_1').click(function(){
        $.ajax({
            type: "POST",
            url: "get_data.php",
            data: 'func=getData1',
            success: function(msg){
                $('#div_1').html(msg);
            }
        });
        $('#div_1').show();
    })
});

PHP (somename.php)

<?php
session_start();
if(trim($_POST['func']) == "getData1")
{ 
    echo "Test";
}
?>

How can i pass the sessionid from start.php through my ajax to the get_data.php file ? And how can pass the complete URL "url: "get_data.php," to the js-File so that i can switch the php-files, that should be called from ajax ?

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
user2976312
  • 113
  • 1
  • 8
  • You appear to have two entirely different questions here. You shouldn't merge them together for stackoverflow. Focus one problem at a time. That said, the answer to one appears to be "do nothing at all" and the other don't have any idea what you are asking for. – Quentin Jan 20 '15 at 09:36
  • You can pass multiple variable to AJAX like data: {func:getData1,url:google.com,session:id} – Sunil Pachlangia Jan 20 '15 at 09:38

5 Answers5

1

Store Session ID in javascript variable and send it through ajax call, like this:

var session_id = '<?php echo session_id();?>';

Complete code should be:

var data = {func:'getData1',session_id:session_id};
$('#btn_1').click(function(){
    $.ajax({
        type: "POST",
        url: "get_data.php",
        data: data,
        success: function(msg){
            $('#div_1').html(msg);
        }
    });
    $('#div_1').show();
})

Updates

If you want to access php variable in external js file, define variable before including js file. Like:

<script type="text/javascript">
    var session_id = '<?php echo session_id();?>';
</script>
<script src="./ajax.js" type="text/javascript"></script>
Manwal
  • 23,450
  • 12
  • 63
  • 93
  • thanks so far, but the var session_id is empty in the .js file . if i alert the var session_id after the first row, it shows '' – user2976312 Jan 20 '15 at 10:04
  • @user2976312 I forgot you was using external `JS` file see updated answer. You can access variable in js file like this. – Manwal Jan 20 '15 at 10:12
0
$(document).ready(function()
{
    $('#btn_1').click(function(){
        $.ajax({
            type: "POST",
            url: "get_data.php",
            data: {func:'fuc_name',session:'<?php echo session_id();?>'},
            success: function(msg){
                $('#div_1').html(msg);
            }
        });
        $('#div_1').show();
    })
});
ABIRAMAN
  • 929
  • 8
  • 12
0
var session_id = '<?php echo session_id();?>';
$('#btn_1').click(function(){
    $.ajax({
        type: "POST",
        url: "get_data.php",
        data: {func:"getData1","session":session_id},
        success: function(msg){
            $('#div_1').html(msg);
        }
    });
    $('#div_1').show();
})
});
Priyank
  • 3,778
  • 3
  • 29
  • 48
0

Use json encode. By using json you can pass the php data to js. Changing the code as per below. Set session id after session starts.

$(document).ready(function()`{    
    $('#btn_1').click(function(){ ` $.ajax({  `type: "POST",` dataType:"json", `url: "get_data.php,` data: 'func=getData1'`success: function(msg){ ` $('#div_1').html(msg.id); `}     
    });      
    $('#div_1').show();      
})     

});

in php side echo the variable with json encode. echo json_encode($id); `

nidhin
  • 419
  • 1
  • 4
  • 16
0

Use json encode. By using json you can pass the php data to js. Changing the code as per below. Set session id after session starts.

$(document).ready(function()
 '{    
    $('#btn_1').click(function(){ `
$.ajax({
type: "POST",`
dataType:"json", `url: "get_data.php,` 
data: {func:'enter the data you want to pass'},
success: function(msg){ 
        $('#div_1').html(msg.id); 
   }     
    });      
        $('#div_1').show();      
})     

});

in php side echo the variable with json encode. echo json_encode($id); `

nidhin
  • 419
  • 1
  • 4
  • 16