SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    canada
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow How to pass variables coded in Javascript to another PHP page?

    Hy,

    I am trying to pass variables coded in Javascript inside another page that i created.

    I basically created multiple radio buttons in my php form which users will pick from in an html page. Inside the onClick attribute, I call my javascript functions. For example:

    <INPUT TYPE=\"radio\" NAME=\"question5\" VALUE=\"\" onClick=\"fiveA()\">

    This is what fiveA() which is BTW situated in the same file as the php form except it is inside a javascript tag:

    <SCRIPT LANGUAGE=\"JavaScript\">

    var a5 = 0

    function fiveA() {
    a5= 1
    }

    I have a submit button where Its onClick attribute contains a function called compute(form), and it adds all the values for each answer chosen (A, B, C, or D)
    This is what it looks like:

    function compute(form) {

    tot_A = (a1 + a2 + a3 + a4 + a5)
    tot_B = (b1 + b2 + b3 + b4 + b5)
    tot_C = (c1 + c2 + c3 + c4 + c5)
    tot_D = (d1 + d2 + d3 + d4 + d5)

    if (tot_A >= 3)
    (alert(\"You chose the NERD \" + tot_A ))

    if (tot_B >= 3)
    (alert(\"You chose the ROCKSTAR POSER \" + tot_B))

    if (tot_C >= 3)
    (alert(\"You chose the MARGINAL \" + tot_C))

    if (tot_D >= 3)
    (alert(\"You chose the CLONE \" + tot_D))

    }

    What I want to do now is pass a variable (to another php page) that will contain a number which corresponds to one of the choices (NERD, ROCKSTAR POSER, MARGINAL, or CLONE)

    From what I know I would have to create a global variable and set it to a number and then refer to that global variable in my other php file... but is this type of reference possible ?

    Can someone confirm that to me ?

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    To pass a javascript variable to PHP to need to send it either POST, GET, or COOKIE. JavaScript is a client-side programming languag and PHP is server-side so that is the only way they can talk.

  3. #3
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you could use "hidden" values to get the variables to the other page.
    Code:
    <input type="hidden" value="" name="mytotal_A" />
    After calculating the total, via javascript, update the total
    Code:
    mytotal_A.value = tot_A
    This code is just to give you an idea of what to do, it's not the correct JS syntax. Replace tot_A with whatever you need. If you need separate values for A, B, C , D, use 4 hidden fields.

    Then, when the form is submitted to any other page, the mytotal_A value will be accessible via $_POST['mytotal_A'] [assuming you are using method="post" for your form submission]

    Hope this helps.

    -asT.

  4. #4
    SitePoint Member
    Join Date
    Nov 2004
    Location
    canada
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here's something else I added to it!

    I've tried hidden field in my javascript but it doesn't work it gives me an error:

    Parse error: parse error in /home/foreignf/public_html/PHP/autotriv/autotriv.php on line 281

    Thats what I added to my code:

    if (tot_A >= 3) {
    (alert(\"You chose the NERD \" + tot_A ))
    <input="hidden" name="nerd" value=<?php echo $_POST['nerd']; ?>
    }


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
  •