Hello

I'm using a form with a dropdown list. When the user select "Test1" in the dropdown then press the button "Submit" it has to send the data to .

When the user select "Test2" in the dropdown then press the button "Submit" it has to send the date to .

Also the data they've filled in has to come with.

I'm working with a datacheck.php, so when the press submit it post to datacheck.php and there I have following code:

<?php

if(isset($_POST['TEST1']) && $_POST['TEST1']== 'Y'){
header("Location: ");
}
else {
header("Location: ");
}
?>

But it doesnt work, it goes directly to page2.php even when I've selected Test1 in the dropdown.

Does anyone has an idea?

Grts

Dani AI

Generated

Quick diagnosis: a Location redirect or changing document.location does not forward the form POST body — the browser issues a new GET to the target, so your form fields are lost. Also double-check the select element name/value you test in PHP: if your <select> is named destination, checking $_POST['TEST1'] will never be true. That explains why always landed on page2.

If the browser should submit the data directly to the chosen endpoint, the simplest fix is to set the form action just before submit so the POST goes to the right URL:

document.getElementById('myForm').addEventListener('submit', function () {
  var sel = document.getElementById('destination').value; // value = target URL or key
  this.action = sel;
});

This keeps the request a POST and is the approach was pointing toward (but it must set the form action rather than doing a location redirect).

If you need to validate on your server first (datacheck.php) and then forward the data, relay the POST from PHP using cURL so the server posts to the external site:

$target = ($_POST['destination'] === 'test1') ? 'https://target1.example/receive.php' : 'https://target2.example/receive.php';
$ch = curl_init($target);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

Notes/troubleshooting: if you use header("Location: ...") remember it triggers a GET and should be followed by exit();. If you prefer browser POST after server validation, have datacheck.php render a hidden form with the POST fields and auto-submit it (this preserves the client context). Sanitize inputs, handle SSL/timeouts with cURL, and use var_dump($_POST) while debugging to confirm keys/values. ’s handler idea is fine for same-domain includes, but for posting to other domains use one of the above methods.

Recommended Answers

All 4 Replies

I think you need to submit form using javascript, base-on your selection you fire some javascript function & than you make condition base-on your selection & write code for submit form.

That's correct but I'm not able to find it :)

Member Avatar for Member #120589

If you don't want to use js - simply have a general form handler file, e.g. handler.php

The form sends everything to that. Say the dropdown has a name of "myselect" and the values could be "first", "second" or "third" (just an example) you could do:

switch($_POST['myselect']){
 case "first":
  include("page1.php");
  break;

 case "second":
  include("page2.php");
  break;

 case "third";
  include("page3.php");
  break;
}

Of course this will only work (include) if the form page is in the same domain and the pages are only there to process the data. If the pages contain full html pages. No, it won't be appropriate.

Please try this as per your code:

make javascript function in your selection box.

onChange=funRedirect(this)

<script type="text/javascript" language="JavaScript">


function funRedirect(obj) {

 if (obj.value == 'Test1')
 {
     document.location.href ="www.test1.com";
 }
 else
 {
    document.location.href ="www.test2.com";
 }
}

</script>
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.