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
Related
I'd like to post two values in one drop down option and I'm not sure about the best way to approach it.
The drop down list pulls data in from an external service. This data is 'id' and 'name'.
When I select an option in the drop down and then press submit I want the 'id' and the 'name' to be posted.
My code looks like this:
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
I've tried putting in a hidden input field, but that then doesn't render out the drop down (instead it just creates a list).
I am using both the id and name elsewhere, so I don't want to have to post just the id and then have to get the name again, hence I want to post both of them at the same time.
Any recommendations?
You cannot post two values unless both of them appear in the value attribute. You can place both in, separated by a comma or hyphen and explode() them apart in PHP:
// Place both values into the value attribute, separated by "-"
<option value="<?php echo $name['id'] . "-" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
Receiving them in PHP
// split the contents of $_POST['data'] on a hyphen, returning at most two items
list($data_id, $data_name) = explode("-", $_POST['data'], 2);
echo "id: $data_id, name: $data_name";
You may add a hidden field with the name "id" and then bind an onchange event listener to the <select>. inside the onchange function, get the value of the <select> and assign it to the "id" field.
<form>
<select name="name" onchange="document.getElementById('id').value=this.value">
<!--
...
options
...
-->
</select>
<input type="hidden" name="id" id="id" />
<input type="submit" />
</form>
You could output something like:
<option value="<?php echo $name['id']."_".$name['name'];?>">
Then use preg_splitor explode to separate the two once the form data is processed.
The only way to do this is to include both pieces of information in the value for the single option. Just use code like the following, and then when you get the value back, split the string at the first underscore to separate out the values.
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id'] . "_" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
Also, you could just leave the ID alone in your form, and then look up the id again when the user submits the form.
You could make as many hidden inputs as there are options, each with the name of one option value. The input's values are the contents of the options. In the second script, you can look for the dropdown value, and then take the value of the hidden input with that name.
We can pass it with data attribute, and can access in js,
<option value="<?php echo $name['id']);?>" data-name="<?php echo $name['name']);?>">One</OPTION>
var name = $(this).find(':selected').data('name');
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 php form which submits data to another php for validation. if any fields are left blank the validator php sends a message back to original php form along with all the pre filled variables using session variables so that user doesn't have to fill them again.
i can use these session variables to fill in text boxes again like this
value="<?php echo $_SESSION['fname'] ?>"
but how to go about populating drop downs, radio buttons and check boxes?
thanks
For a select, checkbox or radio box you would need to check whether the values match. Something like:
<select name="fname">
<option value="1" <?php if($_SESSION['fname'] == 1) echo 'selected'; ?>>1</option>
<option value="2" <?php if($_SESSION['fname'] == 2) echo 'selected'; ?>>2</option>
</select>
It's easier if you are iterating through the values for the option field:
<select name="fname">
<?php foreach($array AS $key => $value) { ?>
<option value="<?php echo $value; ?>" <?php if($_SESSION['fname'] == $value) echo 'selected'; ?>><?php echo $value; ?></option>
<?php } ?>
</select>
A <select> element's selected <option> is not determined by it's value= attribute. In order to mark an option selected, the <option> element must have the selected attribute set on it, as follows:
<select>
<option>1</option>
<option selected>2</option>
<option>3</option>
</select>
In order to do this programmatically, you need to iterate through the options, and manually test for the correct option, and add selected to that.
... Noticed that your double quote at the end of the statement was misplaced, it should be:
<script>
$("#fname").val("<?php echo $_SESSION['fname'] ?>");
</script>
There is a far simpler method using JQuery. You can set Selects with .val() so make the most of it.
<script type="text/javascript">
$("#fname").val("<?php echo $_SESSION['fname']; ?>");
</script>
Don't forget to set the id of the select as well as the name to fname.
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.