I built a custom form for search filtering on my site. I'm using hidden input fields to decide what type of search filter was selected. The value gets passed and I check on my search page what the value was, and then direct to the correct search results page like so:
<?php
$search_refer = $_GET['search_type'];
if($search_refer == 'members') {
load_template(TEMPLATEPATH . '/search-members.php');
} else if($search_refer == 'event') {
load_template(TEMPLATEPATH . '/search-member-event.php');
} else {
load_template(TEMPLATEPATH . '/search-site.php');
}
?>
Here are the two fields in HTML/PHP
<div class="state"> // Sorting by user State
<input type="hidden" name="search_type" value="members">
<select id="stateDrop" name="state">
<option value="">State</option>
<?php
foreach($states as $state) {
echo '<option value="'.$state.'">'.$state.'</option>';
}
?>
</select>
</div>
<div class="sort-event"> // Sorting by an Event
<input type="hidden" name="search_type" value="event">
<select id="eventDrop" name="event">
<option value="">Event</option>
<?php
$args = array(
'post_type' => 'events',
'order' => 'ASC'
);
$eQuery = new WP_Query( $args );
while( $eQuery->have_posts() ) : $eQuery->the_post();
echo '<option value="'.$eQuery->post->post_title.'">'.$eQuery->post->post_title.'</option>';
endwhile;
wp_reset_query();
?>
</select>
</div>
Both have the name search_type as a hidden name, but the values are different. members vs event. My problem is when you click on the submit button, the value being received is always the last hidden input value. e.g. event.
Is there a way to decide which field was chosen so that I can then direct the search results to the correct page. (the search pages pull in different information based on the search).
Thanks,
As you have two inputs with the same name, only one of them will ever be set to the server.
If you want them to use the same name you're going to need to use POST and two forms, which would require two separate submit buttons but go to the same page. This way you can submit the desired form based on the filter.
<form action="yourpage.php" method="post">
<input type="hidden" name="search_value" value="members"/>
... member stuff
<input type="submit" value="Filter Members"/>
</form>
<form action="yourpage.php" method="post">
<input type="hidden" name="search_value" value="event"/>
... event stuff
<input type="submit" value="Filter Events"/>
</form>
Or you could use an input type of a radio button, as that supports having the same name as only one value can be selected at once.
Event: <input type="radio" name="search_type" value="event"/>
Members: <input type="radio" name="search_type" value="members"/>
// Then in PHP $_POST["search_type"] will hold the selected value.
If you wish to stick with GET, you could use JavaScript to set the value of a single hidden element before the user clicks the link.
Related
As you can see in the picture I have multiple button inside one FORM so when I click on button It insert always the last value :: S (126).
This is my code:
<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="form-control" placeholder="Numéro de chassit" name="chassit" id="chassit" >
<?php
if (mysqli_num_rows($result) > 0)
{ ?>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<?php echo "<img alt='' src='../admin/image/mercedes/".$row['image']."' >";?>
<div class="card-body">
<input id="model" name="model" type="hidden" value="<?=$row['model'] ?>">
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</div>
</form>
And this is the process
<?php
if(ISSET($_POST['mo'])){
$demandeur = $_SESSION['username'];
$model = $_POST['model'];
$chassit = $_POST['chassit'];
$sql = "INSERT INTO tbl_xp_support (demandeur,model,chassit)
VALUES ('$demandeur','$model','$chassit')";
if (mysqli_query($con, $sql)) {
header("Location: ../xp_group.php");
} else {
echo "Error: " . $sql . " " . mysqli_error($con);
}
mysqli_close($con);
}
?>
The input name="model" it insert always the last value !!!
Because your all input fields has same name, so last one will overwrite any previous ones. If you want to select only one that was selected, you should make it as radio button:
<label class="card-body">
<input id="model" name="model" type="radio" style="display: none;" value="<?=$row['model'] ?>"/>
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</label>
Please note:
you never use $_POST['mo'] value that should contain selected model
You have same ID in every iteration, and that is invalid HTML
Your browser has no way to know that each submit button is supposed to be associated with a single hidden input, since they're all part of the same form. What will actually happen is that regardless of which button is pressed, all of the hidden fields will be submitted.
Since they all have the same name, but different values, PHP has to decide which value to actually put into the $_POST array. Its policy is to take the last value, which is what you see.
There are a few ways I can think of to make this work:
Put each hidden field and submit button into its own HTML <form>, rather than having one for the whole page.
Have a single hidden field at the bottom of the page, and write some JavaScript that updates that hidden field when you click a button, before submitting the form.
Look at the value of the submit button by examining the $_POST['mo'] variable, and get rid of the hidden fields completely. But beware that there are some extra cases you need to consider when doing this.
I have a form with an input field with a value that is pulled from a a WP get_terms() array, like so:
<?php $location = sanitize_text_field($_GET['location']);
if($location) {
$matching_locations = get_terms(array(
'taxonomy' => 'job_location',
'fields' => 'slugs',
'name__like' => $location
));
};
if($matching_locations) : ?>
<section class="job-search__side-bar-location">
<h2 class="job-search__side-bar-title">Location</h2>
<div class="job-search__sider-bar-location-row">
<label class="job-search__side-bar-location"><?php echo ucwords($matching_locations[0]); ?></label>
<input type="hidden" name="location" class="job-search__side-bar-location" id="location_side-bar" value="<?php echo $matching_locations[0]; ?>" readonly />
<input id="remove-location" class="job-search__side-bar-location-delete" value="✕" />
</div>
</section>
<?php endif; ?>
Underneath the location input, I have a second input with an id of remove-location, which has a jQuery click method assigned to it that is supposed to assign an empty string as the value of the location input and then resubmit the form:
$('#remove-location').click(function() {
$('#location_side-bar').val("");
$('#sidebar-search').submit();
})
However, when I click on the remove-location input, the form is submitted again with the location same value from the tax query. Is there any way around this? I'd like for the form to retain the location value in all cases, except for when the remove-location input is clicked.
I've added an image to help illustrate what I'm aiming for:
I have an html form (and accompanying php code) that allows user to input task names, which are stored in a PHP array $task_list[].
I also have, below the task list, a dropdown from which users can select a desired value. I want to create a button modify which will allow users to change the value they have selected.
For instance, if the task list is:
Get up
Take a bath
Go for a jog
Go to school
Come back home
A user should be able to select "Go to school", and having done so, click Modify and change that to "Go to work", or something else.
I've made the select dropdown list work and can access the selected value and store it in a variable $selected. But I am completely flummoxed as to how I can allow users to change the value in the $task_list array.
My code–
task_list.php:
<form action="." method="post" >
<!//select dropdown>
<select name='taskid'>
<?php foreach( $task_list as $id => $task ) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<br />
<label> </label>
<input type="hidden" name="action" value='modify'>
<input type=submit value="Modify Task">
<br />
<label> </label>
<input type="hidden" name="action" value='promote'>
<input type=submit value="Promote Task">
</form>
index.php:
//get selected value
function getSelected()
{
if(isset($_POST['taskid']))
{
$myvalue = $_POST['taskid'];
}
else
{
$myvalue = $task_list[0];
}
return $myvalue;
}
//skipping ahead to modify field (all other buttons work perfectly):
case 'modify':
$myval = getSelected(); //$myval returns index of selected item
$selected = $task_list[$myval]; //selected item
break;
I just want to allow the user to click modify and change the value they selected in the $task_list array.
Thank you.
I am on page: index.php?page=2 and there is a search form for "crteria" and "term", but every time I submit, it takes me to index.php?criteria=x&term=x instead of index.php?page=2&criteria=x&term=x. So it ignores the page=2. Also I want to submit the criteria and term on the same page I am. Here's the code:
<div id="search">
<form name="search" action="index.php?page=2" method="get">
<p>Search by:</p>
<select name="criteria">
<option>Name</option>
<option>Last name</option>
<option>Project name</option>
</select>
<br><input type="text" name="tern" placeholder="term">
<br><button type="submit">Find`enter code here`</button>
<hr>
</form>
</div>
On the index.php I include the page that the form is on like this:
if(isset($_GET['page']) && $_GET['page'] == 2){
include 'modules/search.php';
}
So when I'm on index.php?page=2 the search.php is providing me with the form that you can see above.
I know, it's a mess, it's my first project. Thank you in advance.
You can simply add an input hidden parameter into your form and remove it from action attribute:
<form name="search" action="index.php" method="get">
<input type="hidden" name="page" value="2">
In case of more than one parameter or if the parameters could vary, you could add this code into the php that print the form:
foreach($_GET as $k => $v){
$k = addslashes($k); //or a DBMS specific escape function
$v = addslashes($v); //or a DBMS specific escape function
echo "<input type='hidden' name='$k' value='$v' />";
}
I have spent quite some time making a function and the last 15-20 minutes trying to figure this out. I need help!
I am selecting multiple rows from the database and then running them in a while loop.
They are available on a dropdown menu.
<form method="POST" action="adminprocess.php">
<fieldset>
<p>
<label class="left2">League:</label>
<select name="league" class="combo">
<?php
$q = $database->selectAllLeagues();
while($row=mysql_fetch_assoc($q))
{
$theid = $row['id'];
extract($row);
?>
<option value="<? echo $theid; ?>">
<? echo $format.'_'.$game.'_'.$name.'_Season '.$season;?>
</option>
<?
}
?>
</select>
</p>
<p>
<input type="hidden" name="replaceleague" />
<input type="hidden" name="format" value="<? echo $format; ?>" />
<input type="hidden" name="game" value="<? echo $game; ?>" />
<input type="hidden" name="name" value="<? echo $name; ?>" />
<input type="hidden" name="season" value="<? echo $season; ?>" />
<input type='submit' class="button" value='Select league' />
</p>
</fieldset>
</form>
$theid seems to be working fine dependning on which row i select on the dropdown menu.
However, I cant get the values below in the hidden inputs to pass through the correct values from the row selected in the dropdown box.
It seems to always pass through the 4 variables from the first row of the database.
So basically, I need it to select the right row and use that data.
What am i doing wrong!!!
Thanks for reading!
Your hidden fields are initialized outside the loop, so they'll use the values that were left over from the last iteration of the while loop. (i.e. the last fetched row)
Why do you actually need the hidden fields in the first place? When you submit the form, the league field will contain the ID of the row selected in the drop-down box. Using the ID, you can fetch the other fields from the database when processing the form.
To directly answer your question about the while loop, it's because the hidden inputs are echoed outside the loop, after which data the last-iterated row from your database is used by PHP to output to those hidden inputs.
But I suggest that instead of using hidden form elements like that, you submit your form with the <option> with the value a user chooses, read the value (as in $_POST['league']), and fetch the row from your database with that ID and use it accordingly. (You may wish to keep the replaceleague hidden input if your application needs it of course.)
It's much easier, plus it ensures the information about the row a user chooses is coming from your database and not tampered with. In fact, for most applications this is the right way to go.