www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to pass Jvascript Variables to PHP

  1. #1
    Join Date
    Sep 2005
    Posts
    73

    resolved How to pass Jvascript Variables to PHP

    Hi Every one.
    I finally figured a way to pass JavaScript variables to PHP.
    I takes 3 files.
    1. Setup file a PHP or HTML
    <script language="javascript">
    var1= 9;
    var2= 19;
    var3= 'time';
    </script>
    <!-- now we are calling the sender script -->
    <script language="javascript" src="sendpar.js"></script>

    2. the "translation" file is a JavaScript FORM

    document.write('<form name="sendme" action="showpar.php" method="post"><input type="hidden" name="par1" ><input type="hidden" name="par2" ><input type="hidden" name="par3" >');
    sendme.par1.value = var1;
    sendme.par2.value = var2;
    sendme.par3.value = var3;
    document.write('</form>');
    document.sendme.submit() ;

    3. is the PHP "receiver file"
    just a simple file to show you all values:

    <?php
    foreach($_POST as $key => $dat) {
    echo $key .'='. $dat ."\r\n";
    }
    ?>

    That is ALL.

    Good Luck!

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    You could also sent the post request via AJAX without using the form or any other intermediate steps.

  3. #3
    Join Date
    Apr 2009
    Posts
    6
    Um, no offense, but that is probably the worst way to accomplish the task.

  4. #4
    Join Date
    Apr 2007
    Posts
    1,664
    If you're going to be negative about someone's efforts please give the reasons and if you can, suggest a better solution.

    Mindzai's suggestion is a good one and allows for sending the jS vars to PHP without a page reload.

    Lazer, jQuery is a pretty easy way use AJAX. I've used Xajax for a while and like that too.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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