PHP jQuery Data-Attribute populate dropdown - php

I have an HREF with various data-attributes (about 8) that come from a PHP query and loaded into a table. When you click the button, it takes the data-attributes via jQuery and sends them to a modal window, where I can use them in a form.
I can open the window and place the attributes into an INPUT field with no problem.
What I would like to do is place the attribute in the first OPTION in a SELECT dropdown.
Here is the button with the attributes:
<tr><td><a href='' class='modAccount'
data-modcode=".$row[partner_code]."
data-modname=\"".$row[partner_name]."\"
data-boiler=\"".$row[boilerplate]."\"
data-surcharges=\"".$row[no_new_surcharges]."\"
data-ccalog=\"".$row[cca_logistics]."\"
data-toggle='modal'>Click</a></td></tr>
I have quite a few data-attributes. I had to eliminate a few for this question.
Here is the jQuery used to retrieve the data-attributes and send them to a modal window, called modifyModal.
Here is the jQuery:
$(function()
{
$('.modAccount').click(function(e)
{
e.preventDefault();
$partnerCode = $(this).attr('data-modcode');
$partnerName = $(this).attr('data-modname');
$boiler = $(this).attr('data-boiler');
$surcharges = $(this).attr('data-surcharges');
$ccalog = $(this).attr('data-ccalog');
// the 5 below currently populate the INPUT fields for the ID's listed
$('#modpartnerCode').val($partnerCode);
$('#modpartnerName').val($partnerName);
$('#modplatform').val($platform); // <- should go to dropdown
$('#modboiler').val($boiler); // <- should go to dropdown
$('#modsurcharges').val($surcharges); // <- should go to dropdown
});
});
$(document).on("click", ".modAccount", function ()
{
$('#modifyModal').modal('show');
});
As you've probably read in the note above, I can send the attributes to the INPUT fields with their respective IDs.
I need to be able to populate the first OPTION of a dropdown SELECT field.
I don't think I need to show the MODAL window code. As stated, I can populate the INPUT fields using the ID's I listed.
How can I get the IDs to populate the dropdown SELECT first OPTION value?
Here is the code for the modal:
<div class="modal fade" id="modifyModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
</div>
<div class="modal-body">
<form role="form" action="api/modifyAcct.php" method="get" id="modAcctForm" name="modAcctForm">
<input readonly type="text" class="form-control" id="modpartnerName" name="modpartnerName" />
<input readonly type="text" class="form-control" id="modpartnerCode" name="modpartnerCode" />
<select class="form-control" id="modboiler" name="modboiler" />
<option value=""></option> // <- #modboiler goes here
<option value="Y">Yes</option>
<option value="N">No<option>
</select>
<select class="form-control" id="modsurcharges" name="modsurcharges" />
<option value=""></option> // <- #modsurcharges goes here
<option value="Y">Yes</option>
<option value="N">No</option>
</select>
<select class="form-control" id="modplatform" name="modplatform" />
<option value=""></option> // <- #modplatform goes here
<option value="Y"></option>
<option value="N"></option>
</select>
</div>
</div>
</div>
</div>

Use jquery to select the first option and set the value.
$('#modplatform option:first').val($platform);
$('#modboiler option:first').val($boiler);
$('#modsurcharges option:first').val($surcharges);

Related

laravel fetch only the previously selected option submitted previously on a edit form

I've got a form that submits correctly, but it has a value on a question appended to a name, now to edit i'm almost done in the edit form, but the blade php with the foreach argument fetches all options from the select, i want to show and be able to edit the previously selected option. how do i do this on blade php.
resuming i want to submit it first then in dashboard of the form results have a button of edit and be able to edit the previously submited forms.
here's the code:
<select name="quilometragem" id="mySelect" class="chosen-select" onchange="myFunction()">
#foreach($viaturas as $item)
<option value="{{ $item->matricula }}:{{ $item->quilometragem }}">
{{ $item->matricula }}
</option>
#endforeach
</select>
<p id="demo"></p>
<div class="form-group mb-3">
<label for="">Chegada Kms</label>
<input type="number" name="kmfim" class="form-control">
</div>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "Matricula:QuilómetrosCorrentes: " + x;
}
</script>

Assign "select" attribute to jquery select2 dropdown dynamically

I am working on jquery select2 dropdown. I have to display the multiple selected option in select area. For this purpose i loop through each of select2. If condition is met i will assign 'selected' attribute property to option so that it may appears in the select area.
But somehow the select2 fails to display the selected ''.
HTML CODE:
<div class="form-group col-md-12">
<label for="inputsskill">Skill<span class="required-field">*</span></label>
<fieldset>
<select class="custom-select w-100" name="skill_name[]" id="skill_name" multiple="multiple" style="width: 100%;">
#if(!$skills->isEmpty())
#foreach($skills as $skill)
<option value="{{$skill->id}}">{{$skill->name}}</option>
#endforeach
#endif
</select>
</fieldset>
</div>
JQUERY CODE:
// parse skills
var parseData = JSON.parse(skill);
// parseData is an array which contain 1,2,3 values like ['1','2','3']
$("#skill_name > option").each(function() {
// if select2 and array values matches assign selected attribute to that option
if( $.inArray($(this).val(), parseData) !== -1 ) {
$(this).attr('selected', 'selected');
}
});
Select2 Jquery:
$( "#skill_name" ).select2({
});
I dont know where i am going wrong, i would appreciate if someone guide me in this.
Thanks,
you can achieve this functionality with PHP easily.
E.g : fetch skills from the database which you want to preselect and create an array of skills. Then match this in your loop.Like
<div class="form-group col-md-12">
<label for="inputsskill">Skill<span class="required-field">*</span></label>
<fieldset>
<select class="custom-select w-100" name="skill_name[]" id="skill_name" multiple="multiple" style="width: 100%;">
#if(!$skills->isEmpty())
#foreach($skills as $skill)
<option value="{{$skill->id}}" <?php
if(in_array($skill->name, $selectedskillsArray)){
echo $selected = 'selected';
}
?> >{{$skill->name}}</option>
#endforeach
#endif
</select>
</fieldset>
</div>
Try this one.
Jquery solution
$( "#skill_name" ).select2({
});
$('#skill_name').val(["1","2","3"]).trigger('change');

Redirect selected option on submit laravel

How can I redirect my selected option when I click submit?
I am trying to make a modal Sign Up that is located in the header for different users. When the user choose one, they will then be redirected to specific sign up page.
This is from my header.blade.php
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Choose Roles/Plan</h4>
<div class="input-field col s12">
<select id="select-action" name = "action">
<option value="" disabled selected>Choose your option</option>
<option value="basicsignup">Basic</option>
<option value="advancedsignup">Advanced</option>
<option value="teamsignup">Team</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="submit">Submit</button>
</div>
</div>
routes.php
Route::get('/basicsignup', function(){
return view('actions.basicsignup');
})->name('basicsignup');
Route::get('/advancedsignup', function(){
return view('actions.advancedsignup');
})->name('advancedsignup');
Route::get('/teamsignup', function(){
return view('actions.teamsignup');
})->name('teamsignup');
Edit: Original examples were animals, flowers, cars.. edited it for clarity at least.
You can simply use JavaScript onsubmit function to call a JS function than get your selected value and then you can submit a form with your customised url.
Example
<form method="post" id="myForm" onsubmit="return submitForm()">
<select id="select-action" name="action">
<option value="" disabled selected>Choose your option</option>
<option value ="animals">Animals</option>
<option value ="flowers">Flowers</option>
<option value ="cars">Cars</option>
</select>
<button type="submit">Submit</button>
</form>
<script type="text/javascript">
function submitForm() {
var selectedOption = $('#select-action').val();
var url = "";
if(selectedOption == 'animals') {
url = '{{ route('your/route')}}';
} else if (selectedOption == 'flowers') {
url = '{{ route('your/route')}}';
}
.
.
.
$('#myForm').attr('action', url);
$('form#myForm').submit();
return false;
}
</script>

How to change content in div, based on dropdown selection

I load my content from my DB with PHP, into my div.
I have a dropdown above it, where i should be able to sort out the content, meaning that when dropdown is chosen, the old content in the div is removed, and the new appears.
So how is it possible to "remove" the old content in the div, and get the new content based on the dropdown selection ?
This is what i got so far:
<div class="col-md-4">
<ul class="nav nav-tabs top_nav">
<li role="presentation" class="active">Edit</li>
<li role="presentation" class="inactive">
<!-- THE DROPDOWN THAT SHOULD SORT THE CONTENT BELOW -->
<!-- REMOVE OLD, AND GET NEW AFTER SELECTED -->
<select id="filter_type">
<option selected value="Alle">Alle</option>
<option value="Privat_Tale">Privat Tale</option>
<option value="Erhverv_Tale">Erhverv Tale</option>
<option value="Gamle">Gamle</option>
<option value="Privat_MBB">Privat MBB</option>
<option value="Erhverv_MBB">Erhverv MBB</option>
<option value="Familietele">Familietele</option>
<option value="Retention">Retention</option>
</select>
</li>
</ul>
<!-- LOAD THE CONTENT FROM DB, AND SHOW IT -->
<div class="abo_load" id="abo_load">
<?php
if (mysql_num_rows($sql_select_edit) > 0) {
while ($row = mysql_fetch_assoc($sql_select_edit)) {
?>
<div class="abo_box" id="abo_box">
<label class="abo_navn"><?php echo $row['abo_name']; ?></label>
<input type="hidden" value="<?php echo $row['category']; ?>" name="category" />
<form action="conn/delete.php" method="post">
<input type="hidden" value="<?php echo $row['id'];
?>" name="slet">
<input class="delete" value="Delete" type="submit" onclick="return confirm('Er du sikker??\nDet ville jo være ÆV med en Fejl 40!');">
</form>
<label class="edit">Edit</label>
<?php include("files/edit_list_content.php"); ?>
</div>
<?php
}
}
?>
</div>
</div>
EDIT:
I have already made it with JQuery, i load a php page, that gets the selected item, and then get the data from the DB. But the problem is, that when its loaded, i can't use any JQuery to manipulate with the new content. Like i have a edit label on the content with a JQuery hide function on. That function is working when i get the data though php from my DB, but not when i get the data though JQuery load. Thats why i want to load the data though php
You need to bind change event on your dropdown & based upon selection, fire an ajax request to get related content & then upon receiving update content inside DIV.
If you are using jQuery, Some useful functions would be:
Ajax: http://api.jquery.com/jquery.ajax/
Binding event: https://api.jquery.com/change/
A good example of implementation has been given here:
https://stackoverflow.com/a/4910803/2004910

How can I get the values from a dropdown?

Im trying to get some values of my dropdown in php. Its in div, so how can I do that?
<div class="ui dropdown selection" tabindex="0" style="right: 120px;">
<div class="default text" name="dif" id="dif">Difficulty</div>
<i class="dropdown icon"></i>
<div class="menu" id="difficulty" name="difficulty">
<div class="item" value='0' data-value="0">Easy</div>
<div class="item" value='1' data-value="1">Normal</div>
<div class="item" value='2' data-value="2">Hardcore</div>
</div>
</div>
For example, if user select "Easy", I will get with php the value 0.
I tried:
$difficulty = ($_POST['difficulty']) ? $_POST['difficulty'] : '0';
You can access them in jQuery or Javascript:
http://robertnyman.com/2010/04/29/using-html5-custom-data-attributes-to-store-data-on-html-elements/
You could store the value in a hidden form and field and use .submit()
http://api.jquery.com/submit/
And then check for your post value. Divs do not store values, they are containers so adding 'value=0' will not work. But you can access attributes using JS or jQuery.
Edit: IGNORE BELOW I just saw your edit.
If you're using PHP, you can POST the data to another page.
<form action="process.php" method='post'>
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Then on the process.php page you can access the value with the superglobal: $_POST['cars']
You can't get them from a div with just PHP. I'm guessing that there is some Javascript that is updating a hidden input somewhere on the page when the user selects an option from this stylized dropdown list. You'll need to find that hidden input, and that's where your data is coming from when the form is submitted.

Categories