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>
Related
I have a form with several input fields plus a select element that has several dropdown options. The user fills in the input fields and selects one of the drop down options and submits the form. The php handler for the form saves the form inputs in a database and builds a page from these inputs. An edit link on the built page returns to the form page with the fields preset to what they were the last time the form was submitted, so the editing can start from the last state submitted.
Presetting the input fields is easy, where I can just specify value=<?php echo [saved value from last submission];?> But presetting the select element to the last value chosen is not so easy. I know that jQuery has $('select#id').val("4"); which will preset the select#id element to option #4, but how can I trigger this when the form page is loaded again? Presettng the inputs by setting value= doesn't seem to generate anything that can be sensed by jQuery.
Thanks for any ideas.
Set the attribute selected for one of the options you want to be the selected. So, something like <option <?php echo 'selected="selected"';?>>4</option>.
Edit:
Either you can generate the options in a loop having the values in an array, checking each iteration if the current option was the one that was selected by the user and set the selected attribute of that one option, or you can just use the following, although not so elegant, approach:
<select name="number">
<option value="1" <?php echo ($_POST['number'] == '1') ? 'selected="selected"' : ''; ?>>1</option>
<option value="2" <?php echo ($_POST['number'] == '2') ? 'selected="selected"' : ''; ?>>2</option>
<option value="3" <?php echo ($_POST['number'] == '3') ? 'selected="selected"' : ''; ?>>3</option>
<option value="4" <?php echo ($_POST['number'] == '4') ? 'selected="selected"' : ''; ?>>4</option>
</select>
user2570380:
I think your edited solution would work so I gave you credit. But I've found another way that works that may be even more elegant. In the HTML of my PHP file I've placed at the bottom:
<script>
jQuery('select#age_group').val(<?php echo $age_group?>);
</script>
</body>
Here select#age_group is the select element I want to preset and $age_group is the value coming into PHP that I want to set into the select element.
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>.
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.