post javascript variable as form input to php

Discussion in 'JavaScript' started by Hemant Agarwal, Aug 16, 2010.

  1. #1
    Hi,
    I am trying to post a javascript variable as form input to a php file.

    I have a radio button and if the user selects the radio button and submits the form,the javascript variable should be posted to the php file.However,it is not working.This is what I am trying to do.What's the correct method?Thanks
    Note:questions[4][1] is the javascript variable here.

    <input type="radio" name="q5"><script>document.write(questions[4][1]);</script><br>
     
    Hemant Agarwal, Aug 16, 2010 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Assuming myVariable is global:

    <input type="radio" name="q5" onclick='this.value=myVariable'>
    Code (markup):
    There should be more than one button in the group, otherwise the action cannot be undone.
    Perhaps:
    <input type="radio" name="q5" onclick='q5[1].value=null'>Undo<br>
    <input type="radio" name="q5" onclick='this.value=myVariable'>Set
    
    Code (markup):
     
    Logic Ali, Aug 16, 2010 IP
  3. Hemant Agarwal

    Hemant Agarwal Member

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    yeah,
    there will be more than one radio button.This is how the form looks like

    
    <form name="QuestionAnswers" action="30" method= "post">
       
    <input type="submit" name="Submit_answers" value="Submit Answers" >
    
    
    
        
    <script>document.write(questions[0][0]);</script><br> <!-- This is the question -->
    
    
     <!-- The following are the answer choices  -->
    
     <input type="radio" name="q1"><script>document.write(questions[0][1]);</script><br>
    
     <input type="radio" name="q1"><script>document.write(questions[0][2]);</script><br>
     
     </form>
      
    Code (markup):
     
    Hemant Agarwal, Aug 17, 2010 IP
  4. Hemant Agarwal

    Hemant Agarwal Member

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #4
    Hi
    Your method worked.Thanks a lot.Here's my new code(please note that I have not published certain parts of the code before/after and in between the present code as I thought it was irrelevant to the discussion.)

    
    <form name="QuestionAnswers" action="30" method= "post">
    
    
    
    <input type="submit" name="Submit_answers" value="Submit Answers" >
    
    
     <!--This is the question-->
    <script>document.write(questions[0][0]);</script><br>
    
    <!--These are the answer choices-->
     <input type="radio" name="q1" onclick='this.value=questions[0][1]'><script>document.write(questions[0][1]);</script><br>
    
     <input type="radio" name="q1"  onclick='this.value=questions[0][2]'><script>document.write(questions[0][2]);</script><br>
    
    
    </form>
    
    Code (markup):
     
    Hemant Agarwal, Aug 17, 2010 IP
  5. Lucas Rodrigues

    Lucas Rodrigues Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you can try to add it to a url and get it as a GET with the server page
     
    Lucas Rodrigues, Aug 23, 2010 IP