Whats the possibility of one updating a database field with a form having a drop down without changing the value of the drop down field - php

Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.

I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.

Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>

What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.

Related

select multiple options at a time

<select class="form-control default-select2 " id="group" data-size="10" data-live-search="true" data-style="btn-white" multiple="multiple">
<option value="" disabled="disabled" selected="selected">Select Group</option>
<!-- <option value="1">Admin</option>
<option value="4">User</option> -->
<?php $groups = $objUser->GetGroups(); ?>
<?php foreach($groups as $group){ ?>
<option value="<?php echo $group['id'] ?>"<?php echo ($data[0]['group_id'] == $group['id'] ? "selected='selected'" : "") ?>><?php echo $group['primary_name'] ?></option> <? } ?>
</select>
Now I want user to select multiple option at a time. i searched the whole internet but i couldn't find the answer to fix this issue. i dont know what i am missing.
Your code can actually manage multiple selections. Just hold Ctrl key and click on different options.
Exemple of multiple select : here
When you submit the form, you will be able to get all the selected options into an array.

HTML select required validation

I want to validate select option but it's not working for me
this is my source code
<?php echo form_open_multipart('dashboard/add_item') ?>
<select name="provinsi_id" id='propinsi_id' required>
<option value="0">-Select Category-</option>
<option value="1">Graphics</option>
<option value="2">Fonts</option>
<option value="3">Prints Template</option>
</select>
<button type="submit" class="button big dark">Next<span class="primary"> Step</span></button>
<?php echo form_close(); ?>
required on a <select> will reject if the selected option has no value.
Try setting the value on the first option to "" instead of "0".
<option value="">-Select Category-</option>

How to $_GET values from sql to be inserted in <select type> PHP

I'm using xampp PHP
How to do I get values from MySQL in select type like getting value in
<input type="text" value="<?php $variable['valueinsql']; ?>">
But I want the value to be inserted in
<select type>
And I tried
If(isset($_GET['id'])){
<p> Banks:</p><select multiple="multiple" name="otherbanks[]" style=" width:190;" value = "<?php $list["Banks"] ?>"">
<option value="Banksone">One</option>
<option value="Bankstwo">two</option>
<option value="Bankthree">three</option>
I want to see it automatically highlighted.
Use in_array to check the value available in array or not. If available echo 'selected';
Try this ==>
<select multiple="multiple" name="otherbanks[]" style=" width:190;">
<option <?php if (in_array('Banksone',$list["Banks"])) {echo 'selected';} ?> value="Banksone">One</option>
<option <?php if (in_array('Bankstwo',$list["Banks"])) {echo 'selected';} ?> value="Bankstwo">two</option>
<option <?php if (in_array('Bankthree',$list["Banks"])) {echo 'selected';} ?> value="Bankthree">three</option>
</select>
To highlight particular options the selected attribute should be set on each option that is applicable - as below example.
<select name='otherbanks[]' multiple=true>
<option value=1 selected>one
<option value=2>two
<option value=3 selected>three
<option value=4>four
<option value=5 selected>five
</select>
Presumably you generate the select menu using PHP and a query to the database so to accomplish that ( highlighting ) in PHP you would need a loop to iterate through each value in $list["Banks"] and check the value against the value from sql. Without knowing exactly how you generate the select menu or knowing the value of $list['Banks'] it is hard to know exactly and I may be "barking up the wrong tree" as it were.

trying to display previously selected value in dropdown list php

I have made some code to create a dropdown in a webpage and you can select among 2 currency values namely USD and SGD. I have been able to get the value for the currency field while entering the data in the database. But while trying to edit the entry in the database, I am able to use $_POST to get all entries to display except the value of currency. I would ideally want to display the previously selected value on the dropdown. As of now the drop down on the edit page just displays the default "Please Choose" and doesn't show the previously selected value. any help would be greatly appreciated.
Code :
<select id="currency" name="currency" placehoder="Currency">
<option value='' disabled selected style='display:none;'>Please Choose</option>
<option value="SGD">SGD</option>
<option value="USD">USD</option>
</select>
And I am trying to somehow display the previously read value that exists in the database and display that instead of the "Please Choose" so that while editing I don't have to re-select the currency value.
Supposing you stored in $currency the value from DB:
<select id="currency" name="currency" placehoder="Currency">
<option value='' disabled style='display:none;'>Please Choose</option>
<option value="SGD"<?php echo $currency == "SGD" ? " selected" : ""; ?>>SGD</option>
<option value="USD"<?php echo $currency == "USD" ? " selected" : ""; ?>>USD</option>
</select>
Use PHP to Solve it..
<select name="select_limitby" onChange="frm_sub()">
<?php if($_SESSION[select_limitby]!='') { ?>
<option value="<?php echo $_SESSION[select_limitby]; ?>" <?php if($_POST[select_limitby]=='$_SESSION[select_limitby]') {?> selected="selected" <?php }?>><?php echo $_SESSION[select_limitby]; ?></option>
<?php } ?>
<option value="">Default</option>
<option value="9" <?php if($_POST[select_limitby]=='9') {?> selected="selected" <?php }?>>9</option>
<option value="12" <?php if($_POST[select_limitby]=='12') {?> selected="selected" <?php }?>>12</option>
<option value="15" <?php if($_POST[select_limitby]=='15') {?> selected="selected" <?php }?> >15</option>
</select>
USE session if it does not work...
otherwise you can leave session...

How do I Pass Values from One Form to Another?

I have this form:
<form name="summaryform">
<select name="category" id="category" style="width:500px;">
<option value="">Choose category...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
</select><br>
<select name="subcategory" id="subcategory" style="width:500px;">
<option value="">Choose sub category...</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
<option value="Scheme">Scheme</option>
</select><br>
<input type="text" name="comments" id="comments" value=" Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">
On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments")
How do I pass the values from this form to another form?
Let's say below is the form I want to pass the values to:
<div>
<p>
<form name="summaryform" method='POST' action='process.php'>
<div style="font-size:12pt;">
value from one dropdown
value from the other dropdown
value from textbox
</form>
</p>
</div>
jQuery Method
If the forms are on the same page, you can use jQuery to get the values and write to the div:
var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);
PHP Method
If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for.
NOTE: for this method to work the page being POSTed to MUST be PHP. You will also need to add an action and method in the first form to POST to the second form.
This would be the code in the form on the page being POSTed to:
<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
A value can be passed between forms using the POST and GET methods. The first form (Which is sending the values) does not have this defined, so modify the first form to this -
<form name="summaryform" method='POST' action='process.php'>
Then, in process.php, put the following php code -
$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];
After that, you can just use the variables as you see fit!
If you want just print the selected values from first form #zsaa14's
answer is good
If you want to print whole select list, autoselected your value, heres a code.
foreach($categories as $category) {
$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';
echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;
}
NOTE: the variable names and keys are for example, use Yours. This is only fragment of code.
You can use post data fields to send the values to the next form

Categories