Isabella Isabella - 4 months ago 17
PHP Question

PHP page redirect depending on jQuery selectmenu choice

I need to redirect my selectmenu options using PHP. Basically, I need to be able to choose a teacher in the selectmenu and when I submit the page, it redirects to that specific teachers page.

<?php
ob_start();
session_start();
require_once 'dbconnect.php';

if( !isset($_SESSION['client']) ) {
header("Location: homepage_login.php");
exit;
}
// select loggedin users detail
$res=mysql_query("SELECT * FROM clients WHERE client_id=".$_SESSION['client']);
$userRow=mysql_fetch_array($res);


if( isset($_POST['btn-nxt-page']) ) {

$client_student_id = $_POST['client_student_id'];
$ss_name = $_POST['ss_name'];

$client_student_id = strip_tags(trim($client_student_id));
$ss_name = strip_tags(trim($ss_name));

$count = mysql_num_rows($result);

if ($count) {

$query = "INSERT INTO appointments(client_student_id,ss_name,) VALUES('$client_student_id','$ss_name')";
$res = mysql_query($query);
}

function redirect($where){
header("Location: $where");
}
if ($_REQUEST['ss_name'] == 'John'){
redirect('http://localhost/homepage_loggedin_book_john.php');
}elseif($_REQUEST['ss_name'] == 'Smith'){
redirect('http://localhost/homepage_loggedin_book_smith.php');
}
}
?>


This is my HTML, as you can see I have options and then in my PHP i try to redirect but it's not working...

<div class="form-group">
<div class="input-group">
<input type="text" name="client_student_id" class="form-control" placeholder="Enter your Student ID" required />
</div>
</div>
<br>
<br>

<label for="ss" id="menu">Select a teacher</label>
<select name="ss_name" id="#menu">
<option value="John">John</option>
<option value="Smith">Smith</option>
<option value="Greg">Greg</option>
<option value="Jess">Jess</option>
</select>
<br>
<br>

<div class="form-group">
<button type="submit" class="btn btn-block btn-primary" name="btn-nxt-page">Next Page</button>
</div>


Any help would be awesome. I just really need to get this working so I can move on.

If I'm not being specific enough:

I need the selectmenu options to be able to redirect to a new page when chosen and submitted.

Also, if anyone could help me to add in the values I'm entering through the selectmenu and the text box into my database that would be awesome!!

Answer

Replace the code below and get rid of the redirect function.

if ($_REQUEST['ss_name'] == 'John'){
    redirect('http://localhost/homepage_loggedin_book_john.php');
}elseif($_REQUEST['ss_name'] == 'Smith'){
    redirect('http://localhost/homepage_loggedin_book_smith.php');
}

if ($_REQUEST['ss_name'] == 'John') {
    header("Location: http://localhost/homepage_loggedin_book_john.php");
} else if ($_REQUEST['ss_name'] == 'Smith') {
    header("Location: http://localhost/homepage_loggedin_book_smith.php");
} 

Try this and post any errors.