Results 1 to 3 of 3

Thread: Assign Jquey value to Php Variable inside jquery function

  1. #1
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Assign Jquey value to Php Variable inside jquery function

    Hi All,

    I am struck with some issue in jquery. i want to set a php variable inside jquery. Following is the jquery function i am using

    $("#edit").on("click", function() {

    var yearss = "2002"; // i will get this value dynimaically

    <? $years = ?> yearss; // i don't know how to get this in php

    // for some reasons i cant use ajax or json, because i am not sending data to another page, i want that php variable inside this jquery function only

    });

    Can anyone help me or let me know it can't be achieved this way.
    Last edited by hemi519; 01-21-2014 at 02:39 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If I understand what you're asking, it cannot. You will need to setup at least a small external PHP script and use AJAX to have the server set the PHP variable $years and it ($years) will not be available on the current page unless it's either refreshed or queries the server via AJAX for the value, which would have to be stored or passed in some way (as a part of a query string to another PHP page/script or set in a cookie or session variable to survive either process). However, at the same time that you set $years, you can set a javascript variable to the same thing. It will be immediately available on the current page to use for anything you like.

    In other words, by the time any click event occurs on the page (in fact, by the time any javascript event occurs or even before anything is parsed by the browser) all PHP variables have already been set and parsed and you cannot go back and change any of them except with AJAX. However, you can change how the page looks using just javascript.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    To clarify, you can't "go back and change them" even with ajax: you're actually running that php again; starting over, fresh. Ajax is still your most likely solution, however.

    Quote Originally Posted by hemi519
    for some reasons i cant use ajax or json, because i am not sending data to another page, i want that php variable inside this jquery function only
    That wouldn't prevent you from using ajax. You would, however, need to modify the existing page to be able to respond only to the ajax request when needed, without printing the entire page again. In many cases, it's simply easier to create a separate page to handle the ajax request.

    Might I ask for what purpose PHP needs this variable? Might help find a solution, if we knew what the problem was.

Similar Threads

  1. how to assign variable within query
    By raihan in forum MySQL and other databases
    Replies: 1
    Last Post: 04-26-2013, 01:47 PM
  2. Assign dynamic value to variable
    By Rachele7 in forum JavaScript
    Replies: 1
    Last Post: 07-18-2011, 10:57 AM
  3. Replies: 1
    Last Post: 11-14-2008, 06:00 PM
  4. Replies: 1
    Last Post: 09-20-2008, 02:36 AM
  5. Replies: 4
    Last Post: 06-17-2007, 08:40 PM

Bookmarks

Posting Permissions

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