Forum Moderators: coopster

Message Too Old, No Replies

Either or selection in form

Results from drop down or text entry

         

meganp

2:50 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Hi,

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>&nbsp;
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>&nbsp;</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...

jatar_k

6:58 pm on Jan 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would add a radio button before each type of date range and have it default to the first selection. Then you can check for which type was selected first and then do your var assignments from there. Your if statements in the one you have are a little mixed up. If you add the radio it should help the confusion.

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>&nbsp;
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>&nbsp;</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;
}
?>

meganp

8:12 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Brilliant, thank you!

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...

jatar_k

2:54 am on Jan 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you are quite welcome

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.