multiple choice script with a tweak

by 8 replies
10
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
#programming #choice #multiple #script #tweak
  • 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...
    • [1] reply
    • 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"];


      switch ($choice){
      case "1":
      ...
      header( 'Location: http://www.yoursite.com/page1.html' );
      exit();
      case "2":
      ...
      header( 'Location: http://www.yoursite.com/page2.html' );
      exit();
      case "3":
      ...
      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.
  • 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.
    • [1] reply
    • 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
      • [1] reply
  • @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;
    • [1] reply
  • Banned
    [DELETED]

Next Topics on Trending Feed