Home>

I want to use the value entered in the form in the redirect

When I redirect, I lose the value I entered in the form.
I want to send the name and value when I somehow redirect.

Corresponding source code
<? php
// Declare variables
$msg1 = "";
$msg2 = "";
$flg1 = "";
$flg2 = "";
$number1 = "";
$number2 = "";
$error = "";
// Error handling ①
if (isset ($_GET ['num1']) == true) {
    if ($_GET ['num1'] == "") {
        $msg1 = "Number 1: Please enter a value in half-width numbers";
        $flg1 = 2;
    }
    elseif (! is_numeric ($_GET ['num1']) == true) {
        $msg1 = "Number 1: Only numbers can be entered";
        $flg1 = 2;
    }
    else {
        $number1 = $_GET ['num1'];
        $flg1 = 1;
        $msg1 = "";
    }
}
// Error handling ②
if (isset ($_GET ['num2']) == true) {
    if ($_GET ['num2'] == "") {
        $msg2 = "Number 2: Please enter a value in half-width numbers";
        $flg2 = 2;
    }
    elseif (! is_numeric ($_GET ['num2']) == true) {
        $msg2 = "Number 2: Only numbers can be entered";
        $flg2 = 2;
    }
    else {
        $number2 = $_GET ['num2'];
        $flg2 = 1;
        $msg2 = "";
    }
}
// Redirect process
if ($flg1 == 1&&$flg2 == 1) {
    header ('Location:');
    exit;
}
else {
    $error = "There is an error in the input";
}
if (empty ($flg1) == true&&empty ($flg2) == true) {
    $error = "";
}
?>
<! DOCTYPE html>
<html>
<head>
 <meta charset = "UTF-8">
 <title>Multiplication system</title>
</head>
<body>
     It is a system that performs multiplication 
  <p>Please enter numbers in half-width</p>
  <form method = "get" action = "./ ep17-task_input_improvement-2.php"><!-If i write this way, you can move to php of the same file->
  <table border = "1">
      <tr>
          <td>Number 1</td><td><input type = "text" name = "num1" value = "<? php echo $number1?>"></td>
          <td>×</td>
          <td>Number 2</td><td><input type = "text" name = "num2" value = "<? php echo $number2?>"></td>
      </tr>
      <tr>
          <th colspan = "5"><button type = "submit">Display</button></th>
      </tr>
  </table>
  </form>
  <? php echo $error;?><br>
  <? php echo $msg1;?><br>
  <? php echo $msg2;?><br>
</body>
</html>
What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    Since form transmission is a request only to the specified destinationNew requestIt goes without saying that nothing is sent by the redirect by the header that will send.
    Decide what you want to do and how you want to be redirected. If you want to use a session or connect with GET, you can create your own query string, or you can implement it without redirecting.

  • Answer # 2

    If you attach it to Location of header as an argument, you may be able to pass it.

    header ("Location: HOGE? Num1 = $number1&num2 = $number2");