Home > Enterprise >  HTML form - use the entered values and redirect
HTML form - use the entered values and redirect

Time:06-03

I want to make a form that will redirect the user to a subpage /variant with 2 values entered in the form.

For example, the user types red, green and is redirected to example.com/variant/red-green

``

<form action="/variant/" method="POST">
  <input type="text" name="first">
  <input type="text" name "second">
   
  <input type="submit">
</form>

``

CodePudding user response:

You can use the header() directive to redirect the user. Place this at the very top of your page:

<?php

if ( isset($_POST['first'] ) {
  header('Location: /variant/' . $_POST['first'] . '-' . $_POST['second']);
  exit();
}

?>

CodePudding user response:

You will need Javascript for that buddy. for example:

<script>
const text1 = document.getElementsByName("first").value;
const text2 = document.getElementsByName("second").value;

document.getElementsByName("submit").addEventListener("submit", ()=>{
    window.location.href = `www.example.com\variant\ ${text1}-${text2}`;
})
</script>

then you can do whatever you want with that value .

  • Related