Select Option from list Only once - Laravel - php

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

Related

PHP selectfield get value from database, change and POST

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.

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?

Retrieve previously selected chosen dropdown subject value(s)

I'm having a problem with the coding because I cannot get the chosen dropdown menu multi-select Version 1.4.2 to retrieve the previous selected subject value(s) from the dropdown menu when I click it again to activate it.
I'm using two different tables with one field name for each one, so I'm trying to match the t_subjects field name(s) in the member table with the name field name in the category table and if there is a match, that subjects field name will result in <option value='subject_name' selected >subject_name</option> and where there is no match it will be <option value='subject_name'>subject_name</option>
This is what I'm trying to get to happen for those subjects that match when I click it anytime after the first post.
There are forty-two subjects that are dynamically loaded in a chosen multi-select dd menu option.
I have no problem with posting the subject(s) names to the database using the implode method.
if(isset($_POST['submit']))
{
$subjects = implode(',', $_POST['subjects']);
But, if I open the page again to make another post with the subjects field name blank, I get an error that says:
Warning: implode() [function.implode]: Invalid arguments passed in...
So, you can see why I need to have the previous subject(s) values selected when I activate it again. Here is the code for the chosen dd menu.
<tr>
<td width="32%">Subjects:</td>
<td width="68%">
<input type="radio" name="utype" id="sutype" value="s" />Subject(s)
<div id="studspan" style="display:none;">
<strong style="vertical-align:top;">Select Subject(s) :</strong>
<select name="subjects" id="subjects" data-placeholder="Select Subject(s)" style="width:350px;" multiple class="chosen-select">
<?php
$cat_sele = mysql_query("SELECT * FROM category");
while ($cat_row=mysql_fetch_array($cat_sele)) {
if($_POST['subjects'] == $cat_row['name']) $s = " selected"; else $s = "";
echo "<option value='{$cat_row['name']}'$s>{$cat_row['name']}</option>";
}
?>
</select>
</div>
The problem seems to be in this part of the code: if($_POST['subjects'] == $cat_row['name']), because when I replace $_POST['subjects'] with 'Accounting' to check it, it will show that Accounting has been selected when I activate it again, but if I add another subject name, it will show all of the subjects as selected. I even tried if($_POST['t_subjects'] == $cat_row['name']) as well as ("SELECT name FROM category"), but they didn't work either. So any help to help this teacher make it work will be very appreciative.
I have also included other parts of the code that are involved in the process too.
elseif($_SESSION['user_type']=="m")
{
$getuser_sele = "select * from member where member_id = '".$_SESSION['ses_id']."'";
$getuser_qry = mysql_query($getuser_sele);
$getuser_row = mysql_fetch_array($getuser_qry);
$subjects = stripslashes($getuser_row['t_subjects']);
=============================================================
elseif($subjects=="" && $_SESSION['user_type']=="m")
{
$error="Please Enter Preferred Subjects";
}
=============================================================
elseif($_SESSION['user_type']=="m")
{
$qr=mysql_query("update member set
t_subjects='".addslashes($subjects)."',
=============================================================
<body onload="$('.chosen-select').show();
$('.chosen-select').chosen();">
=============================================================
<script type="text/javascript">
$("#sutype").click(function () {
$("#studspan").show(1000);
$('.chosen-select').chosen('destroy');
$('.chosen-select').show();
$('.chosen-select').chosen();
});
</script>
<script src="js/chosen.jquery.js" type="text/javascript"></script>
=============================================================
Update:
Tristan, it works but it shows all of the subject names as selected and de-seleted in the choice list, which is not what I want to happen. I also got the following error in the source code for each of the subject names. I'm just showing the first one below.
<b>Warning</b>: array_search() expects parameter 2 to be array, null given in <b>/home/.../.../edit.php</b> on line <b>2784</b><br /> <option value='Accounting' selected>Accounting</option><br />
I think the problem is how the table/fields are setup in the dbase.
Here is what should happen based upon the first row of the t_subjects. If I log in as member 1 and open the chosen dd menu, Accounting,Athletics,Art from row one of t_subjects should be selected while the rest of the subject list would be non selected in the chosen dd menu. This would happen for each member's row t_subjects they have listed when they initially posted them the first time.
all images to show what I'm talking about.
Using <select name="subjects" ... multiple> will only send post data for one selection. You need to use <select name="subjects[]" ... multiple> to send all selected options in the post data.
Then $_POST['subjects'] will be an array so you will need to search the array for the category name.
$cat_sele = mysql_query("SELECT * FROM category");
while ($cat_row=mysql_fetch_array($cat_sele)) {
if(array_search($cat_row['name'], $_POST['subjects']) !== false) $s = " selected"; else $s = "";
echo "<option value='{$cat_row['name']}'$s>{$cat_row['name']}</option>";
}

Handling a lots of selects (dropdown lists)

Let's say, that i have users stored in a database, identified by an id (an integer primary key). All the users have a multiple choice attribute, for example privileges on a site (Admin, Member, Administrator etc...). I would like to create a php admin page, where i can select for each user his/her privileges, and all users at one page (or at least more users at one page):
Tom [_privileges_v_]
Ed [_privileges_v_]
Lisa [_privileges_v_]
...
How would it efficient to do?
if i print the dropdowns with the name attribute set as the id of the record, then how could i access them through php?
<select name="12323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<select name="4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
Then after i post this form, my $_POST array will contain a $_POST["12323"] and a $_POST["4323"] member.
How could i make something like this, so i could iterate through the $_POST values?
Thanks for any kind of help!
You can output a select box like this
<select name="user_4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<input name="otherinp" value=""/>
in case your form has another input.... then in php code you can do
foreach($_POST AS $name=>$value) {
if (strpos($name, "user_")===0){
$userid = str_replace("user_","",$name);
//Do st with userid
}
}
//Work with orther input $_POST['']
Either fetch all the users again and loop through their IDs, then check if there is a post value linked to it, or loop through all the post values.
foreach($_POST AS $name=>$value) {
echo "Selection box with name {$name} has value {$value}";
}

Problem With PHP/HTML Dropdown Box Code (selected value)

I wrote some PHP code that will allow me to create an html dropdown box, and then choose what should be the selected value. The code is:
$role_drop = "<select name='role'>";
$role_drop .= "\r\n<option value='Resident Assistant'>Resident Assistant</option>";
$role_drop .= "\r\n<option value='Community Assistant'>Community Assistant</option>";
$role_drop .= "\r\n<option value='Head RA'>Head RA</option>";
$role_drop .= "\r\n</select>";
$result = mysql_query("SELECT role FROM users");
if (#mysql_num_rows($result)) {
while ($r=#mysql_fetch_assoc($result)) {
$role = $r["role"];
$role_drop = str_replace(">$role</option>", "selected=\"\" >$role</option>",$role_drop);
echo $role_drop;
}
}
In reality, this code has a bunch of HTML mixed in, but here is all of the PHP. When I run it, it seems to work. However, let's say the query returned 4 dropdown boxes with roles (from 4 users), and I were to Edit, or select, a new role for the 2nd dropdown box returned (with an UPDATE query), then when the page refreshes, all of the roles including and AFTER the dropdown box I updated will display their selected values as the new one I selected in the 2nd dropdown box.
And it's not that the values in the actual database are wrong, they are just displaying the wrong selected value. Here is the source code for the 3rd dropdown box after I select a new value for the second one:
<select name="role">
<option selected="" value="Resident Assistant">Resident Assistant</option>
<option value="Community Assistant">Community Assistant</option>
<option selected="" value="Head RA">Head RA</option>
</select>
So, it seems its selecting the correct value (Resident Assistant), however its ALSO selecting "Head RA", which is what I changed the prior dropdown box to.
It's very strange, and I have NO idea why this is happening. Any ideas?
Thanks!
It's because you're updating $role_drop each time, so all the previous changes are going to show up in subsequent dropdowns. I'd change the loop to something like this:
if (#mysql_num_rows($result)) {
while ($r=#mysql_fetch_assoc($result)) {
$role = $r["role"];
$temp_role_drop = str_replace(">$role</option>", "selected=\"\">$role</option>", $role_drop);
echo $temp_role_drop;
}
}
That way you're not overwriting your original dropdown markup.
Nuts - forgot to escape my code. I meant, "It's just <OPTION VALUE="foo" SELECTED>".
Dunno if this helps, but according to the HTML spec you shouldn't be passing a value along with the SELECTED attribute of each OPTION. It's just .

Categories