multiple choice script with a tweak

8 replies
Hi,

I need a simple multiple choice script that will allow me to do two things.

1) set the number of choices per question as I wish

2) when someone clicks their answer/choice they are immediately taken to a page determined by me. Each possible choice can be linked to a different page.

Sounds simple and it can be done with html links but I want to do it with a script.

Funny, can't find a script though.

Anyone?

Tom
#choice #multiple #script #tweak
  • Profile picture of the author Anditya
    Ya, sound simple... Cant be done JUST with HTML 'select' tag. Must use JS to process the request.

    Must coding with PHP + SQL if the list is thousand or using Alphabet section...
    {{ DiscussionBoard.errors[6094461].message }}
    • Profile picture of the author Brandon Tanner
      Assuming you want to use a drop-down menu for the multiple choices, then copy and paste the following code into your webpage...

      <form action="multiple_choice.php" method="post" name="multiple_choice">

      <select name="choice">
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
      </select>

      <br />

      <input type="submit" value="Submit" />

      </form>


      Then create a php file named multiple_choice.php and paste the following code in it...

      <?php

      $choice = $_POST["choice"];
      // Gets the value of "choice"

      switch ($choice){
      case "1":
      // If the choice is "1", then go to the following URL...
      header( 'Location: http://www.yoursite.com/page1.html' );
      exit();
      case "2":
      // If the choice is "2", then go to the following URL...
      header( 'Location: http://www.yoursite.com/page2.html' );
      exit();
      case "3":
      // If the choice is "3", then go to the following URL...
      header( 'Location: http://www.yoursite.com/page3.html' );
      exit();
      }

      ?>


      The text in red above describes what the code does. So just change the URL's above to the correct pages on your site. And you can add as many choices/URL's as you want... I just used 3 here as an example.
      Signature

      {{ DiscussionBoard.errors[6094741].message }}
  • Profile picture of the author Anditya
    Sorry, because I use my phone here so I not give any example... Try this very simple JS + HTML than using HTML + PHP :

    <select multiple onChange="window.location=this.options[this.selectedIndex].value;">
    <option value="1.html">Page 1</option>
    <option value="FULL URL">Page 2</option>
    </select>

    * Sorry again, I use my phone to type above code, just try if work at your page.
    {{ DiscussionBoard.errors[6094897].message }}
    • Profile picture of the author easy does it
      Thanks guys,

      Brandon, I like your solution. Looked nice on my site but I got error 404, maybe I put the .php in the wrong place in wordpress.

      This is how I solved the problem. I decided I needed radio buttons or checkboxes with each choice redirecting to a new page and no possibility of using the back button... Brandon if you're able to do this in .php I'm curious. Anyway here is my solution... very simple but it took me a looong time.

      <input type="radio" onclick="window.location.replace('http://www.yahoo.com'); return true;"> yahoo<div></div>
      <input type="radio" onclick="window.location.replace('http://www.w3schools.com'); return true;"> w3<div></div>
      <input type="radio" onclick="window.location.replace('http://www.w3schools.com'); return true;"> Click Here<div></div>

      Tom
      {{ DiscussionBoard.errors[6099593].message }}
      • Profile picture of the author Brandon Tanner
        Originally Posted by easy does it View Post

        Thanks guys,

        Brandon, I like your solution. Looked nice on my site but I got error 404, maybe I put the .php in the wrong place in wordpress.

        This is how I solved the problem. I decided I needed radio buttons or checkboxes with each choice redirecting to a new page and no possibility of using the back button... Brandon if you're able to do this in .php I'm curious. Anyway here is my solution... very simple but it took me a looong time.

        <input type="radio" onclick="window.location.replace('http://www.yahoo.com'); return true;"> yahoo<div></div>
        <input type="radio" onclick="window.location.replace('http://www.w3schools.com'); return true;"> w3<div></div>
        <input type="radio" onclick="window.location.replace('http://www.w3schools.com'); return true;"> Click Here<div></div>

        Tom
        I just assumed you were using a regular HTML site. But the code should work on any platform, as long as the relative path between the files is correct. Sometimes it's difficult to figure out the actual physical location of pages/files in WordPress though, since there are so many different themes and possible configurations. WordPress is really not one of my strong points, so maybe someone else can chime in about that.

        Regarding your Javascript solution, that will work for most of your site's visitors, but it obviously won't work for anyone who has Javascript disabled in their browser. That's a relatively small percentage of folks, but it's something to keep in mind.
        Signature

        {{ DiscussionBoard.errors[6104574].message }}
  • Profile picture of the author SteveJohnson
    @Brandon - the header calls in your switch statements need to be followed by exit();, not break;. The simple sending of a header doesn't stop script execution. Not killing the script specifically after sending a redirect header can cause unexpected behavior.

    @Tom - you shouldn't depend on javascript to defeat the 'back' button, because it doesn't always work. The best you can hope for is to defeat a resubmission of form data. In your form processing, use sessions to determine if the form has already been submitted. Basics:

    session_start();
    if ( isset( $_SESSION['redirect_url']) and ! empty( $_SESSION['redirect_url'] ) ) :
    // redirect to originally redirected page
    header( 'Location: ' . $_SESSION['redirect_url'] );
    exit();
    else :
    // do your switch/redirection statement, but set the $_SESSION['redirect_url']
    // variable before redirection
    endif;
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[6105605].message }}
    • Profile picture of the author Brandon Tanner
      Originally Posted by SteveJohnson View Post

      @Brandon - the header calls in your switch statements need to be followed by exit();, not break;.
      Good point! I have updated the code.
      Signature

      {{ DiscussionBoard.errors[6105750].message }}
      • Profile picture of the author easy does it
        Hi Steve,

        As you can tell I'm not a coder so adding the session stuff is probably beyond me. If the javascript I have works most of the time in defeating the back button I'll be happy. My purpose was to limit the number of page calls.

        Brandon

        I really don't want to miss out on non-javascript visitors but I like the idea of radio buttons redirecting to pages, looks neat. I guess I could have (html) images linking to my pages but I'm still undecided.

        Tom
        {{ DiscussionBoard.errors[6111638].message }}

Trending Topics