SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool RadioButton with array,Help Plz

    Hi All,
    I have a questionare.I want to show 5 questions of that questionare in each page.for the time being i have 5 pages.Each questions has multiple radio button answer.
    when user comes into the 1st page he choose answer of 5 q.then the radio button answer hold in an array and passed into next pages as a hidden array and then again add some answers from the 2nd page it goes to 3rd and so on..
    at the end of the test(questionare) it goes into Mysql databases.
    Will you guide me how can I do that?
    -::Ujjwal::-
    Visit My Site

  2. #2
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to make it?
    means I have some questions and i am splitting it into 3 or 4 pages.one pages has 5 q with multiple radio button answer.if user select all those in one page and then goes into next one and do the rest.
    -::Ujjwal::-
    Visit My Site

  3. #3
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi ujwal,
    yes it's possible. The $_SESSION superglobal will do the trick.

    PHP Code:
    $_SESSION['answer1'] = $answer1;
    $_SESSION['answer2'] = $answer2;
    // etc... 
    Note that the $answer1 variable is set based on the user input (use $_POST['whatever'] to get it). Process it the way you want once you collect the form data.

    Using this, you won't need the hidden field trick.


  4. #4
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    according to duddie i am doing this nbut the page is not moving.

    I saved this page rd1.php
    PHP Code:
    <?php
    if(isset($_POST['B1']))
    {
        
    session_start();
        
    $_SESSION['fruit']=$_POST['R1'];
        
    $_SESSION['animal']=$_POST['R2'];
        
    header('Location :/rd2.php');
    }

    ?>
    <form method="POST" action="<? echo $_SERVER['PHP_SELF'?>">
      
      <p>What is your Favourite Fruit?</p>
      <p><input type="radio" value="Mango"  name="R1">Mango</p>
      <p><input type="radio" name="R1" value="Lichi">Lichi</p>
      <p>What is your favorite animal?</p>
      <p><input type="radio" name="R2" value="Tiger">Tiger</p>
      <p><input type="radio" name="R2" value="Lion">Lion</p>
      <p><input type="submit" value="B1" name="B1"></p>
    </form>
    and in rd2.php i have done
    PHP Code:
    <?php
    echo "YOUR choices are $_SESSION['fruit'] and $_SESSION['animal']";
    ?>
    My q is that why the page is not moving and where my script is wrong?
    I want to do select the choosing radio button from rd1.php and then storing it into session variable and accessing it into rd2.php.
    Where i am wrong in my script?
    -::Ujjwal::-
    Visit My Site

  5. #5

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you calling session_start() in rd2.php too?

  6. #6
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by drzoid
    Are you calling session_start() in rd2.php too?
    No I haven't call it but at first the page is not moving to rd2.php
    how to call session_start() in rd2.php .I don't know that
    If i do $_SESSION['fruit'] is not printing the value which has been store in it?
    plz guide me i am novice
    -::Ujjwal::-
    Visit My Site

  7. #7

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless you have session.auto_start ON, you need to call session_start() on each page you want to use the $_SESSION array on, as this function actually initialises the array (basically it checks whether the client sent a session ID, if it did it will validate it and retrieve the associated data, otherwise it will create a new session and send it back to the client).

  8. #8
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fine but why my rd1.php is not moving to rd2.php sir?
    -::Ujjwal::-
    Visit My Site

  9. #9

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By definition a redirect should include an absolute URL, however a relative one is usually fine as well.

    Is rd2.php in the same directory as rd1.php? Then try the following
    PHP Code:
    header('Location: rd2.php');
    exit(); 

  10. #10
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It shows this error
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in d:\www\rd2.php on line 2

    but url shows
    http://localhost/rd1.php
    -::Ujjwal::-
    Visit My Site

  11. #11

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either
    PHP Code:
    <?php
        session_start
    ();
        echo 
    "YOUR choices are {$_SESSION['fruit']} and {$_SESSION['animal']}";
    ?>
    or
    PHP Code:
    <?php
        session_start
    ();
        echo 
    'YOUR choices are '.$_SESSION['fruit'].' and '.$_SESSION['animal'];
    ?>

  12. #12
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you sir i have correct it
    -::Ujjwal::-
    Visit My Site

  13. #13
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    regarding that can i test if any user miss to choose 4th answer then remind him you have to choose 4th answer(asssuming that each page has 5 questions)
    -::Ujjwal::-
    Visit My Site

  14. #14

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no 4th answer, only two, R1 and R2.

    But you can check with
    PHP Code:
    if (isset($_POST['varname'])==false

  15. #15
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but sir if assume that there is 5th qs and 4th one is miss by any user.and this 4th is written in html so how to remind this user?
    PHP Code:
    <?php
    if(isset($_POST['B1']))
    {
        
    session_start();
        
    $_SESSION['fruit']=$_POST['R1'];
        
    $_SESSION['animal']=$_POST['R2'];
        
    $_SESSION['flower']=$_POST['R3'];
        
    $_SESSION['dish']=$_POST['R4'];
        
    $_SESSION['color']=$_POST['R5'];
        
    header('Location: rd2.php'); 
    exit(); 
    }

    ?>
    <form method="POST" action="<? echo $_SERVER['PHP_SELF'?>">
      
      <p>1.What is your Favourite Fruit?</p>
      <p><input type="radio" value="Mango"  name="R1">Mango</p>
      <p><input type="radio" name="R1" value="Lichi">Lichi</p>
      <p>2.What is your favorite animal?</p>
      <p><input type="radio" name="R2" value="Tiger">Tiger</p>
      <p><input type="radio" name="R2" value="Lion">Lion</p>
     
      <p>3.What is your Favourite Flower?</p>
      <p><input type="radio" value="Rose"  name="R3">Rose</p>
      <p><input type="radio" name="R3" value="SunFlower">SunFlower</p>
      <p>4.What is your Favourite dishes?</p>
      <p><input type="radio" value="Chienese"  name="R4">Chienese</p>
      <p><input type="radio" name="R4" value="Moglai">Moglai</p>
      <p>5.What is your Favourite Color?</p>
      <p><input type="radio" value="Blue"  name="R5">Blue</p>
      <p><input type="radio" name="R5" value="Red">Red</p>
      <p><input type="submit" value="B1" name="B1"></p>
    </form>
    plz help me
    -::Ujjwal::-
    Visit My Site

  16. #16
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I don't understand is: so far the action of form:
    <form method="POST" action="<? echo $_SERVER['PHP_SELF'] ?>">
    is the script itself, it will allways return to itself
    <form method="POST" action="otherscript.php">
    Then it goes there ?
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  17. #17

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following should do the trick
    HTML Code:
    <?php 
    if(isset($_POST['B1'])) 
    {
        if ((isset($_POST['R1']) && isset($_POST['R2']) && isset($_POST['R3']) && isset($_POST['R4']) && isset($_POST['R5']))==false)
        {
            exit('Answer missing');
        }
    
        session_start(); 
        $_SESSION['fruit']=$_POST['R1']; 
        $_SESSION['animal']=$_POST['R2']; 
        $_SESSION['flower']=$_POST['R3']; 
        $_SESSION['dish']=$_POST['R4']; 
        $_SESSION['color']=$_POST['R5']; 
        header('Location: rd2.php'); 
        exit(); 
    } 
    
    ?> 
    <form method="POST" action="<? echo $_SERVER['PHP_SELF'] ?>"> 
       
      <p>1.What is your Favourite Fruit?</p> 
      <p><input type="radio" name="R1" value="Mango">Mango</p> 
      <p><input type="radio" name="R1" value="Lichi">Lichi</p> 
    
      <p>2.What is your favorite animal?</p> 
      <p><input type="radio" name="R2" value="Tiger">Tiger</p> 
      <p><input type="radio" name="R2" value="Lion">Lion</p> 
    
      <p>3.What is your Favourite Flower?</p> 
      <p><input type="radio" name="R3" value="Rose">Rose</p> 
      <p><input type="radio" name="R3" value="SunFlower">SunFlower</p> 
    
      <p>4.What is your Favourite dishes?</p> 
      <p><input type="radio" name="R4" value="Chienese">Chienese</p> 
      <p><input type="radio" name="R4" value="Moglai">Moglai</p> 
    
      <p>5.What is your Favourite Color?</p> 
      <p><input type="radio" name="R5" value="Blue">Blue</p> 
      <p><input type="radio" name="R5" value="Red">Red</p> 
    
      <p><input type="submit" value="B1" name="B1"></p> 
    </form>


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
  •