My switch fails. I get wrong answers

Hello,
Here is my form:

<form method="post" action="action_page.php">
			    <div id="periods" class="selector">
				    <p>Please select desired period:</p>
					  <input type="radio" name="period" value="all" checked="checked">All (default)<br><br>
					  <input type="radio" name="period" value="filter" id="period">From: <input type="text" class="datepicker" name="start_date"> <br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp To: <input type="text" class="datepicker" name="end_date"> <br><br>
					  <button type="button" id="periods-button">Clear selection</button>
			    </div><!-- End periods -->
			    <div id="days" class="selector">
					  <p>Select days / A Day (One day at least)</p>

				
 					  <input type="checkbox" name="weekday[]" value="6" class="days"> Sunday
				  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
					  <input type="checkbox" name="weekday[]" value="0" class="days"> Monday<br><br>
					  <input type="checkbox" name="weekday[]" value="1" class="days"> Tuesday
				  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
					  <input type="checkbox" name="weekday[]" value="2" class="days"> Wednesday<br><br>
					  <input type="checkbox" name="weekday[]" value="3" class="days"> Thursday
				  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
					  <input type="checkbox" name="weekday[]" value="4" class="days"> Friday<br><br>
				 
 				  <button type="button" id="days-button">Clear selection</button>
			    </div><!-- End days -->
			    <div id="intraday" class="selector">
					  <p>Select intraday  time range</p>
				 <input type="radio" name="intraday" value="am"> AM <small>(00:00 - 11:59)</small><br><br>
				 <input type="radio" name="intraday" value="pm"> PM <small>(12:00 - 23:59)</small><br><br>
				  <input type="radio" name="intraday" id = "hour_range">
				   From:<input type="time" min="00:00" max="23:59" name = "start" placeholder="00:00" value="00:00">
				   To : <input type="time" min="00:00" max="23:59" name = "end" laceholder="23:59"value="23:59"><br><br>
				  
	              <button id="intraday-button" type="button" value="Clear selection ">Clear selection</button>  
			    </div><!-- End intraday -->
				<div class="apply"><button type="reset" value="reset form">Reset Form</button></div>
		<div class="apply"><button type = "submit" value="submit" -->>Filter Results</button></div>
		<div id="" style="clear:both;"></div>
		  </form>

Here is the action_page:

<?php
	 //code for period
	switch ($_POST['period']) {
	     // filter is selected. From date selected. To date not selected
		case isset($_POST['period']) == 'filter' && isset($_POST['start_date']) == true &&  isset($_POST['end_date']) == false:
			echo '1';
		    break;
		// filter is selected. From date selected. To date selected
		case isset($_POST['period']) == 'filter' && isset($_POST['start_date']) == true &&  isset($_POST['end_date']) == true:
			echo '2';
		    break;
		// filter is selected. From date not selected. To date selected
		case isset($_POST['period']) == 'filter' && isset($_POST['start_date']) == false &&  isset($_POST['end_date']) == true:
			echo '3';
		    break;
		// filter is selected. from date not selected. to date not selected
		case isset($_POST['period']) == 'filter' && isset($_POST['start_date']) == false &&  isset($_POST['end_date']) == false:
			echo '0';
		    break;
		// Att(default) is selected.
		default:
			echo 'all';
	}
	
//code for dayy
echo"<br>";
if(isset($_POST['weekday'])){
	
	    $days = ($_POST['weekday']);
		print_r($days);
		echo "<br><br>";
}

//  code for intraday
if(isset($_POST['intraday'])){
$hours = ($_POST['intraday']);

switch ($hours) {
    case "am":
        $hour_start = '00:00';
		$hour_end = '11:59';
		echo $hour_start;
		echo $hour_end;
        break;
    case "pm":
        $hour_start = '12:00';
		$hour_end = '23:59';
		echo $hour_start;
		echo $hour_end;
        break;
    case "on":
        $hour_start = ($_POST['start']);
		$hour_end = ($_POST['end']);
	    echo $hour_start;
		echo $hour_end;
        break;
    default:
        $hour_start = '00:00';
		$hour_end = '23:59';
		echo $hour_start;
		echo $hour_end;
} 
}		
	
 

?>

When I check it I get wrong input from the first div in the form , the period div

when I check the second radio button without dates or with dates in either input field or both I eitherget 2 as output or all - which is the default of the switch

What do I do wrong?

P.S. the rest of the form works well

That’s not how switch/case works. A simple example from the manual:

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

It looks as though you might need to use multiple if statements.

Edit: Also isset($_POST['period']) will evaluate to true or false. It will never be equal to 'filter'.

Thanks for the comments, Here are the changes:

 //code for period
	
	     // filter is selected. From date selected. To date not selected
		if($_POST['period'] == 'filter' && isset($_POST['start_date']) == true &&  isset($_POST['end_date']) == false){
			echo '1';
		}else 
			
		    
		// filter is selected. From date selected. To date selected
		if($_POST['period'] == 'filter' && isset($_POST['start_date']) == true &&  isset($_POST['end_date']) == true){
			echo'2';
		}else
			
		    
		// filter is selected. From date not selected. To date selected
		if($_POST['period'] == 'filter' && isset($_POST['start_date']) == false &&  isset($_POST['end_date']) == true){
			echo '3';
		}else
			
		   
		// filter is selected. from date not selected. to date not selected
		if($_POST['period'] == 'filter' && isset($_POST['start_date']) == false &&  isset($_POST['end_date']) == false){
			echo '0';
		}else {
			echo 'all';
		}

I still get 2 as result in any input when the second radio button is checked

ten have a look at your inputs: var_dump($_POST)

1 Like

…and do not proceed it the $_POST['variable'] is not correct.

var_dump($_POST);
exit; // ENSURE VARIABLE IS CORRECT BEFORE PROCEEDING

Irrespective of the values, you could simplify the logic somewhat as follows. It could be further simplified, but this should give some idea.

  if ($_POST['period'] == 'filter') {
    // filter is selected. From date selected. To date not selected
    if (isset($_POST['start_date']) &&  !isset($_POST['end_date'])) {
      echo '1';
    } else 
        
    // filter is selected. From date selected. To date selected
    if (isset($_POST['start_date']) &&  isset($_POST['end_date'])) {
      echo'2';
    } else
        
    // filter is selected. From date not selected. To date selected
    if (!isset($_POST['start_date']) &&  isset($_POST['end_date'])) {
      echo '3';
    } else
       
    // filter is selected. from date not selected. to date not selected
    if (!isset($_POST['start_date']) &&  !isset($_POST['end_date'])) {
      echo '0';
    } else {
      echo 'all';
    }
  }

The problem with all these isset()s is they will be true regardless of whether they were filled in.
Being set as an empty string is set all the same.
The only way they will not be set is if someone tampers with the form and removes or renames the inputs.
Shouldn’t you be checking for empty in the trimmed input instead?

1 Like

This is a revised version of the example @Gandalf gave, with isset swapped for empty and trim.
I also edited the comments, as the logic was reversed in those, which again may have beed cause for confusion, or caused by confision.

if($_SERVER['REQUEST_METHOD'] === 'POST'){
	
	  if ($_POST['period'] == 'filter') {
    // filter is selected. From date not selected. To date  selected
    if (empty(trim($_POST['start_date'])) &&  !empty(trim($_POST['end_date']))) {
      echo '1';
    } else 
        
    // filter is selected. From date not selected. To date not selected
    if (empty(trim($_POST['start_date'])) &&  empty(trim($_POST['end_date']))) {
      echo'2';
    } else
        
    // filter is selected. From date  selected. To date not selected
    if (!empty(trim($_POST['start_date'])) &&  empty(trim($_POST['end_date']))) {
      echo '3';
    } else
       
    // filter is selected. from date selected. to date selected
    if (!empty(trim($_POST['start_date'])) &&  !empty(trim($_POST['end_date']))) {
      echo '0';
    } else {
      echo 'all';
    }
  }
}

On a side note, your HTML could do with some work.

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp

This should never be seen, ever.
For a start, the correct syntax for a non-breaking space is &nbsp; with a semi-colon at the end.
But more importantly, don’t ad HTML to force layout like this, use CSS, margins and padding will do this without cluttering your HTML code with stuff that makes your eyes bleed.
The exact same applies to:-

<br><br>

In a form use the <label> element to label your inputs.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.