1

I have two separate pages, one page is where it uploads the file and the other page displays the information.

In the imageupload.php page, I have this session below:

$_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];

I also have a javascript function which calls back to the javascript functiom:

<script language="javascript" type="text/javascript">window.top.stopImageUpload();</script> 

Now on a seperate page (QandATable.php), I have a javascript function, but my question is how can I call the $_SESSION code above in the javascript function so I can append it to $('.list')?

Below is javascript function:

 function stopImageUpload(success){

var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').append('<br/>');
          }
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
          }

return true;

    }
4
  • 3
    FYI: window.top.stopImageUpload; doesn't do anything. I assume you meant window.top.stopImageUpload();? Commented Apr 20, 2012 at 21:54
  • @Rocket yes, sorry that was a typo Commented Apr 20, 2012 at 21:55
  • 1
    I believe you want to use ajax Commented Apr 20, 2012 at 21:55
  • How can it coded to use AJAX? Commented Apr 20, 2012 at 21:57

4 Answers 4

2

You cant, because $_SESSION is a server side variable but you can access it by.

For the entire session variable

<script type="text/javascript" >
  var session = <?php echo json_encode($_SESSION); ?>;
</script>

For a particular variable in session.

<script type="text/javascript" >
  var session_var = <?php echo json_encode($_SESSION['VAR_NAME']); ?>;
</script>

Now you have js variable called session with that information. However it is not advisable in most situation to output all that info to public pages.

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

4 Comments

So for $_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];, will it be coded as var session = <?php echo json_encode($_SESSION['fileImage']['name'] ?>;
I am getting an invalid regular expression flag w error when I include code above
What is $_SESSION['fileImage']['name'] returning in php
It returns the name of the file uploaded
2

Session variables are stored on the server. JavaScript is executed on the cliend side, so it knows nothing about the server side. It know only as much as you pass to it.

To pass a variable to javascript, use an ajax request, or simply output the values:

<script>
var sesionValue = <?=json_encode($_SESSION['value']);?>;
</script>

1 Comment

Here, you are seriously missing "output escaping" : what it the value contains an apostrophe ?! depending on how this value is populated, this can be the source of real security issues (js code injection, ...) some escaping function like addslashes() needs to be used.. or more generally (depending on the value type), json_encode()
1

You should look into using JQuery, as it makes these AJAX-like tasks much easier.

See my function I wrote just today to do something similar to what you're asking.

This takes some PHP output (returned in the success part of the call to ajax(). The format it takes is in JSON, which is compatible by both PHP and JavaScript (JSON: JavaScript Object Notation).

function viewClientDetails(id) {
    var clientParams;
    clientParams.clientID = id;
    $.ajax({
        url: BASE_URL + '/clients/get-client-details.php',
        type: 'POST',
        data: clientParams,
        dataType: 'JSON',
        success: function(myClient) {
            var name = myClient.name;

            $('td#name').html(name);

        },
        error: function(e) {
            console.log(e.responseText);
        }
    })
}

In my PHP file (called /clients/get-client-details.php) I have something like this:

<?php
...
$myClient = array('name' => 'Mr Foobar');
print json_encode($myClient);

?>

This simply writes my PHP object to JSON format.

In the JS code above, the code inserts a part of the JSON data into an HTML table-data element whose CSS selector ID is #name, with the line: $('td#name').html(name);

Apologies if this confuses you more, I thought I'd show an example of what you can try some time..

This may help you a bit along the way...keep trying things, you'll get there :)

Comments

0

You can't. $_SESSION is a PHP variable, and that code runs server-side.

You'll need to store the value as a Javascript variable in the output from your PHP file, then access that variable in your Javascript.

3 Comments

$_SESSION is just a variable.
$_SESSION is neither a method, nor it's a function. It's a variable (superglobal).
You're both correct, I was mixing up my brackets and getting confused - I'm going to blame the fact that it's late.

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.