Execute a condition if form values are not set in php

Execute a condition if form values are not set in php



I am trying a execute a piece of code if form values are not set, but it's not working properly.



Here is my form:


<form method="post" action="downloadreport.php">
<div class="col-md-12" >
<div class="form-group label-floating">
<label class="control-label">From Date</label>
<input type="text" class="form-control" id="datepicker" name="datepicker" >
</div>
</div>
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">To Date</label>
<input type="text" class="form-control" id="datepicker1" name="datepicker1" >
</div>
</div>
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Select Usertype</label>
<select name="trackforums" id="trackforumns" class="form-control" >
<option value="1">Active</option>
<option value="0">Inactive</option>
<option value="3">Active/Inactive</option>
</select>
</div>
</div>
<div class="col-md-12">
<div class="form-group label-floating">
<button type="submit" name="submit" value="submit" class="btn btn-info form-control">Download Report</button>
</div>
</div>
</form>



Here are my form values


if (isset($_POST['submit']))
$fromdate =$_POST['datepicker'];
$todate = $_POST['datepicker1'];
$usertype= $_POST['trackforums'];

if($todate >= $fromdate)
//execute report with dates
elseif(isset($_POST[fromdate]) && $_POST[todate] = "")
//execute full report
else
echo "Your To date should be greater than from date";




When I have tried this, it is not going to the elseif condition if I didn't enter any dates in form field.





when i try with var_dump($_POST) getting like this array(4) ["datepicker"]=> string(0) "" ["datepicker1"]=> string(0) "" ["trackforums"]=> string(1) "1" ["submit"]=> string(6) "submit"
– user200
Aug 23 at 5:14






Don't use comments to add details. Edit your post with important informations.
– franiis
Aug 23 at 5:17





its a huge code inside conditions thats y i have kept like this
– user200
Aug 23 at 5:18






I mean your comment under question when i try with var_dump($_POST) .... Comments in code are fine, if all important details are left in question.
– franiis
Aug 23 at 5:20


when i try with var_dump($_POST) ....





$_POST[todate] = "" doesn't check a value, it sets a value. It should be $_POST[todate] == "".
– Magnus Eriksson
Aug 23 at 5:35


$_POST[todate] = ""


$_POST[todate] == ""




1 Answer
1



Your form is delivering empty strings when the date values are missing; empty() can check that. empty() offers the added benefit of checking for missing keys, so you won't get Notices.


empty()


empty()



Untested Suggested Condition Block:


if (!isset($_POST['submit']))
echo "No submission";
elseif (empty($_POST['datepicker']) || empty($_POST['datepicker1']))
echo "at least one date picker is empty, treat as no dates picked";
elseif ($_POST['datepicker1'] >= $_POST['datepicker'])
echo "invalid To date";
else
echo "execute report within valid date range





yes i have looked at can you please make your answer with only 3 conditions as per my question? as i am little bit confused with those conditions
– user200
Aug 23 at 5:48






I believe this improves your validation process. What happens when $_POST['datepicker'] is empty? Do you execute something in this case?
– mickmackusa
Aug 23 at 5:50


$_POST['datepicker']





No actually in my case if user want to download excel either they will enter both from and todate orelse they wont enter any values in from and todate
– user200
Aug 23 at 5:51





You understand my problem rite?
– user200
Aug 23 at 5:55





For _POST['datepicker1'] >= $_POST['datepicker'] to logically work, you need both date picker values to be "non-empty". This is the reason for my first elseif check.
– mickmackusa
Aug 23 at 6:14



_POST['datepicker1'] >= $_POST['datepicker']


elseif






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)