how to get dynamic element for ajax - php

I know i made lots of mistake thats why I come to here.
Actually I have some data
According to data I have Indication 1, Indication 2, Indication 3 and Indication 4, for each indication there is some message for user and if I select 2, 3 or 4 then the message will move to that corrosponding indication. And really I dont have idea how to do it becouse these message are coming from dynamicaly as:
$i=1;
while($row=mysqli_fetch_array($qry))
{
?>
<div class="solo"><div style=" width:100px;height:24px;float:left;padding:4px ;">
<select style="width:60px;font-size:14px;" onchange="move(this.value);">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<!--<option value="60min">60min</option>
<option value="other">other</option>-->
</select>
</div>
<div style=" width:600px;height:24px;float:left;padding-top:8px;text-align:center;"> <?php echo $row['message'];?></div>
<div style=" width:100px;height:24px;float:left;padding:4px ;">
<div style="width:100px; float:left;">
<input style="float:left;" type="checkbox" value="<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>" name="foo[]"/><div style="padding:2px; float:left;">Kill</div>
</div>
and I am using to get these message id
<script type="text/javascript">
function move(str)
{
var a=document.getElementsByClassName("mid")[0].value;
alert(a);
}
</script>
and i display this message id as in hidden text box:
<input type="hidden" name="msg" class="mid" value="<?php echo $row['id']; ?>">
am just confuse how to get a particuler message id for each message becouse above move() function is only giving one id by [0]
So I just want to know how to do this for each element when all the input box have same class.

If i understand correctly you want to iterate over the result list of getElementByClassName.
For this have a look at: https://stackoverflow.com/a/15843940/1885950
In your case it will look like:
var mid_holders = document.getElementsByClassName("mid");
for(var i = 0; i < mid_holders.length; i++)
{
alert(mid_holders.item(i).value);
}

document.getElementsByClassName("mid") gives you an array of dom elements. You are currently taking only the first element in that array. So loop through it and get each ones value attribute
function move(str) {
var a=document.getElementsByClassName("mid");
for(var i=0;i<a.length;i++)
alert(a[i].value);
}
update
Its not clear what you are trying to achieve.
If you are trying to alert the value of dom element with index as str, then the function could be as simple as
function move(str) {
alert(document.getElementsByClassName("mid")[str].value);
}
if not please mention what you are trying to achieve in your question.

Related

how to get all values from a select2 multiple after submitting form php?

After selection of several elements (tasks) inside the select2 multiple, I submit a form and I would like to get in POST the data with PHP.
However, I get only the last selected value and can't get the others (dd($_POST['ovr_tch']); returns only the last value).
<select name="ovr_tch[]" id="field_ovr_tch" class="form-select" multiple style="resize: vertical; width: 100%">
<option value="" selected disabled>
Veuillez sélectionner une tâche...
</option>
<?php foreach ($tasks as $task) { ?>
<option value="<?php echo $task['PK_TACHE_TCH']; ?>">
<?php echo $task['LB_TACHE_TCH']; ?>
</option>
<?php } ?>
</select>
<script>
$('#field_ovr_tch').select2();
</script>
Any help would be grateful!
Based on what you have
<select name="ovr_tch[]" id="field_ovr_tch" class="form-select" multiple style="resize: vertical; width: 100%">
You can access the selected values like this
$selectedValues = $_POST['ovr_tch'];
Edit: Make sure your select2 library is properly configured
$('#field_ovr_tch').select2({
multiple: true
});
Then loop through $selectedValues to access each value
foreach ($selectedValues as $selectedValue) {
// do something with $selectedValue
}

PHP concatenation $i in jQuery

Bit of a strange one, I have a webpage that iterates through a set number of times using while ($i <= 21):. During this iteration I have a dropdown in each which displays some values I pulled from a database, at this point that specific value doesn't matter but the id of each dropdown uses the value of $i. For example:
<select name="dropdown<?php echo $i; ?>" id="dropdown<?php echo $i; ?>">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
What I am then wanting to do is take the value selected from that dropdown and place in another field that also has the id corresponding to the iteration number, for example:
<textarea name="textfield<?php echo $i; ?>" id="textfield<?php echo $i; ?>"></textarea>
I am using the following code for the jQuery to get the value from the dropdown and put it into the textarea:
<script>
$("#dropdown<?php echo $i ?>").on("change",function(){
//Getting Value
var selValue = $("#dropdown<?php echo $i ?>").val();
//Setting Value
$("#textfield<?php echo $i ?>").val(selValue);
});
</script>
However it doesnt seem to like the use of <?php echo $i ?> part as if i replace that with for example 2 or 3 then it works for that iteration.
I have tried setting the variable in PHP like: $textfield= 'textfield'.$i; and the using this as a whole in jQuery like this: $("#<?php echo $textfield ?>").val(selValue); but it doesnt like that either. Interstining if I change it to $textfield= 'textfield'.'2'; or $textfield= 'textfield2'; it works. Seems like it doesnt like my use of $i, is it the PHP concatenation that it doesnt like?
Anyone experienced this or know of a fix?
Huge fixes are required in your code but will explain the solution in minimal coding.
use array in name,dont use id,use class and data attribute to store the loop value as follows
<select name="dropdown[]" class="dropdown" data-id="<?php echo $i; ?>">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<textarea name="textfield[]" class="textfield<?php echo $i; ?>"></textarea>
now in script single piece of code is enough to perform your operation with the help of class dropdown and dynamic class textfield
<script>
$(".dropdown").on("change",function(){
//Getting Value
var selValue = $(this).val();
var dynamic_id = $(this).data('id');
//Setting Value
$(".textfield"+dynamic_id).val(selValue);
});
</script>

jQuery and PHP to update Select Box value in Text field

I know this question has been asked a lot of times in several other ways but none of them helped me. So, I have formatted it in my own words.
Supppose I have a select box like this:
<select name="something">
<option value="1"><?php echo $value1; ?> for <?php echo $value2; ?>
</select>
<input type="text" name="sometext" value="">
I want to have the <?php echo $value1; ?> in the text field updated live on change of the select box option. To be clear, I DO NOT want the value="1" in the text field and I need to have that value="1" there for some reason and I cannot replace it with value="<?php echo $value1; ?>". I strictly want the inside value <?php echo $value1; ?> to be replaced in the text field. But I do not know how I can achieve it. Please help me experts. For live changing jQuery preferred.
Try below code:
<select name="something">
<option value="1" data="<?php echo $value1; ?>"><?php echo $value1; ?> for <?php echo $value2; ?></option>
</select>
<input type="text" name="sometext" value="">
See below working HTML
jQuery(document).ready(function($){
$('#something').change(function(){
$('#sometext').val($(this).find(':selected').attr('data'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="something" id="something">
<option value="1" data="value1">value1 form value01</option>
<option value="1" data="value2">value2 form value02</option>
<option value="1" data="value3">value3 form value03</option>
<option value="1" data="value4">value4 form value04</option>
</select>
<input type="text" name="sometext" id="sometext" value="">
Hope this will work for you,
$(document).ready(function() {
$('select').change(function(){
var selectedOption = $("select option:selected").text(); //Get the text
selectedOption = selectedOption.split("for"); //split the 2 value by using for
alert(selectedOption[0]); //Get the value before for
$('input').val(selectedOption[0]);
});
});
You need to fetch the selected option text instead of the usual option value, it is pretty easy to do.
You don't need to add any data attributes to achieve this, as there is a method to fetch the actual option text.
Here is a working example:
PHP for populating a select element (below I show the static HTML)
<select id="something" name="something">
<?php foreach($selectList as $value=>$text){ ?>
<option value="<?php echo $value;?>"> <?php echo $text;?> </option>
<?php } ?>
</select>
HTML
<select id="something" name="something">
<option value="1"> Value 1 </option>
<option value="2"> Value 2 </option>
<option value="3"> Value 3 </option>
</select>
<input type="text" id="sometext" name="sometext" value=""/>
jQuery
$(document).ready(function(){
$('#something').on('change',function(){
var selectedText = $(this).children(':selected').text();
$('#sometext').val(selectedText);
});
});
This approach is fast and easy to maintain.
And of course a jsfiddle to see it in action: https://jsfiddle.net/kzgapkt5
Hope it helps a bit

How to update data when input is drop down?

I am so new in html, php, javascript, mysql. i created a drop down menu using java script that contains a list of 2011 to 2510. During update i can not display the stored value that is generated using java script. I searched but found nothing. A portion of code.....
<select name="eca">
<option id="eca" required="required" class="ecadetail" value="NSS" <?php if ($eca == 'NSS') echo 'selected="selected"';?>">NSS</option>
<option id="eca" required="required" class="ecadetail" value="NCC" <?php if ($eca == 'NCC') echo 'selected="selected"';?>">NCC</option>
<option id="eca" required="required" class="ecadetail" value="Cultural" <?php if ($eca == 'Cultural') echo 'selected="selected"';?>">Cultural</option>
</select>
</div>
<div class="label">Year</div>
<div class="inputyear">
<select name="years" >
<script language="JavaScript">
// loop to create the list
var year = 2010
for (var i=1; i <=500; i++)
{
year++;
document.write("<option>" + year + "</option>");
}
// end JS code hide -->
<option value="<?php if ($year == 'year') echo 'selected="selected"';?>"></script>
</script>
</select>
</div>
</div> <!-- end of 7th row -->
the value for eca is working fine. The value year is stored in $year.
help please...
This loop can purely done in php then why involving the javascript
<select name="years" >
<?php
$matchyear = 2010;
for ($i=2010; $i <=2510; $i++)
{ ?>
<option value="<?php echo $i;?>" <?php if ($i== $matchyear) echo 'selected="selected"';?>><?php echo $i;?> </option>
<?php }
?>
</select>
Make things easier for you not a complex programming so that other person can understand easily and call you by good names

get selected item from a dropdown list and save it to a php variable

I have a dropdown list(in a php file and without form tag)
print "<select id="animal" onchange="getSelectedItem(this.value)">
<option value = "Dog"> Dog </option>
<option value = "Cat"> Cat </option>
</select>";
and a variable $selAnimal
and a javascript function
function getSelectedItem(opt){
//$selAnimal = opt <- how do i do this?
}
As much as possible I wouldn't like the page to reload so I avoid putting this inside a form and submitting as I select. I can't get to make $.post method work. I know for a fact that this cannot be directly done since php is server side and javascript is client side.
I also thought of putting the selected value inside a hidden component(span or something) and get the value from it. But I have no idea how to get it from the component. I've also seen use of AJAX or jquery but I'm not really knowledgeable enough.
I need to save this value to the php variable since I'll be using it as basis for the options in my second dropdown. For example, if I choose Dog, the dropdown list would have dog breeds as options.
I've spent days looking for possible solutions everywhere. Help would be very much appreciated. Thank you!
here is the solution
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#animal').change(function(){
$.post('loadbreeds.php', { animal:$('#animal').val() },
function(data){
$('#breedList').html(data);
});
});
});
</script>
<form method="post" >
<div id="animalList">
<select id="animal" name="animal">
<option value="">--Select--</option>
<?php if (!empty($animals)) { foreach ($animals as $value) { ?>
<option value="<?php echo $value['animalKey']; ?>"><?php echo $value['animalName']; ?></option>
<?php }} ?>
</select>
</div>
<div id="breedList">
<select id="breed" name="breed">
<option value="">--Select--</option>
</select>
</div>
<input type="submit" value="Submit" />
</form>
code for loadbreeds.php
$animalKey = $_POST['animal'];
$breeds = selectByKey($animalKey); // code for selecting breeds
<select id="breed" name="breed">
<option value="">--Select--</option>
<?php if (!empty($breeds)) { foreach ($breeds as $value) { ?>
<option value="<?php echo $value['breedKey']; ?>"><?php echo $value['breedName']; ?></option>
<?php }} ?>
</select>
You cannot have the variable available to the same file since PHP declares all its variables before the JS even starts. You can however simply redirect the user to a new file where the variable is available.
Use something like this:
function getSelectedItem(opt){
location.href = location.href + "?selAnimal=" + opt;
}
In PHP, now you can use the selAnimal variable to display something different, like this:
$selectedAnimal = $_GET["selAnimal"];
if($selectedAnimal == "dog"){
// Whatever you want to do now
}
A better way would be to use POST with forms, but this should work fine for you as well.
Try the following, to rebuild the second dropdown:
http://jsfiddle.net/kAY7M/59/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
print "<script type=\"text/javascript\">var AnimalTypeList = [\"Dog1,Dog2,Dog3,Dog4\",\"Cat1,Cat2,Cat3,Cat4\"];</script>";
<script type="text/javascript">
$("#animal").change(function () {
var sel = $("#animal").prop("selectedIndex") - 1;
var list = AnimalTypeList[sel].split(",");
var Counter = list.length;
$("#type").children().remove();
for (var i = 0; i < Counter; i++) {
$("#type").append("<option value = '" + list[i] + "'> " + list[i] + "</option>");
}
})
</script>
I could do a pure Javascript only version too, but that is a bit more code.

Categories