the bottom line of what I want to do is-

I have a file with a form (form.php) that has 2 drop lists, on sumbit I want to be redirected to another page (1.php) if dropbox 1 was set, to page 2.php if droplist 2 was set and to page 3.php if both droplists were set.
is it possible?? I don't get the header() thing..but I don't think that's what I'm looking for..
a think there sould be another file in the middle that has a switch or something and redirects to the different pages after the submit


form.php->redirect.php->1.php
__________________->2.php
__________________->3.php

so is there a function for that?

Dani AI

Generated

A simple, robust way is to post the form to a small server-side router (redirect.php) that examines the two select values and issues an HTTP redirect. This avoids client-side delays and implements the Post/Redirect/Get pattern so the browser won't resubmit the form if the user refreshes. The key points are: decide destination, whitelist allowed targets, send a Location header, then stop execution.

<?php
// redirect.php (form method="post")
$sel1 = !empty($_POST['select1']);
$sel2 = !empty($_POST['select2']);

$dest = '/form.php'; // fallback

if ($sel1 && $sel2) {
    $dest = '/3.php';
} elseif ($sel1) {
    $dest = '/1.php';
} elseif ($sel2) {
    $dest = '/2.php';
}

// whitelist to avoid open-redirects
$allowed = ['/1.php','/2.php','/3.php','/form.php'];
if (!in_array($dest, $allowed, true)) {
    $dest = '/form.php';
}

// send 303 so POST converts to GET at the destination
header('Location: ' . $dest, true, 303);
exit;
?>

Troubleshooting notes: the redirect must be sent before any output (no HTML, whitespace, or echo), otherwise PHP will warn and the header will fail. Put this logic at the top of redirect.php, or use output buffering. Validate/select element names (the name attributes in the form) and method (POST vs GET). Always whitelist destinations to prevent malicious redirects.

As showed a client-side/meta refresh option, that works but is less reliable and slower; header() server redirects are preferred. If you need a client-side-only solution (no server processing), set the form action dynamically with JavaScript before submit, but keep a server-side fallback for users without JS.

Recommended Answers

All 2 Replies

the bottom line of what I want to do is-

I have a file with a form (form.php) that has 2 drop lists, on sumbit I want to be redirected to another page (1.php) if dropbox 1 was set, to page 2.php if droplist 2 was set and to page 3.php if both droplists were set.
is it possible?? I don't get the header() thing..but I don't think that's what I'm looking for..
a think there sould be another file in the middle that has a switch or something and redirects to the different pages after the submit


form.php->redirect.php->1.php
__________________->2.php
__________________->3.php

so is there a function for that?

if option 1
{
$url=www.one.com;
echo "<meta http-equiv=\"refresh\" content=\"1;URL=$url\" />";
}
else if option 2
{
$url=www.two.com
echo "<meta http-equiv=\"refresh\" content=\"1;URL=$url\" />
}

.. and so on

thank you so much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.