Posting corresponding datas on clicking submit button - php

for($i=0;$i<2;$i++)
{
?>
<tr>
<td>
<select name="sel_<php echo $i; ?>" id="sel_<php echo $i; ?>">
<option value="1">A</option>
<option value="2">B</option>
</select>
</td>
<td>
<input type="submit" name="sub_<?php echo $i; ?>" value="submit" />
</td>
</tr>
<?php
}
?>
Now I have 2 select boxes sel_1 and sel_2, each of them have corresponding submit button sub_1 and sub_2. I want to post the corresponding data when a submit button is pressed. For example: when I press sub_1 I need to post the value of sel_1.
So, if press on the submit button in a specific row, I need to post the value of the select box in that row.
How can I do this?

It doesn't matter that both select are sent. Just work with the one select you need.
if (isset($_POST['sub_0'])) {
$data = $_POST['sel_0'];
} elseif (isset($_POST['sub_1'])) {
$data = $_POST['sel_1'];
}
In $data will be value of the first, or the second one, select.
IF you need to send just one selectbox, you need more forms (each form will contain select and submit).

since you have not mentioned the form method i am using $_REQUEST here,
if (isset($_REQUEST['sub_0'])) {
// use the value of $_REQUEST['sel_0'];
} elseif (isset($_REQUEST['sub_1'])) {
// use the value of $_REQUEST['sel_1'];
}
Note: you can replace $_REQUEST with $_POST or $_GET as per you form method type or else you can use $_REQUEST itself if you wish to do so

Related

How to use data when submit button is clicked for next sql query

I am trying to create an html form where i have a dropdown menu populated with data from stations table. I am able to select a station and then i want to use this station name when a button is clicked to display a table with related data. But i don't understand how i can use selected station to create another query when submit button is clicked. Below is my code:
<form method="post">
<label>Choose a station:</label>
<select name="owner">
<?php
$sql = mysqli_query($conn, "SELECT name FROM stations");
while ($row = $sql->fetch_assoc()){
?>
<option value="owner1"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
i think onclick must be used but i don't understand what would be posted that i can use for new sql query.
When you click submit button browser send form via POST method to the same page and reload it. So you can capture the value selected in the form with this PHP code:
If (isset($_POST['owner'])) {
// mysqli query where the selected value is $_POST['owner']
}

Using GET with a form

So I have a form which has a drop down list, when the submit button is pressed the script task4.php is called. My problem is that I can use the whole option that is selected, however I only need the tid. How do I get just the tid and use it task4.php?
<form action="task4.php" method="get">
<select>
<?php
foreach($results as $row) {
echo "<option>".$row[tid].", ".$row[category].", ".$row[division].", ".$row[clubID].", ".$row[name]."</option>";
}
?>
</select>
<input type ="submit" value="Submit">
</form>
This is what I've got in task 4 and it isn't working:
if (isset($_GET['tid'])) {
$tid = $_GET['tid'];
}
It's possible (perhaps even necessary) that in the absence of a value the browser is sending the text contents of the selected option with the form data.
Just give the option a value:
echo "<option value=\"".$row[tid]."\">" ...
Additionally, that select should really have a name (I don't even see how it could have worked without one):
<select name="tid">

Pass an array of values as post data to document.forms[].method?

In my html I have this,
<tr>
<td class="grid_cell" width="5%">
<input type="checkbox" name="workspace_trees_rpt_target" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
</td>
</tr>
this are inside a loop so what will be displayed is a lot of checkboxes with different values. And in my script I have this,
if(confirm("Delete cannot be undone, click OK button to proceed.")) {
document.forms[0].method="POST";
document.forms[0].action="delete.php";
document.forms[0].submit();
}
after selecting two of the checkboxes and click the OK button, I then go to delete.php to print_r my post data. But when the result of print_r is displayed it only showed one value for the checked checkboxes, but I was expecting two of them. How can I make it such that when I check multiple checkboxes, the post data for the checkboxes will be an array of values of the checked checkboxes?
you must set the name of the inputs as array:
name="workspace_trees_rpt_target[]"
You have given the same name to all of your checkboxes. So only one value can be returned.
Change to
<input type="checkbox" name="workspace_trees_rpt_target[]" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
Then in your PHP code you will get and array of $_POST['workspace_trees_rpt_target'][]
So process it like this:
if ( isset( $_POST['workspace_trees_rpt_target'] ) ) {
// at least one checkbox has been ticked
foreach ( $_POST['workspace_trees_rpt_target'] as $checkboxe ) {
// do whatever $checkbox it will be set to the value="" that you set earlier
}
}

Decide which hidden input value gets passed

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.

How to show the values from a PHP array in a textfield, based on a dropdown menu

any help would be great. Here's what I have so far:
PHP:
<?php
$arr1 = array("1080", "939", "769", "696","572", "498", "408", "369");
$arr2 = array("1275", "1095", "890", "799", "676", "580", "472", "423");
?>
PullDown Menu:
<select name="age" id="age">
<option value="<?php echo $arr1[0]; ?>">18-24</option>
<option value="<?php echo $arr2[0]; ?>">25-29</option>
</select>
Textfield:
<input name="planA" type="text" id="planA" value="<?php echo $arr1[0]; ?>" size="10" />
I need the textfield to show the correct value from the array, based on what I choose from the pulldown menu.
Thx.
When a user selects a value from the pulldown, they'll just see what you have between option tags; so for the second value, 25-29 would be displayed. According to your code, this has a corresponding value of 1275. If that's the value you need to populate in a text box, you could do so using Javascript:
<select name="age" id="age" onChange=PopulateField(this.value);>
...
And in the head of the document:
<script type="text/javascript" language="javascript">
function PopulateField(numValue)
{
document.getElementById('planA').value = numValue;
}
</script>
Of course realize that if Javascript is disabled on the user's browser, this won't work. When the form is submitted, you'll still be able to get the value of course, by working with the age variable; its value already is set to the element from the array. (I assume the text box is just for user feedback.)
Now, if the form is already submitted, and you want to show the results of the submission in the text field, then all you need to do is change your PHP code to reflect the selected value as so:
<input name="planA" type="text" id="planA" value="<?php echo $_POST['age']; ?>" size="10" />
If your form uses GET instead of POST, change $_POST to $_GET accordingly.
Edit (per comments):
If you have multiple textboxes to populate, it kind of depends whether you have just one pulldown menu to trigger them, or if there are multiple pulldown menus. I'll assume you have another pulldown menu to populate another textbox. In that case, you can use a second javascript function like this:
<script type="text/javascript" language="javascript">
function PopulateFieldA(numValue)
{
document.getElementById('planA').value = numValue;
}
function PopulateFieldB(numValue)
{
document.getElementById('planB').value = numValue;
}
</script>
On the pulldown that is associated with the textfield for plan B, use the appropriate onChange event:
<select name="ageB" id="ageB" onChange=PopulateFieldB(this.value);>
You could pass multiple arguments to a function so you could populate multiple boxes:
function PopulateFields(numValueA, numValueB)
{
document.getElementById('planA').value = numValueA;
document.getElementById('planB').value = numValueB;
}
Anything else is just speculation on my part because I don't really know the specifics of your application. Hopefully this helps set you on the right track.

Categories