-1

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>
1
  • if you save the value in some database, sanitize your inputs or you will have problems later! Commented May 24, 2014 at 23:38

3 Answers 3

1

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;

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

2 Comments

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.
@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!
1

Set it to session with

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

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

Comments

1

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.

Comments

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.