简体   繁体   中英

HTML form doesn't post or retrieve values

This is a standard HTML/PHP form. My code looks fine but for some reason, the echo statement (standard debug measure) doesn't work. Any ideas why?

<html>
<head>
<title>Search</title>
</head>
<body>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
Symbol: <input type="text" name="symbol" />
<br />
Start Date:
<select name="month_start">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

<br />

...
<input type="submit" value="Submit">
</form>

</body>
</html>

<?php
if(isset($_POST['submit'])) {

    $SYMBOL = $_POST['symbol'];
    echo "$SYMBOL";
    $MONTH_START = $_POST['month_start'] - 1;
    echo "$MONTH_START;

?>

Many thanks.

Well I noticed you're missing a closing " on the last echo, instead change to:

echo "$MONTH_START;" (assuming it wasn't a typo)

3 issues - first your form action should be <?php echo $_SERVER['PHP_SELF'] ?> , second, you have a syntax error in your 2nd echo statement, should be echo "$MONTH_START"; (You're missing the closing quote, although you don't need the quotes if you're only echoing the variable.) and lastly, you need to add name="submit" to your submit button when you're post variable contains the proper submit key for your display condition

You're not setting $_POST['submit'] , so it will never echo anything.
Test if a field (that you're using) is set:

<?php
 if(isset($_POST['symbol'])) {

   $SYMBOL = $_POST['symbol'];
   echo "$SYMBOL";
   $MONTH_START = $_POST['month_start'] - 1;
   echo "$MONTH_START";
}
?>

In order to get any value in the action property, you have to put an echo in from of the variable, like so: <?php echo $_SERVER['PHP_SELF']; ?> <?php echo $_SERVER['PHP_SELF']; ?> . However, this will not help, because you could just do this <form method="post" action=""> , and it would still POST to the same page.

Secondly, you need an extra " in the last line, making it echo "$MONTH_START"; .

Also, the variable you are trying to catch, will never be set, because it's not part of you form. Try using print_r($_POST); to see.

You get two variables: $_POST['symbol'] and $_POST['month_start'] - you should really be checking if one (or both) of them are set, before taking any action.

Hope it helps! :)

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