PHP selectfield get value from database, change and POST - php

I'm a beginner with PHP and have some decent issues ^^
Would like to pick the data from the field "disabled" from the database and update the selectfield with the value.
The selectfield has 2 possible options "yes" and "no" (value 0 or 1).
Now i would finally like to post the selected or unchanged value back to the database.
Here all the values including disabled which are loaded into $row by MYSQL SELECT query
$mydb->query('SELECT a.* ...
$row = $mydb->fetchRow();
Here the working query to write into the database / table (tested on other fields).
disabled is the field that should be updated/overwritten:
$sql = "REPLACE INTO `api_tokens` (`id`, `disabled`) VALUES (
".$id.",
".(empty($_POST['disabled']) ? 0 : 1).")";
Here the form which should have 2 selectable options, one of them shoudl be the value loaded from the database and the other value should be the opposite.
Unclear value/ares is marked with ????
<div class="row">
<label for="disabled">Deactivate</label>
<select name="disabled" id="disabled">
<option selected="<?=$row['disabled']?>"></option>
<option value="????">No</option>
<option value="????">Yes</option>
</select>

Try this. The key is to check the value for each condition. If the saved choice was 'no' (0) then set the select attribute. Do the same for 'yes' (1).
<div class="row">
<label for="disabled">Deactivate</label>
<select name="disabled" id="disabled">
<option value="0"<?php echo ($row['disabled'] == 0?' selected':''); ?>>No</option>
<option value="1"<?php echo ($row['disabled'] == 1?' selected':''); ?>>Yes</option>
</select>
UPDATE
I see from your comment that you'd like additional clarification. Here's what happens:
Each <option> element will specify a value attribute as one possible choice of the <select> element it belongs to. When a choice is made, the <option> element's selected attribute is set. (Technically, there is the multiple attribute on the <select> element, but let's not get into that now.)
When your user submits the form, their choices (option values) are made persistent by storing them in a database. In this example, this will be either 1 for yes or 0 for no.
If you want to present the user with the last state their form was in, you have to read their choices back from the database. You now have to set the select attribute on the <option> element they chose before. On their screen, their chosen option is now highlighted in the <select> list.
You can find the choice they submitted in $row['disabled'], where the 'disabled' key is equal to the name attribute of the <select> element. To set the select attribute on the right (previously chosen) <option> element, you check $row['disabled'] against each <option> element's value. Are they equal? Then this was their last chosen option, so now highlight it and set the selected attribute on this specific <option> element.

Related

Select Option from list Only once - Laravel

In my controller:
public function putbillpage()
{
$proname = productmodel::select('itemname')->get();
return view('putbillpage', compact('proname'));
}
In My View:(It lists the product name)
<td>
<select name="proname[]" class="form-control proname">
<option value="" selected="true" disabled="true">Select Product</option>
#foreach($proname as $v)
<option value="{{$v->itemname}}" >{{$v->itemname}}</option>
#endforeach
</select>
</td>
Whenever i click the plus on the right end a new row is added below.when is press cross row is removed.
Now my question is if i select a option in first row.then in the second row that option must not be displayed..
in the above image in first row i have selected cricket bat and now in the second row only soaps must be displayed.And when i remove first row the cricket bat also must be displayed in the options.
Any Help or suggestions are most Welcome.Thanks.
You should set a Flag
for example Soap is already selected in the next row choice you should remove the Soap option or alert the user that this option is already selected
Just set a variable on your js or set a cookie for selected options
Let me know if this helps you Thank you

Get the right value of select box and display it in php

I have a form with a select box where the user can select his gender. I succeed in getting the selected value and displaying this. But I want to use another term for the selected value.
For example, there are 2 values in the select box, named "man" and "vrouw".
<select name="geslacht" class="formulier-input">
<option value="" disabled selected hidden>Kies uw geslacht</option>
<option value="man">Man</option>
<option value="vrouw">Vrouw</option>
</select>
But I want to display Dhr. as the user selects for the value "man" and Mevr. as the user selects for the value "vrouw".
I guess I can do this in an if statement with:
if($geslacht === "the value"){ }
I don't know how I can finish this and I don't know what to put in the place of "the value".
How can I do this?

How do I get a drop down/select option have Multiple Values

I've read other questions on here that dealt with what I am looking for. But it still did not help me.
I want to have multiple values in an option of the select tag of a form that will be posting the information onto a different file that is viewable to the visitor by iframe. Example of what I am trying to do:
The following is on the file: Form.php
<div>
<form action"blank.php" method="post" target="box">
<select name="g1">
<option name="g1" value="Value 1, Value 2"> Text </option>
<option name="g1" value="Value 1, Value 2"> Text Blah </option>
</select>
<input type="submit" />
</form>
</div>
<iframe src="blank.php" name="box"></iframe>
The Following is a long the lines of what I want to appear on the: blank.php which is visible by iframe.
Option 1 or 2 value's should appear like this
[ Value 1 ] [ Value 2 ]
An Example:
Its a drop down menu that will post two seperate values when you select one option.
Like if I selected an option that says Hola - hello and it will post:
[Hola] means [Hello].
and the option tag would have the values as:
<option name="g1" value="Hola, Hello">Hola - Hello</option>
Sounds like you want to build an array of your form fields where multiple values are allowed. Read the PHP manual for more.
From the manual:
How do I get all the results from a select multiple HTML tag?
The
select multiple tag in an HTML construct allows users to select
multiple items from a list. These items are then passed to the action
handler for the form. The problem is that they are all passed with the
same widget name. I.e.
<select name="var" multiple="yes">
Each selected option will arrive at the action handler as:
var=option1
var=option2
var=option3
Each option will overwrite the contents of the previous $var variable.
The solution is to use PHP's "array from form element" feature. The
following should be used:
<select name="var[]" multiple="yes">
This tells PHP to treat $var as an array and each assignment of a
value to var[] adds an item to the array. The first item becomes
$var[0], the next $var[1], etc. The count() function can be used to
determine how many options were selected, and the sort() function can
be used to sort the option array if necessary. Note that if you are
using JavaScript the [] on the element name might cause you problems
when you try to refer to the element by name. Use it's numerical form
element ID instead, or enclose the variable name in single quotes and
use that as the index to the elements array, for example:
variable = document.forms[0].elements['var[]'];
First of all, you don't need a name attribute on each of your <option> elements. Just the <select> box.
Second of all, I think all you need is the handy dandy explode function. You'll get the value like this...
$values = $_POST['g1'];
...and then split them into an array like this...
$values = explode(', ', $values);
...and finally just access the values like so:
$value1 = $values[0];
$value2 = $values[1];
I know that this is in the PHP section, but what you are trying to do would be much easier to achieve with javascript. If you change the iframe tags to:
<div id="valueDisplay"></div>
and add an ID and listener to the select
<select name="g1" id="dropDown" onchange="showValues()">
<option name="g1" value="1"> Text </option>
<option name="g1" value="2"> Text Blah </option>
</select>
You just write a little script like this and put it in the body somewhere:
<script type="text/javascript">
function showValues(){
var outDiv = document.getElementById('valueDisplay');
var selectValue = document.getElementById('dropDown').value;
var divText;
if(selectValue == 1) divText = '[ Value 1 ][ Value 2 ]';
if(selectValue == 2) divText = '[ Value 1 ][ Value 2 ]';
outDiv.innerHTML = divText;
}
</script>
To achieve what you are trying to do with iframes would be more difficult. It would require you to use javascript/jquery to post the data within the iframe.
Note that using onchange="" is no longer the accepted method of adding listeners. It is just easier if you are not familiar with javascript.

Form 'SELECT' element returning incorrect value

Hello I have a form select element as follows:
<select name="color" id="color">
<option value="1" label="Red">Red</option>
<option value="2" label="Green">Green</option>
<option value="3" label="Blue">Blue</option>
</select>
When I submit the form, and check the $_POST, I get:
array('color' => 'Red')
Where it should be:
array('color' => '1')
I am a little confused, would it have something to do with the label attribute?
---- Edit ----
dojo.addOnLoad(function() {
dojo.forEach(zendDijits, function(info) {
var n = dojo.byId(info.id);
if (null != n) {
dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
}
});
dojo.parser.parse();
});
var zendDijits = [{"id":"color","params":{"autocomplete":"true","required":"true","dojoType":"dijit.form.ComboBox"}},...
Your code appears to be valid, but the label may be interfering with something. Since you don't need it (you use the same text as the text between the option tags), I suggest you remove it.
Try it this way:
<select name="color" id="color">
<option value="1">Red</option>
<option value="2">Green</option>
<option value="3">Blue</option>
</select>
Some more info on the label attribute.
Definition and Usage
The label attribute specifies a shorter version of an option.
The shorter version will be displayed in the drop-down list.
Although the information was not provided in the question for anyone to answer, the solution to the problem was this:
The form element was being created in Zend Framework as a Zend_Dojo_Form_Element_ComboBox, and I found the following information in the documentation.:
ComboBoxes return the label values, and not the option values, which
can lead to a disconnect in expectations. For this reason, ComboBoxes
do not auto-register an InArray validator (though FilteringSelects
do).
Changed the element to a Zend_Dojo_Form_Element_FilteringSelect, and the problem was resolved, working fine now.
Thanks to #devdRew who asked the right question that tipped me off on the thought of dojo/dijit changing the value of what is posted.

'Remember' form drop list value when submitting to SELF?

I know how to 'remember' some form values whenever submitting the form to itself, in this case because of a picture upload function which requires the form to be submitted to itself. I simply want it so that if the user has filled out all fields and then uploads an image, the form doesn't get resetted (cleared).
I have solved this in regular fields and checkboxes like this:
<input type="text" name="headline" id="headline" value="<?php echo #$_POST['headline'];?>">
But how can I do this with drop lists? or radio buttons? There is no value option in a 'SELECT' list, even though I have tried writing in value anyways in the SELECT statement. Didn't work!
So, how can I set the SELECT (drop down lists) value with PHP (OR JAVASCRIPT) ?
If you need more input let me know, thanks!
For selects, you need to compare each option to your posted value, and handle it individually. Simply print out your options in a loop, and test each value against the value was was previously posted. If it maches, add selected to the attributes of that particular option.
$color = $_POST["colors"];
$colors = array("red","green","blue");
<select name="colors">
<?php foreach ($colors as $option) { ?>
<option<?php print ($option == $color) ? " selected" : ""; ?>>
<?php print $option; ?>
</option>
<?php } ?>
</select>
Actually, found out that it is possible to set the selectedIndex with javascript...
So I could put the selectedIndex in a hidden input before submitting the form, and then get that selectedIndex and set it with a javascript function... tricky but suits me better in this case...
document.getElementById("select").selectedIndex=nr;
Thanks though Jonathan!

Categories