Home > Net >  Need form to redirect user if they select age below 18
Need form to redirect user if they select age below 18

Time:07-06

I am working on a site with an html form, and the form requires the user to submit their age.

The user's age will be used for various purposes through-out the website's program.

However, if the user is under the age of 18, they are not eligible to participate in the program anyway.

Therefore... if the user selects 'Under 18' in the form, and then tries to click the 'continue' (submit) button, I need the form to automatically redirect the user to another URL.

I have tried several php, and a few javascript solutions, but so far, no dice.

And help or advice would be greatly appreciated.

I have included the form page which asks for the user's age.

Form Page

<!DOCTYPE html>

<head>

<title>Form Page</title>

</head>

<body>

<form action="nextPage.php" method="POST">

Please select your age:<br><br>

<select id="userAge" name="userAge"  required>
<option value="0" selected="selected" disabled="disabled">Age</option>
<option value="Under 18">Under 18</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
<option value="51">51</option>
<option value="52">52</option>
<option value="53">53</option>
<option value="54">54</option>
<option value="55">55</option>
<option value="56">56</option>
<option value="57">57</option>
<option value="58">58</option>
<option value="59">59</option>
<option value="60 ">60 </option>
</select>

<br><br><br>


<input type="submit"  value="Continue">


</form>

</div>   

</body>

</html>

CodePudding user response:

I would do this in Javascript Before its submitted to the PHP page

function detectChange(ageData) {
    console.log(ageData.value)

  if (ageData.value == 10)
  {
  console.log("Kick user")
 window.location.href = "http://www.w3schools.com";
  }
}
<select id="osDemo" onchange="detectChange(this)">
    <option value="None">None</option>
    <option value="10">under18</option>
    <option value="19">19</option>
    <option value="20">20</option>
</select>

  • Related