Results 1 to 5 of 5

Thread: How to Pass a Javascript variable to a PHP script

  1. #1
    Join Date
    Oct 2010
    Posts
    108

    How to Pass a Javascript variable to a PHP script

    Hello friends I am developing some web document that requires interaction with the server side document. I want to know that how can I pass the java script variable to the Php script on the server side. Since the web document that I am developing is in JavaScript and the other document on the web is a Php enabled script. So can any one help me to know can I make this thing possible. I would be very thankful for all those who comes up with a good advice for the solution.

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: How to Pass a Javascript variable to a PHP script

    Hello friend, I know that unlike html or java which was very easy to communicate to the server side data, Php doesn't have the tools to make it communicate with the client side. Since the javascript which you are using is the client based language and the PHp is only for the server side, inorder of both of them to communicate with each other we need to combine both of them to make them able to communicate with each other. You can get the code from the internet to mix both of them inorder to communicate.

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: How to Pass a Javascript variable to a PHP script

    Hey I have got this code from my friend when he was also working on the same issue as you are currently doing. All you need is to copy this code and paste it in the text editor

    Code:
    <script type="text33/javascript">
    length= screen.length;
    breadth = screen.breadth;
    if (width > 0 && height >0) {
        window.location.href = "http://localhost/main.php?length=" + length + "&breadth=" + breadth;
    } else 
       exit();
    </script>
    After you have done with this save it as index.htm and run it in your browser. After executing this code, you will be automatically redirected to the main.php page where screen resolution in terms of lengthand breadth is displayed in the browser window.

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: How to Pass a Javascript variable to a PHP script

    Hey you can use the following code inorder to communicate with the Php script or when you want to pass the javascript variable to the Php script.

    Code:
    <script>
    var computer = "<?= $phpVar ?>";
    </script>
    In the above code the PHP will parse its own variable and the value of the phpvar will be assigned to the variable computer. The php will always parse the value of the variable before showing in the client side. This is how can send the javascript variables to he php script.

  5. #5
    Join Date
    Apr 2009
    Posts
    569

    Re: How to Pass a Javascript variable to a PHP script

    Hey you can also use the following method to pass the variable to the Php script. You can make use of the get/post arguments for sending and retrieving values from one another. You can make this possible by setting some input value in the java script program and then in your PHP script retrieve it and assign to a PHP variable.
    The javascript program should be something like this
    document.getElementById("d1").value = jasVar;
    And the Php script should be lie this
    $phpVar = $_GET["d1"]

Similar Threads

  1. How to pass PHP variable value to jquery function?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 06:41 PM
  2. How to setup a JavaScript Object Notation variable?
    By hatred in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 02:25 AM
  3. Overloading an operator to pass different variable
    By Flaco in forum Software Development
    Replies: 3
    Last Post: 25-12-2009, 11:27 AM
  4. Passing Javascript variable value into JSP scriptlet
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 23-09-2009, 03:32 PM
  5. javascript dynamic variable name
    By Lauren Bacall in forum Software Development
    Replies: 3
    Last Post: 17-06-2009, 04:20 PM

Tags for this Thread

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
  •  
Page generated in 1,714,095,026.23535 seconds with 17 queries