I would like to know how to submit two drop down menus w/o a submit button. I want to populate the second drop down menu on selection of the first and then be able to echo out the selection of the second drop down menu. Data for the second drop down menu is obtained from a mySQL database. I am using two forms on my page, one for each drop down menu.
<form method="post" id="typeForm" name="typeForm" action="">
<select name="filterType" onchange="document.getElementById('typeForm').submit()">
<option <?php if ($_POST['filterType'] == 'none') print 'selected '; ?> value="none">Filter by...</option>
<option <?php if ($_POST['filterType'] == 'employee') print 'selected '; ?> value="employee">Employee</option>
<option <?php if ($_POST['filterType'] == 'taskName') print 'selected '; ?> value="taskName">Task</option>
</select>
<noscript><input type="submit" value="Submit"/></noscript>
</form>
<form method="post" id="categoryForm" name="typeForm" action="">
<select name="filterCategory" onchange="document.getElementById('categoryForm').submit()">
<option <?php if ($_POST['filterCategory'] == 'none') print 'selected '; ?> value="none"></option>
<?
$count2 = 0;
echo $rowsAffected2;
while ($count2<$rowsAffected2) {
echo "<option value='$filterName[$count2]'>$filterName[$count2]</option>";
$count2 = $count2 + 1;
}
?>
</select>
<noscript><input type="submit" value="Submit"/></noscript>
</form>
I can submit the first form with no problem. It retrieves values into the second drop down menu successfully.But on selecting a value from the second menu the page refreshes and I'm left with an two unselected drop down menus. I tried echoing the $_POST['filterCategory'] and didn't get a result. I tried using onchange="this.form.submit();" in both forms and I still get the same result. Is there a way to do this without using AJAX, JQuery or any complex Javascript script? I require to this completely in PHP.
I want to avoid the second refresh but still be able to gather the $_POST[''] data from the second selection.
use the same form for both selects
You have two ways of doing that.
You can either submit the form using AJAX; that will prevent the whole page from refreshing, and hence, losing the data in both select elements; that is also the coolest way to have it done.
OR
Using a single form, when both the first and second select elements are submitted, use their values to re-create the select elements.
The cleaner way to do it is still the first option; at least, that's what I'll use if I have to do it.
Related
I am creating my form and adding error handling.
When the page is refreshed i want to be able to select the previous selected value in the drop down menu but i am struggling to get this to work.
Is anyone able to help
<select value="<? echo $_POST["Bookie"][$i]?>" style="width:100px;" id="Bookie[]" name="Bookie[]">
<option>Bet365</option>
<option>Betbright</option>
<option>Betfair</option>
<option>Betfred</option>
<option>BetVictor</option>
<option>Boylesports</option>
<option>Bwin</option>
<option>Centrebet</option>
<option>Coral</option>
<option>Ladbrokes</option>
<option>Paddy Power</option>
<option>Pinnacle Sports</option>
<option>SBOBET</option>
<option>Sky Bet</option>
<option>Stan James</option>
<option>unibet</option>
<option>William Hill</option>
</select>
You have to repeat this for each <option> tag, changing Paddy Power as appropriate:
<option<?php if(!empty($_POST) && $_POST['Bookie'][$i] == 'Paddy Power') echo ' selected="selected"';?>>Paddy Power</option>
If your <form> tag has method="post", you'll be fine with the above snippet. Otherwise you'll need to change $_POST in the above to $_GET.
Also, remove value="<? echo $_POST["Bookie"][$i]?>" from your <select> tag as that attribute is not supported there. The selected attribute is for setting the selected option in a <select> list.
just use $_SESSION['convert']:
The first time you open the page you will check if session exists:
session_start();
$convert = isset($_SESSION['convert'])?$_SESSION['convert']:"Bet365";
So, when you will write your dropdown like:
<form>
<select>
<option value="Bet365" <?php echo $convert=='Betfair'?'selected':''?>>Bet365</option>
<option value="Coral" <?php echo $convert=='Coral'?'selected':''?>>Coral</option>
</select>
</form>
then on form submit you will take the actual convert selected and save it in session:
session_start();
$convert = $_POST['convert'];
$_SESSION['convert] = $convert;
last selected option after refresh the page
I have four radio buttons and I would like to get the drop-down menu based on the selected radio button. Is it best if I just write all those drop-downs ready and display them based on the selection or is there a way to do it with ajax (because the drop-down values are coming from database) ?
Here are the radio buttons:
<input type="radio" name="some" value="someValue1">
<input type="radio" name="some" value="someValue2">
<input type="radio" name="some" value="someValue3">
<input type="radio" name="some" value="someValue4">
Here is the drop-down:
<select name="reason">
<?php
$data = WorkReason::all();
foreach ($data as $d) {
?>
<option value="<?php echo $d->code; ?>"><?php echo $d->reason; ?></option>
<?php
}
?>
</select>
I am using php ActiveRecords to get the values from database.
So if anybody knows a good way of doing this I would appreciate some help.
Make the different drop down lists and add a php condition to check which radio button was clicked and display that list.
You can use an array to select the correct code for you.
$radio2select = array ("someValue1" => "<select>..." , "someValue2" => "...", "someValue3" => "..."); // you get the idea
echo $radio2select($_REQUEST['radio']) ;
The use of an array is a trick to avoid using a switch statement, and it works well when your drop down boxes have been precomputed.
Otherwise, make a function which build the drop down box:
function select_reasons($which){
<select name="reason">
<?php
$data = WorkReason::all(array('conditions' => array ( 'reason = ?' => $which)));
foreach ($data as $d) {
?>
<option value="<?php echo $d->code; ?>"><?php echo $d->reason; ?></option>
<?php
}
?>
</select>
}
then just pass the $_REQUEST[‘radio'] to generate the corresponding dropdown list.
Note that I don't know your database schema, but I think that it should get you started.
Also, this code will work either on a regular Get or Post, or could be embedded within an ajax roundtrip (this depends on the framework you are using).
Another advantage is that only the necessary html code get included in the page, compared to a CSS based solution.
Make the different drop down lists and add a php condition to check which radio button was clicked and display that list.
if (isset($_POST['checkbox1']) {
echo '<select>...'
} else if (isset($_POST['checkbox2']) {
echo '<select>...'
}
Just load all Dropdown menues and display the one the user chose earlier with CSS:
display:block
display:none
I am using a chained select-menu to guide the visitor trough some questions. This works like a charm, however, in the end the user has to click a button to send all values to a PHP-script. This also works without a problem, however, when the page reloads (because the button sends the form to the same page), all fields are hidden again and this is not what i want.
I would like the chained selection menu's to be shown when items in them are "selected" when the page reloads. Using PHP i simply test if a field was selected and add that status to the select-menu. But they remain hidden until you manually select everything again.
I think it's just a minor adjustment in the JS-part. But my limited knowledge of JS leaves me without a solution.
Sources
jsFiddle
Chainedselection script
jQuery UI and selectmenu
Since the form submits to itself the values the user chose will be available in either $_GET or $_POST (depending on whether the action of your form is get or post).
The best approach would then be to use those values to set the selected attribute on the appropriate <option> node using something like this.
$selectedC1Value = '';
<? if isset($_POST['c1']) { ?>
$selectedC1Value = $_POST['c1'];
<? } ?>
<select name="c1" id="c1" >
<option value="" <? if ($selectedC1Value == '') { echo 'selected'; } ?>>
--
</option>
<option value="age" <? if ($selectedC1Value == '') { echo 'selected'; } ?>>
Age
</option>
<option value="education" <? if ($selectedC1Value == '') { echo 'selected'; } ?>>
Education
</option>
<!-- etc -->
</select>
This would have to be repeated for each <select> in your <form>.
so when i send the form with with the first option 'public' selected. the data is inserted. but when i try submitting the form with the other option selected, the ones in the for each loop. the data no longer is sent. i have inspected the elements. and they are outputting all of the correct values. and they are displaying properly. why arent they inserting into the db? when i click submit nothing happens. but when i click submit for the first option, it works fine?
<form method='POST' action='add.php'>
<select>
<option name="user_page_id" value="<?php echo $_SESSION['user_id']; ?>">Public</option>
<?php
$dis=show_groups_select_list($_SESSION['user_id']);
foreach($dis as $key=>$list){
echo '<option name="user_page_id" value="'.$list['id'].'">'.$list['username'].'</option>';
}
?>
</select>
</form>
You need to put the name attribute on the select tag, not the option tag.
<select name="user_page_id">
<?php foreach ($dis as $key => $list): ?>
<option value="...">...</option>
<?php endforeach; ?>
</select>
I know you got your answer, just some more information for those who face the same problem but that solution did not work:
I had the same problem and it turned to be made by Bootstrap!
In case you are using Bootstrap , you need to provide class attribute of select:
<select name="any_name" class="form-control">
<option value="this is what would be sent if selected"> sth </option>
</select>
for more information take a glance at this discussion too!
I have a sign-up form in php, which sends data to another php form for insertion into mysql
If any of the fields are missing, an error mesage is sent back to sign-up form using session variable (value="<?php echo $_SESSION['error']; ?>") along with all the fields the user had already filled up ( so that they dont have to fill it up again ) using session variables as well..
for text boxes im using value="<?php echo $_SESSION['fname']; ?>" which works fine.. but this doesnt seem to work for drop down boxes or radio buttons..
any suggestions?
Drop down boxes and radio buttons use a selected or click index. You need to check for each one if it was selected or clicked and give it the appropriate attribute.
<?php if($_SESSION['dropBox1'] == "value") echo ' selected="selected"'; ?>
First, you shouldn't submit the form to another page for validation. Do it in the current page. And drop down lists and radio buttons value attribute is the value that is tied to the selection. Not what is displayed or in any way tells it that it is the selected item. #James beat me for code on how to show which is selected.
<select>
<option value="0" <?php echo $_SESSION['dropBox1'] == "0" ? ' selected="selected"' : '' ?>>option1</option>
<option value="1" <?php echo $_SESSION['dropBox1'] == "1" ? ' selected="selected"' : '' ?>>option2</option>
<option>etc..</option>
</select>