Tuesday, September 27, 2016

Dynamic Query based on User Selection in HTML/PHP

<!DOCTYPE html>
<head>

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

$client = $_POST['client'];
$statusa = $_POST['statusa'];
$mobile = $_POST['mobile'];
$calldate_from = $_POST['calldate_from'];
$calldate_to = $_POST['calldate_to'];
$leaddate_from = $_POST['leaddate_from'];
$leaddate_to = $_POST['leaddate_to'];
$appdate_from = $_POST['appdate_from'];
$appdate_to = $_POST['appdate_to'];
$submitdate_from = $_POST['submitdate_from'];
$submitdate_to = $_POST['submitdate_to'];

$filters = array(
    'client'   => $client,
    'statusa'     => $statusa,
    'mobile' => $mobile,
    'calldate_from' => $calldate_from,
    'calldate_to' => $calldate_to,
    'leaddate_from' => $leaddate_from,
    'leaddate_to' => $leaddate_to,
    'appdate_from' => $appdate_from,
    'appdate_to' => $appdate_to,
    'submitdate_from' => $submitdate_from,
    'submitdate_to' => $submitdate_to
);

$where = 'WHERE';

$sql = "SELECT * FROM dharamart";

foreach ($filters as $field => $value) {
    if($value) {
        $sql .= " $where $field = '$value'";
        $where = 'AND';
    }
if(substr_count($value,"from")){
echo "yes";
}
}

$sql .= ";";

echo $sql;
}
?>
<script>
function checkTextField_callfrom(val){
document.getElementById("calldate_to").required = true;
}
function checkTextField_callto(val){
document.getElementById("calldate_from").required = true;
}
function checkTextField_leadfrom(val){
document.getElementById("leaddate_to").required = true;
}
function checkTextField_leadto(val){
document.getElementById("leaddate_from").required = true;
}
function checkTextField_submitfrom(val){
document.getElementById("submitdate_to").required = true;
}
function checkTextField_submitto(val){
document.getElementById("submitdate_from").required = true;
}
function checkTextField_appfrom(val){
document.getElementById("appdate_to").required = true;
}
function checkTextField_appto(val){
document.getElementById("appdate_from").required = true;
}
</script>
<body>
<form method="post" action="query_screen.php">
 Client<select name="client">
 <option value="" selected="selected">Select any one</option>
 <option value="1">A</option>
 <option value="2">B</option>
 <option value="3">C</option>
 </select>

 Status<select name="statusa">
 <option value="" selected="selected">select any One</option>
 <option value="1">Fresh Data</option>
 <option value="2">Assign to caller</option>
 <option value="3">Assign to DJ</option>
 </select>

 Mobile<input type="text" name="mobile" />
 Call Date From<input type="date" name="calldate_from" id="calldate_from" onblur="checkTextField_callfrom(this);" />
 Call Date To<input type="date" name="calldate_to" id="calldate_to" onblur="checkTextField_callto(this);" />

 Lead Date From<input type="date" name="leaddate_from" id="leaddate_from" onblur="checkTextField_leadfrom(this);" />
 Lead Date To<input type="date" name="leaddate_to" id="leaddate_to" onblur="checkTextField_leadto(this);" />

 App Date From<input type="date" name="appdate_from" id="appdate_from" onblur="checkTextField_appfrom(this);" />
 App Date To<input type="date" name="appdate_to" id="appdate_to" onblur="checkTextField_appto(this);" />

 Submit Date From<input type="date" name="submitdate_from" id="submitdate_from" onblur="checkTextField_submitfrom(this);" />
 Submit Date to<input type="date" name="submitdate_to" id="submitdate_to" onblur="checkTextField_submitto(this);" />

    </select>
 <input type="submit" name="submit" value="submit">
</form>
</body>
</html>

No comments:

Post a Comment

Dharamart.blogspot.in