Php Laravel bind php variable with html select - php

I am very new to php laravel.
I need to populate a dropdown in php blade with certain list I send to the view and I need to bind the selected value with a certain php variable.
My code:
<div class="dropdown" id="managerDropdown">
{{ Form::select($row->manager_id,Manager::getSelectOptions(),$row->manager_id) }}
</div>
I'm not understanding how do I bind my "$row->manager_id" with this drop down, so that when I submit, "$row->manager->id" will have selected records id(value).

Related

How to specify pressed links/buttons in a foreach loop

The following is a foreach loop that will show a group of buttons coming from DB.
#foreach($acc as $accs)
<div class="col-xs-1">
{{$accs->first_name}}
</div>
#endforeach
As I click on one of the buttons, I want to show a new view with all same buttons but ONLY the selected button highlighted (e.g. class="btn btn-warning"). If I add the below code in the new view, all buttons will be highlighted, not only the selected one.
Any idea how can this be done?
#foreach($acc as $accs)
<div class="col-xs-1">
{{$accs->first_name}}
</div>
#endforeach
You should add a new property for that button like $is_highlighed in the controller send to the view and in view make an if statement for finding this button
you have two way for this goal
add a new field in DB like is_highlighed (Boolean)
or
you can add a property in the controller
for example, you get a collection from DB by the model and have some values like id, text, color and etc
and you want to make highlighted blue button so in the controller, you check the color and if button color is the blue that adds an is_highlighed = true

How to fix cant retrieve data with jquery

i should tell you my code is actually work, but a litle bit of annoying problem i cant solve by my self.
The problem is i only can retrieve the first record(id) from my data grid but i cant retrieve the second or other record id
example :
when i click my edit button of my first record of my data then i want to send the id to my jquery and show it in console and use it for selecting data form database (this is work)
but when i click edit of my second record of my data it wont send data to my Jquery and show it in console
This is my Grid code
<?php foreach($data['gakubu_data'] as $gakubu):?>
<li class="list-group-item d-flex justify-content-between align-items-center"><?= $gakubu['code_gakubu'];?> | <?= $gakubu['gakubu'];?>
編集
</li>
<?php endforeach;?>
</div>
</div>
this is my Jquery Script
$(function(){
$('#showUpdateModal').on('click',function(){
$('#formModalLabel').html('データ編集');
$('.modal-footer button[type=submit]').html('データ編集');
const id = $(this).data('code');
console.log(id);
});
});
Your loop creates multiple elements with the same id (id="showUpdateModal"). Id's must be unique within a document, so when you select it with: $('#showUpdateModal'), it will always return the first element with that id (since there shouldn't be any more elements with the same id).
You can solve it by using a class instead:
編集
(I removed the id and added showUpdateModal as a class instead)
Now you can register the event on the class instead:
$('.showUpdateModal').on('click', function() {
// Your current code
});

getting selected value from drop down list in laravel

I'm new to laravel, in the view page I'm showing a Dropdown-List which is fetched from the database and based on that Dropdown-List value selection, I'm getting the corresponding value id. But I also want one more corresponding fields from database based on the Dropdown-List I'm selecting.
How to get it? Based on that values I want to do Javascript operation.
Here's my code:
{{
Form::select('asset_type_id',$assettype_list, Input::old('asset_type_id'),
array('class'=>'select2', 'onchange' => 'check(Input::get(this.value);)',
'style'=>'width:350px'))
}}
You cannot use Input::get(this.value) in your onchange as it will be printed literally. You can just use the code without the Input::get. Of course, this all depends on how you are using the value in your javascript.
{{
Form::select('asset_type_id',$assettype_list, Input::old('asset_type_id'),
array('class'=>'select2', 'onchange' => 'check(this.value)',
'style'=>'width:350px'))
}}

Create <select> elements on the fly and send multiple POST as an array using jQuery and PHP

I wonder if it's possible to populate <select> elements with option values generated by PHPto a page (after load) whenever a user is clicking a button withid='add'and then be able to store each value selected as an array then send this via $_POST to a PHP page?
I would like to populate the <select> element like this:
/theloader.php
echo "<select class='plan_id'>\n";
while ($row = mysql_fetch_array($course_elements)) {
echo "<option value='".$row['plan_id']."'>".$row['plan_name']."</option>\n";
}
echo "</select>\n";
?>
This will output
<select class='plan_id'>
<option value='1'>Description</option>
<option value='2'>Description</option>
<option value='3'>Description</option>
</select>
Then when the user has populated the needed elements and selected values, click save and send everything to anotherfile.php:
$("#save").click(function() {
// Store each value as a variable then send to /anotherfile.php
});
I lack knowledge in how to perform this kind of action and would really appriciate som tips in the right direction.
Thank's in advance.
Is there any reason the population of the selects has to be dynamic? Does the data change, or are you just letting the user add more line items to choose from pre-set options?
If the data in the dropdown itself is dynamic, you will need a combination of javascript and ajax calls to get the data from the php in real time.
If the options are not dynamic, then you do not need ajax at all, just populate the variables in javascript and create an action to add more dropdowns using the already defined variables.

Create dynamic form fields based on a dropdown , jquery ,php

I have a dropdown in my form which actually helps generate mysql db queries on the fly based on the query type .
e.g if my querytype in the dropdown in fetchYourDetails , then a text box will appear in the form which will ask for my name . Once , I enter my name and click on generate , it will frame the query and run it in the background php.
Like this I have around 4/5 query types and it's likely to increase in the future. Here I have a problem with my current implementation .
What I'm presently using is : a form where I have the dropdown and all the parameter fields (hidden) to start off with . Now, with the onchange() function in my js , I will show some specific parameter rows based on the dropdown option selected and hide the rest.
so here's how my HTML code looks like :
<form>
<table>
<tr id='dropdown'>
<td>
<select id='select_options' onchange ="javascript:updateCustomQueryFields();">
<option value='getyourdetails'></option>
<option value='getyourbikedetails'></option>
<!-- so on -->
</select>
</td>
</tr>
<tr style='display:none'>
<td>
<!-- one field -->
<!-- create the generate option -->
</td>
</tr>
<tr style='display:none'>
<td>
<!-- one field -->
<!-- create the generate option -->
</td>
</tr>
<!-- so on -->
</table>
</form>
and my onchange function looks like :
function updateCustomQueryFields()
{
val=$('#select_options').val();
//hideallfieldsfirst
if(val=='getyourdetails')
{
//show one field
}
else if(val=='getyourbikedetails')
{
//show one field
}
}
so far so good , now , when I click on generate option , it will execute the query and fetch the data from the db and display it in a results div below the form.
So, at the time of displaying results also, I have to hide some fields and display others in the view , based on the parameters set in the controller.
So the series of if-else statements are likely to increase there as well , when we add some more parameters .
So what I want is some mechanism by which we will generate the parameter fields on the fly , e.g , if the query type is 'getyourdetails' , only the name field should be created and rest all fields should be removed in the backend onchange function .
So how can that be achieved using jquery ? and will it be a proper methodology to implement the scenario what I want ? I have to keep in mind the propulating of my parameter fields once the call returns from the controller after fetching the results.
I'm not sure that I understood your question but at least my knowledge about your request says for making these kinds of forms it's better to use remove and append functions of jQuery. You can create any control dynamically and set them under control by the ID that you set for them. It's easy as drinking a glass of water!
Remove, Append
Correct me if my answer is wrong.

Categories