简体   繁体   中英

PHP my form wont set submit

I'm a beginner in php , and I wanted to do some form that hide after you press submit button and tell you value of input, but it doesn't work. I think there is some problem with my submit button ( i spend like 2 hours trying to solve this problem). Thanks

    <!DOCTYPE HTML>
<html>
<head>
<title>Jednoduchý formulář</title>
</head>
<body>
  <?php
    $show = True;
  if(isset($_GET['submit'])){
    if(empty($_GET['jmeno'])){echo "Vyplnte jmeno";}else {
      if (empty($_GET['prijmeni'])){echo "Vyplnte prijmeni";}else{

    echo "Jméno: ".$_GET['jmeno'];
        echo "<br>";
    echo "Prijmeni: ".$_GET['prijmeni'];
        echo "<br>";
    echo "Je Vám ".$_GET['vek'];
        echo "<br>";
    echo "Děkuji za vyplnění formuláře";

    $show=False;
  };
  };
};

  if ($show){ ?>
<form  action="form1.php" method="get" >
<table width="250" align="center">
    <tr>
     <td>Jmeno:</td> <td><input type="text" name="jmeno" value="" size="10" maxlength="25"></td>
    </tr>

    <tr>
     <td>Prijmeni:</td> <td><input type="text" name="prijmeni" value="" size="10" maxlength="25"></td>
    </tr>

    <tr>
     <td>Heslo:</td> <td><input type="password" name="heslo" value="" size="10" maxlength="15"></td>
    </tr>

    <tr>
    <td>Vek</td> <td><input type="radio" name="vek" value="méně než 18 let "checked> mene nez 18  <br>
                     <input type="radio" name="vek" value="18 let a více">  18 a vice  </td>
    </tr>
    <tr>
    <td></td><td><input type="submit" value="submit"></td>
    </tr>
</table>
</form>

<?php
    };
  ?>
</body>
</html>

I think that's because you have not set the name of Submit input, but you are verifiying that input in PHP to be isset:

Change your code to.

<input type="submit" value="submit" name="submit">

You should be using $_POST() , beyond that you need to name the submit input, then check if it's set.

<?php

IF (isset($_POST['form1'])) {

    $err = "";
    IF (!empty($_POST['jmeno'])) { $jmeno = $_POST'jmeno']; }ELSE{ $err .= "jmeno is empty.<br>"; }
    IF (!empty($_POST['prijmeni'])) { $prijmeni = $_POST'prijmeni']; }ELSE{ $err .= "prijmeni is empty"; }
    IF (!empty($_POST['heslo'])) { $heslo = $_POST'heslo']; }ELSE{ $err .= "heslo is empty"; }
    // ...... etc.

    IF (!empty($err)) {

        $result = "<p>".$err."</p>";
        $result .= "<form action=\"form1.php\" name=\"form1\" method=\"post\" >
<table width=\"250\" align=\"center\">

    <tr>
     <td>Jmeno:</td> <td><input type=\"text\" name=\"jmeno\" value=\"".$_POST['jmeno']."\" size=\"10\" maxlength=\"25\"></td>
    </tr>

    <tr>
     <td>Prijmeni:</td> <td><input type=\"text\" name=\"prijmeni\" value=\"".$_POST['prijmeni']."\" size=\"10\" maxlength=\"25\"></td>
    </tr>

    <tr>
     <td>Heslo:</td> <td><input type=\"password\" name=\"heslo\" value=\"".$_POST['heslo']."\" size=\"10\" maxlength=\"15\"></td>
    </tr>

    <tr>
    <td>Vek</td> <td><input type=\"radio\" name=\"vek\" value=\"méne než 18 let \"checked> mene nez 18  <br>
                     <input type=\"radio\" name=\"vek\" value=\"18 let a více\">  18 a vice  </td>
    </tr>
    <tr>
    <td></td><td><input type=\"submit\" name=\"form1\" value=\"submit\"></td>
    </tr>
</table>
</form>";

    }ELSE{

        // everything is good to go....
        $result = "whatever the next html body should be.";

    }

}ELSE{

    $result = "<form action=\"form1.php\" name=\"form1\" method=\"post\" >
<table width=\"250\" align=\"center\">

    <tr>
     <td>Jmeno:</td> <td><input type=\"text\" name=\"jmeno\" value=\"\" size=\"10\" maxlength=\"25\"></td>
    </tr>

    <tr>
     <td>Prijmeni:</td> <td><input type=\"text\" name=\"prijmeni\" value=\"\" size=\"10\" maxlength=\"25\"></td>
    </tr>

    <tr>
     <td>Heslo:</td> <td><input type=\"password\" name=\"heslo\" value=\"\" size=\"10\" maxlength=\"15\"></td>
    </tr>

    <tr>
    <td>Vek</td> <td><input type=\"radio\" name=\"vek\" value=\"méne než 18 let \" checked> mene nez 18  <br>
                     <input type=\"radio\" name=\"vek\" value=\"18 let a více\">  18 a vice  </td>
    </tr>
    <tr>
    <td></td><td><input type=\"submit\" name=\"form1\" value=\"submit\"></td>
    </tr>
</table>
</form>";

}

// display results
echo("<!DOCTYPE HTML>
<html>
<head>
<title>Jednoduchý formulár</title>
</head>
<body>
".$result."
</body>
</html>");

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM