I have a page that has a javascript date picker, the result of which goes into an input box on an HTML form (see image below). This works fine.

enter image description here

I would also like the chosen date to be stored in as session variable. Can anyone point to how I should do this? Can a session variable be assigned within javascript?

Code so far:

<?php
session_start();

Datepicker

....
<script type="text/javascript">
window.onload = function(){
    g_globalObject1 = new JsDatePick({
        useMode:2,
        target:"inputFieldStart",
        dateFormat:"%Y-%m-%d",
        cellColorScheme:"armygreen"
    }); 

};
</script>

HTML Form

<form action="https://website" method="Post">
<input name="StartDate" type="text" id="inputFieldStart">
<input type="Submit" value="Update" class="button">
</form>
share|improve this question
    
if you save the value in some database, sanitize your inputs or you will have problems later! – Spikolynn May 24 '14 at 23:38

The session variable needs to be set as a php variable, on the server. Your HTML form, which calls your server with a Post method passes this variable to the php page, and it can be read and set as a session variable using

<?php
$start_date = $_POST["StartDate"];
$_SESSION['start_date'] = $start_date;

?>
share|improve this answer
    
You are right yes, but what I want to do is set the session variable at the time of picking a date. The form has already loaded at this point. – RGriffiths May 24 '14 at 23:29
    
@Richard typically PHP is used to create a HTML document, which can contain javascript - it's all just a bunch of text to PHP. You cannot interact directly with PHP from javascript - you MUST do a HTTP request of some kind! – James May 24 '14 at 23:52

Set it to session with

 $_SESSION["date"] = $_POST["StartDate"];

to "set it with javascript" use AJAX. For instance jQuery's $.ajax

share|improve this answer

A session variable cannot be assigned with JavaScript directly, you can however use AJAX to send a request to a PHP document to create/change the session variable if you must insist on using JavaScript.

In your PHP document (we'll call it date_assign.php) write the following:

$date = $_POST['date'];
$_SESSION['date'] = $date;

Then in your JavaScript use this function (with the jQuery library included, of course) to send the request to the PHP document.

// Function to call in order to change session variable.
function sessionVariable() {

    // Get date from picker.
    var date = $('#inputFieldStart').value();

    // Create data string.
    var datastr = 'date=' + date;

    // Create the AJAX request.
    $.ajax({
        type: 'POST',
        url: 'date_assign.php',
        data: datastr
    });

}

Of course this is a long way around of doing it but it means you can accomplish it with JavaScript. You can call the function whenever you need to set the session variable, this can be done on an interval or you can bind a click listener to the Submit button to send the data, your call.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.