First, I would like to say that I'm not the greatest PHP developer. That being said, I would like some input on my code. I spent an hour trying to get this to work and I finally got it to work, however, I would like to know if this is the best way to do it. I'm asking the community because I'm trying to become a better developer.
I'm building a search form that uses POST method to send values to a search page. This page then takes the values, checks the conditional statements, and updates the query based on the results.
Here is my code.
Search form code
echo '<form method="post" class="cat-search" action="'.esc_url( home_url( '/search' ) ).'" role="search" >
<div class="gender-fields">
<span class="label-wrap">
<input type="radio" name="gender" value="male" id="male">
<label for="male">Male</label>
</span>
<span class="label-wrap">
<input type="radio" name="gender" value="female" id="female">
<label for="female">Female</label>
</span>
<span class="label-wrap">
<input type="radio" name="gender" value="" id="both">
<label for="both">Both</label>
</span>
</div>
<select class="cat-letters" name="letter">
<option value="">Search by letter</option>';
foreach ( $categories as $category ) {
echo'<option value="'.$category->slug.'">'.$category->name.'</option>';
}
echo '</select>
<input class="search-submit" value="Search" type="submit" />
</form>';
Search page conditional code
$let = esc_html($_POST["letter"]);
$gen = esc_html($_POST["gender"]);
if ( !empty($_POST["gender"]) && !empty($_POST["letter"])) {
$cat_array = array($let,$gen);
$cat_string = implode("+", $cat_array);
} else if ( empty($_POST["gender"] ) ) {
$cat_string = $let;
} else {
$cat_string = $gen;
}
First, I tried to use the isset()
function instead of empty()
, but I just couldn't get it to work.
Is this the right way to do it, or is there a better way to achieve the same results?
*I never had a problem with the form, the problem was with the conditional statements.
I would just recommend using isset here
$let = esc_html(isset($_POST["letter"]) ? $_POST['letter'] : '');
$gen = esc_html(isset($_POST["gender"]) ? $_POST['gender'] : '');
So you don't have any PHP errors when there are not letter and gender parameters received ;)
Read more about ternay operators here
It is good practice to avoid using GLOBAL variables directly, get a temp variable instead.
$post = $_POST;
$let = esc_html(isset($post['letter']) ? $post['letter'] : '');
With PHP 7, you can use operator ??
no need to check for undefined index:
$let = esc_html($post['letter'] ?? '');
Also use (single)''
over (double)""
quotes wherever possible, and check for whitespaces by trimming
Before PHP 7:
$post = $_POST;
$cat_array = array();
foreach(array('letter','gender') as $field){
if(isset($post[$field]) && trim(esc_html($post[$field]))){
array_push($cat_array,esc_html($post[$field]));
}
}
$cat_str = implode('+',$cat_array);
- Why did you add the trim() in the if statement?
What if user inputs only whitespaces? Your code will allow it and which defeats validation process.
- How is it that if only one field is selected the implode function doesn't add a + at the end? Is it because it's an array?
Yes, implode()
will join/concat two or more elements of array.
First of all, it is not a good practice to write the HTML contents using PHP. Try to separate the HTML from PHP in your HTML form as follows.
<form method="post" class="cat-search" action="<?php echo esc_url(home_url( '/search' ));?>" role="search" >
<div class="gender-fields">
<span class="label-wrap">
<input type="radio" name="gender" value="male" id="male">
<label for="male">Male</label>
</span>
<span class="label-wrap">
<input type="radio" name="gender" value="female" id="female">
<label for="female">Female</label>
</span>
<span class="label-wrap">
<input type="radio" name="gender" value="" id="both">
<label for="both">Both</label>
</span>
</div>
<select class="cat-letters" name="letter">
<option value="">Search by letter</option>
<?php foreach ( $categories as $category ) { ?>
<option value="<?php echo $category->slug;?>"><?php echo $category->name;?></option>
<?php } ?>
</select>
<input class="search-submit" value="Search" type="submit" />
</form>
And then the Search page conditional code
$let = trim(esc_html($_POST["letter"]));
$gen = trim(esc_html($_POST["gender"]));
if ($let != "" && $gen != "") {
$cat_array = array($let,$gen);
$cat_string = implode("+", $cat_array);
} else if ($let != "") {
$cat_string = $let;
} else {
$cat_string = $gen;
}