www.webdeveloper.com
Results 1 to 5 of 5

Thread: Pass selected date back to PHP from javascript

  1. #1
    Join Date
    Aug 2009
    Location
    Earth, Solar System
    Posts
    11

    Pass selected date back to PHP from javascript

    Hi there,

    I have little problem I'm facing. I have a calendar and when the user selects a day, it shows the selected date. However, I can't seem to pass that selected date back to a php script.

    This is the function that prints the selected date:
    Code:
         if (this.selectHandler) {
              this.selectHandler(this, this.date.print(this.dateFormat))  // Prints the selected date to the screen.
    	  var dateSelected = this.selectHandler(this, this.date.print(this.dateFormat))
    
    	  location.href="getSelectedDate.php?selectedDate="+dateSelected  // This is where I pass to the php script
         }

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by sleeper24 View Post
    However, I can't seem to pass that selected date back to a php script.
    You can not pass a javascript variable to a php script. You may pass a value to a database (via a classic submit or AJAX technique), from where the php application might read it. Javascript is a client-side language, while php is a server-side one. They can not interfere the way you have mentioned.
    Last edited by Kor; 08-12-2009 at 01:25 PM.

  3. #3
    Join Date
    Aug 2009
    Location
    Earth, Solar System
    Posts
    11
    I got it to work. Here is the code. Although I don't know if it is legal or standard.
    Code:
    var dateSelected = this.date.print(this.dateFormat);
    var page = document.title;
    location.href="getSelectedDate.php?selectedDate="+dateSelected+"&page=" +page;

  4. #4
    Join Date
    Aug 2009
    Posts
    25
    I think what you're looking for is the PHP equivalent of ASP's postback. You can fake it by submitting the page as a form (with itself as the action) and having PHP read the data from $_POST["selectedDate"]

    edit: looking at your last post, there might have been an invalid character in the dateSelected variable that couldn't be passed in the URL (like "/") -- can you give us an example of what the new output is?
    Last edited by Alzheimers; 08-12-2009 at 01:50 PM.

  5. #5
    Join Date
    Aug 2009
    Location
    Earth, Solar System
    Posts
    11
    Alzheimers,

    So this is the code that I could not pass the date back.
    Code:
    if (this.selectHandler) {
              this.selectHandler(this, this.date.print(this.dateFormat))  // Prints the selected date to the screen.
    	  var dateSelected = this.selectHandler(this, this.date.print(this.dateFormat))
    
    	  location.href="getSelectedDate.php?selectedDate="+dateSelected  // This is where I pass to the php script
         }
    What I changed from that code was I remove the "this.selectHandler(this, " from this:
    Code:
    var dateSelected = this.selectHandler(this, this.date.print(this.dateFormat))
    Here is what it looks like after I removed that piece of code.
    Code:
    var dateSelected = this.date.print(this.dateFormat);
    Then you can use the "location.href " to pass it to a php file like I did above.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles