简体   繁体   中英

Need help saving php variable from HTML select dropbox

I thinks this is pretty easy but yet can't do it for the sake of my life. Is this too much to ask for?

 <select name="cars">
<?php
if (isset($_POST['Submit1'])) {
$car = $_POST['cars'];
}
?>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
</body>  


</head>
<body>
<Form name ="form1" Method ="POST" Action ="">
<Input Type = "text" Value ="<?php echo $car; ?>" Name ="word">
<Input Type = "Submit" Name = "Submit1" Value = "Submit">
</FORM>

The <select> tag is an HTML form element and in order to be submitted that tag would have to be inside the <form> tag.

Your new code should look like this:

    <body>
    <form name="form1" method="POST" action="">
     <select name="cars">
      <?php
       if (isset($_POST['Submit1'])) {
        $car = $_POST['cars'];
       }
       ?>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
     </select>
     <input type="text" value="<?php echo $car; ?>" name="word" />
     <input type="Submit" name="Submit1" value="Submit" />
    </form>
    </body>

Let me know if you have any other questions.

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