Forum Moderators: coopster
I've got a form for choosing a date range by either a drop down or user-entered data in a text field. If done separately (each method on its own page), they work. But in trying to combine them so that someone can choose one or the other from the same page, no data is displayed upon submit.
Choose date range by dropdown
or
Enter date values in text field for specific dates
I'm sure the problem has to do with passing the variable for the correct choice, but I'm at a standstill now. Can someone help, please? Here's what I have (relevant code):
<?php // show form if not already submitted
if (!$Submit) {
?>
<!-- select entries by date range -->
<form name="form1" method="POST" action="https://domain/affiliatesrev4_date.php">
<p>Select Date Range:</p>
<p>From
<select name="from1">
<option value="Select Date" selected>--Select Date</option>
<option value="2004-12-01 00:00:00">2004-12-01</option>
<option value="2005-01-01 00:00:00">2005-01-01</option>
<option value="2005-02-01 00:00:00">2005-02-01</option>
</select>
to
<select name="to1">
<option value="Select Date" selected>--Select Date</option>
<option value="2004-12-31 23:59:59">2004-12-31</option>
<option value="2005-01-31 00:00:00">2005-01-31</option>
<option value="2005-02-28 00:00:00">2005-02-28</option>
</select> </p>
<p>or enter dates in the format of yyyy-mm-dd:</p>
<p>From:
<input name="from2" type="text" id="from2">
to
<input name="to2" type="text" id="to2">
</p>
<p>
<input type="submit" name="Submit" value="View Sales">
<input type="reset" name="Submit2" value="Reset">
</p>
</form>
<?php }
else {
// create short variable names
$Submit = $_POST['Submit'];
if ((isset($_POST['from1'])) && (isset($_POST['from2']) == "")) {
$from = $_POST['from1'];
}
if ((isset($_POST['from2'])) && (isset($_POST['from1']) == "")) {
$from = $_POST['from2'];
}
if ((isset($_POST['to1'])) && (isset($_POST['to2']) == "")) {
$to = $_POST['to1'];
}
if ((isset($_POST['to2'])) && (isset($_POST['to1']) == "")) {
$to = $_POST['to2'];
}
// has form been submitted?
if (isset($_POST['Submit'])) {
// connect to the database
require_once ('../vsadmin/db_conn_open.php');
// Set last affiliate variable
$lastaffil='';
$affiliates = "SELECT orders.ordAffiliate, orders.ordName, orders.ordID, orders.ordAuthNumber, orders.ordDate, orders.ordTotal, affiliates.affilName FROM orders, affiliates WHERE orders.ordAffiliate = affiliates.affilID AND orders.ordAuthNumber!= '' AND ordDate BETWEEN '$from' AND '$to' ORDER BY orders.ordAffiliate, orders.ordDate DESC";
$result = mysql_query($affiliates);
--snip--
Thanks for any help...
try something like so
<?php // show form if not already submitted
if (!isset($_POST['Submit'])) {
?>
<form name="form1" method="POST" action="<?= $_SERVER['PHP_SELF']?>">
<p>Select Date Range:</p>
<p><input type="radio" name="whichrange" value="1" checked> From
<select name="from1">
<option value="Select Date" selected>--Select Date</option>
<option value="2004-12-01 00:00:00">2004-12-01</option>
<option value="2005-01-01 00:00:00">2005-01-01</option>
<option value="2005-02-01 00:00:00">2005-02-01</option>
</select>
to
<select name="to1">
<option value="Select Date" selected>--Select Date</option>
<option value="2004-12-31 23:59:59">2004-12-31</option>
<option value="2005-01-31 00:00:00">2005-01-31</option>
<option value="2005-02-28 00:00:00">2005-02-28</option>
</select> </p>
<p>or enter dates in the format of yyyy-mm-dd:</p>
<p><input type="radio" name="whichrange" value="2"> From:
<input name="from2" type="text" id="from2">
to
<input name="to2" type="text" id="to2">
</p>
<p>
<input type="submit" name="Submit" value="View Sales">
<input type="reset" name="Submit2" value="Reset">
</p>
</form>
<?php
} else {
// create short variable names
if ($_POST['whichrange'] == 1) {
echo '<p>type 1 was selected';
if (isset($_POST['from1']) && $_POST['from1']!= 'Select Date') $from = $_POST['from1'];
else $from = 'from1 is not set!';
if (isset($_POST['to1']) && $_POST['to1']!= 'Select Date') $to = $_POST['to1'];
else $to = 'to1 is not set!';
} else if ($_POST['whichrange'] == 2) {
echo '<p>type 2 was selected';
if (isset($_POST['from2']) &&!empty($_POST['from2'])) $from = $_POST['from2'];
else $from = 'from2 is not set!';
if (isset($_POST['to2']) &&!empty($_POST['to2'])) $to = $_POST['to2'];
else $to = 'to2 is not set!';
}
echo '<p>from: ',$from,'<br>to: ',$to;
}
?>
It works great, and I was able to concatenate the 00:00:00 and 23:59:59 to the "to" and "from" variables (which I had overlooked) for the text input portion.
Thanks for this... the radio buttons do make it easier, cleaner, and less confusing, and I've learned a lot from seeing how it works. The more I tried the other way, the more confused I got, as you could probably tell.
Thanks again...
that's why I am here all the time after all :)
when I initially build things I usually just spit out variables like that, it makes a massive difference in understanding what you are trying to do.
I built something similar and you should have seen the output, dumping tons to the screen so I could figure out all the intricacies of the script. Date math is such a pain and very easy to miss something.