-2

I have a drop down menu in HTML that asks the user to choose whether they want to be directed to the user feedback form or the user help form. I need to use the get method and PHP to dynamically redirect the user to the form of their choice.

            <!-- Div for drop down menu -->
    <div class="dropDown">
    <select id="select" onclick="myFunction">
    <form method="get" action="assignment5-2.php" name="dropDown">
    <option disabled selected>Choose to fill out help form or general feedback form please.</option>
    <option id="user" value="user">User Feedback Form</option>
    <option id="value" value="help">User Help Form</option>
    </select>
    <input type="button" value="Go" onclick="myFunction()"> 
    </div>
    
Joe Romo
  • 5
  • 1
  • Surely it would be easier to do this in the JS `myFunction()` than in PHP? Saves an extra trip to the server and back, for one thing. And also...where are you stuck? What have you tried? What's the actual problem you're encountering? We're happy to help you at Stackoverflow if needs be, but processing form data and making redirects are both tasks which are extremely well-documented online with examples, tutorials etc. And that's equally true if you take the JavaScript route, too. There should be no reason you can't make a start on this yourself, and come back if you get stuck. See also [ask]. – ADyson Apr 01 '23 at 00:17
  • @ADyson The reason I'm using PHP is because that's what my Professor requested. We've already completed the assignment using Javascript and I had it working perfectly. Now we're recreating the assignment using PHP which is insane. I'm brand new with PHP so I'm kind of lost but I am trying to figure out how to load the forms using the PHP Get array. This is the PHP code I've added so far: – Joe Romo Apr 01 '23 at 00:30
  • @ADyson Hello PHP. "; if(isset($_GET['user'])) { echo "Hello User Form."; echo "
    "; } if(isset($_GET['help'])) { echo "Hello Help Form."; echo "
    "; } ?>
    – Joe Romo Apr 01 '23 at 00:31
  • 1
    The answer below is correct. Your is wrong because 1) there's no "name" on the "select" so it won't be submitted. 2) The GET value will have the name of the select, when you provide one. The values from the options will be the _value_ of the GET parameters, not its name. 3) You haven't specified a redirect. As well as the answer below, take a look at https://developer.mozilla.org/en-US/docs/Learn/Forms/Your_first_form and https://www.php.net/manual/en/tutorial.forms.php and https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php – ADyson Apr 01 '23 at 07:09

1 Answers1

1

Since it is your professor's request (to do it thru PHP) , you may do it as follows:

  1. Thru the select box, pass the GET parameter to the PHP as "choice" with either "user" or "help" as value
  2. In the PHP, do a redirection (header.location) to the appropriate page

Just a few comments on your original code:

  1. If you submit the form thru get method to a PHP script, the normal way will be using form submission say simply using a submit button

  2. There will only be one get parameter if you are only having one select box

So the code is (amend further to suit your further needs if you want):

DEMO

HTML

    <div class="dropDown">

    <form method="get" action="assignment5-2.php" name="dropDown">

Choose to fill out help form or general feedback form please :<br>
    <select name="choice">
    <option  value="user">User Feedback Form</option>
    <option  value="help">User Help Form</option>
    </select>
    <input type="submit" value="Go"> 
    </div>

PHP (assignment5-2.php)

<?php 

//echo "<h1> Hello PHP. </h1>"; 

if (isset($_GET['choice'])) {

  if($_GET['choice']=='user') { 
//  echo "Hello User Form."; 
//  echo "<div id='userForm' style='display: block'>"; 
    header("Location: user.php");
    exit();
  } 

  if($_GET['choice']=='help') { 
//  echo "Hello Help Form."; 
//  echo "<div id='helpForm' style='display: block'>"; 
    header("Location: help.php");
    exit();
  } 
   }else{
 echo "You have NOT chosen either help or user !";
    }


?>

help.php

Hi, this is User Help Form

user.php

Hi, this is User Feedback Form
Ken Lee
  • 6,985
  • 3
  • 10
  • 29