0

Hi I have a drop down list with the first option shown is an option i have set as DATE.. I need an if statement that runs if the date is empty or if the first options which says "DATE" is selected..Right know MY AJAX only works if the date selected..

This is my drop down

<?php
$dates = array();
$argez = (array( 'post_type' => 'latest_message'));
query_posts( $argez );
   if (have_posts())  : while (have_posts()) : the_post(); 
       $dates[] = get_the_date();
       $dates = array_unique($dates);
       print_r($datesun);

    endwhile; 
    echo '<option value="#">DATE</option>';
    foreach($dates as $date) {
     echo '<option value="' . $date . '">' . $date .'</option> ';
     }
    endif;
?>

This is my ajax

global $post, $wpdb;
if ($_POST["series"]!=0 ) {
    $series = array($_POST['series']);
    } else {
         $series = get_terms( 'series', array('fields' => 'ids') );
         //$series = array(implode(', ',$series));
    }
if ($_POST["speaker"]!=0 ) {
    $speaker = array($_POST["speaker"]);
    } else {
         $speaker = get_terms( 'speaker', array('fields' => 'ids') );
         //$speaker = array(implode(', ',$speaker));
    }
if ($_POST["topic"]!=0 ) {
$topic = array($_POST["topic"]);
} else {
     $topic = get_terms( 'topic', array('fields' => 'ids') );
     //$topic = array(implode(', ',$topic));

}
if ($_POST["date"]!= NULL || $_POST["date"] == 'DATE') {
$dates =  strtotime($_POST["date"]);
}
else {

}
    wp_reset_query();

    $myquery= array(

        'post_type' => 'latest_message',
        'tax_query' => array(
        array(
            'taxonomy' => 'series',
            'terms' => $series,
            'field' => 'id'
        ),
        array(
            'taxonomy' => 'speaker',
            'terms' => $speaker,
            'field' => 'id',
        ),
        array(
            'taxonomy' => 'topic',
            'terms' => $topic,
            'field' => 'id',
        )
    ),
    'date_query' => array(
                'year' => date('Y', $dates),
                'month' => date('m', $dates),
                'day' => date('d', $dates),
        ),


    );

print_r($myquery);
    query_posts($myquery);

Part of the problem is the date array brings in this information below.. How do i get it to still run because there isn't any information matching that date.. Any ideas

[date_query] => Array ( [year] => 1970 [month] => 01 [day] => 01

2 Answers2

0

Why not simply set the first value to disabled selected like so:

echo "<select>";
echo "<option value='#' disabled selected>DATE</option>";
foreach($dates as $date)
    echo "<option value='$date'>$date</option>";
echo "</select>";

Source: How do I make a placeholder for a 'select' box?

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220
0

Change your checking statement to this

if (isset($_POST["date"]) && $_POST["date"] != NULL && $_POST["date"] != '#') {
 // if the value is set and it's not equal null or # ; then treat it as passed in value
$dates =  strtotime($_POST["date"]);
}
else {
   // here where the user have selected "DATE"
}

I would also recommend to check How to determine if value is a date in PHP

Community
  • 1
  • 1
devBinnooh
  • 611
  • 3
  • 12