Reputation: 45
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.
Upvotes: 3
Views: 7328
Reputation: 189
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']; ?>
.
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! :)
Upvotes: 1
Reputation: 2289
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
Upvotes: 2
Reputation: 20913
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";
}
?>
Upvotes: 1
Reputation: 59859
Well I noticed you're missing a closing "
on the last echo, instead change to:
echo "$MONTH_START;"
(assuming it wasn't a typo)
Upvotes: 2