code:
<script>
$(document).ready(function(){
$(".check").click(function(){
comment = $(".comment2").val();
alert(comment);
});
});
</script>
<?php
$sql = "select * from enquires2";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
echo "<tr>
<td>
<input type='checkbox' class='check' id='chk".$row['id']."' name='chk' value=".$row['id'].">
</td>
<td>
<input type='text' name='comment2' class='comment2' id='comment2_".$row['id']."' value='' />
</td>
</tr>";
}
?>
In this code when I enter text inside comment2 input field and then check checkbox it alert only 1st row value but when I enter second textbox and then again check checkbox it show nothing. So, how can I get multiple textbox value onclick on checkbox ? Please help.
Thank you
Problem is, that both the elements are of class .comment2, which means the $(".comment2") creates an array for you, and the .val() only gets the value of the first element in the array.
You have to use a more unique value (since ID's not always start at 0, a simple counter thingy wont work)
May I suggest
<script>
$(document).ready(function(){
$(".check").on("click", function(){
var $id = $(this).val();
comment = $("#comment2_" + $id).val();
alert(comment);
});
});
</script>
Using $(this).val() to get the id value saved in your checkbox, and concatenating that with the ID comment2_x to get the content of the input box
Related
How can I get the value of the first input field from a list?
<button id="hello">get data</button><br><br>
<table> <td><label for="voucher_<?php echo $counter; ?>">D/N Number <?php echo $counter; ?></label><br>
<input type="text" name="voucher_<?php echo $counter; ?>" class="input-field exsmall-input" value="<?php echo $voucher; ?>" placeholder="D/N Number" id="voucher_<?php echo $counter; ?>" required>
</td></table>
When I run the above code the number of rows varies depending on the counter.If I want to get the value of the first DN number ,what shall I do ?
$(document).ready(function(){
$('#hello').click(function(){
var name = $("#voucher").val();
alert(name);
});
});
When I run this javascript I get undefined vales.
The reason you are getting the error is because you have used wrong selector to target input element. input element do not have id voucher. You need to put the ID to make existing code work.
Or, You can use attribute starts with selector here from current HTML:
$('#hello').click(function(){
var name = $("input[name^='voucher_']").val();
alert(name);
});
You can do it like below:-
$(document).ready(function(){
$('#hello').click(function(){
var name = $("div[id^='voucher']:visible:first").val();
alert(name);
});
});
Or:-
$(document).ready(function(){
$('#hello').click(function(){
var name = $("input[type=text]:visible:first").val();
alert(name);
});
});
Try this:-
$(document).ready(function() {
$('#hello').click(function() {
var name = $('table').find('input[type=text]').filter(':visible:first')
.val();
});
});
I have a table where i dynamically add rows with input fields based on the user requirement. These input fields are stored as an array so that looping over them can get the values in each row to insert into the db. The issue is that only the first "static" row from the table is recognized in the array. Every other dynamic input field is not stored in the input array when submitted. A snippet of the table code is
<tr><td><input type=number name=minimum[] required></input></td><td><input type=number name=maximum[] required></input></td> <td><input type=number name=overall[] required></input>
<td><?php echo $formular;?></td></tr>
</tbody><tfoot><tr><td colspan="25"><div class="text-center">
<button type="submit" class="btn btn-success">Submit</button>
</div></td></tr></tfoot></table></div></form>
The jquery code to add an extra row is:
<script>
$(window).load(function(){
$(function () {
var rowv="<tr><td><input type=number name=minimum[] required></input></td>
<td><input type=number name=maximum[] required></input></td><td><input
type=number name=overall[] required></input></td><td><?php echo $formular;?>
</td></tr>";
$("#addRow").click(function () {
var row = $(rowv);
$("#mt > tbody").append(row);
});
});
});
</script>
The (abbreviated) php code to process the array is :
foreach($_POST['fi'] as $key=>$value){
$sql="insert into band(fi) values ('$value')";
}
Only the first row is being inserted. Any row that added dynamically is not submitted to the post array. How can i resolve this?
Create your input field like this and then append it to the form and then try.
var input = document.createElement("input");
input.type = "text";
input.name = "minimum[]";
input.required = "true";
$("#testForm").append(input);
Your jQuery syntax seems incorrect. Change that click function to this:
$("#addRow").click(function () {
$("#mt > tbody").append(
"<tr>" +
"<td><input type='number' name='minimum[]' required></td>" +
"<td><input type='number' name='maximum[]' required></td>" +
"<td><input type='number' name='overall[]' required></td>" +
"<td><?php echo $formular;?></td>" +
"</tr>"
);
});
I need help. I am experimenting with J QUERY for the first time. I have a mouseover function to get and display data from the database based on the row id. However, I am getting the same value for all the rows. Thanks!
while($stmt->fetch()){?>
<td class="other">
<input type="hidden" class="rowid" value="<?php echo $id ?>"/>
<?php echo round($other,2); ?>
</td>
<?php
}
?>
//jquery code:
$(document).ready(function(){
$(".other ").mouseover(function(){
var rowid = $('#rowid').val();
$.get('other.php',{postrowid:rowid},
function(data)
{
$('#otherResult').html(data);
$('#otherResult').show();
$(".other").mouseout(function(){
$('#otherResult').hide();
});
});
});
// Change:
var rowid = $('#rowid').val();
// To:
var rowid = $('input', this).val();
Sidenote: Instead of using a hidden field, you can add data to related tags using HTML5 data-* attribute:
<td class="other" data-id="<?php echo $id ?>">
<?php echo round($other,2); ?>
</td>
I am working in wordpress and I have created a custom plugin.In which I have get multiple data from the database and my code is like this.
<?php
foreach($result as $res)
{
?>
<input type="hidden" class="status" value="<?php echo $res->review_status; ?>" />
<button class="aprove" value="<?php echo $res->review_id; ?>">Aprove</button>
<?php
}
?>
Now, I want to get hidden field value in jQuery. My jQuery code is like this:
jQuery(".aprove").click(function(){
var status = jQuery('.status').val();
alert(status);
});
When I click on button then it shows only first value of hidden field. For instance, the fist hidden value is 1 and second value is 0 then it display only fist value 1 for both button. So what shold I have to do to get different hidden value?
Try :
jQuery(".aprove").click(function(){
jQuery('.status').each(function(){
var status = jQuery(this).val();
alert(status);
});
});
.each will loop through all the classes and it will give alert every value of it.
JS Fiddel Demo
Updated
Updated Demo
Here is your answer. for each button the value taken would be from the input element before the button element
jQuery(".aprove").click(function(){
var status = jQuery(this).prev('.status').val();
alert(status);
});
var status = jQuery('.status').val();
alert(status);
this will get the value of element first found on page and will return the result,
if you want all the input values use
jquery each() - https://api.jquery.com/jquery.each/
jQuery('.status').each(function(){
alert($(this).val());
});
// this will give you all the values you want, one by one
you can use .map like this to get what you want:
$(document).ready(function(){
var status=[]
jQuery(".aprove").click(function(){
status = $(".status").map(function() {
return $(this).val();
}).get();
alert(status);//array of values
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" class="status" value="0" />
<input type="hidden" class="status" value="1" />
<button class="aprove" value="">Aprove</button>
I dont understand why this code doesnt work :
$( "#remove" ).click(function() {
var getcontent = $("#noticeid").val();
$.get("admincp.php", { removeid : getcontent } ,function(){
});
});
Heres the html textbox :
<td><input class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
And the button
<input type='button' id='remove' value='remove' class='btn btn-danger'>
And heres the PHP (on the same page):
if($_GET['removeid']){
$removeid = $_GET['removeid'];
$querydelete = "DELETE FROM `$dbtable`.`Notices` WHERE `NoticeID` = $removeid";
mysqli_query($conn,$querydelete);
}
This worked with just simple PHP and HTML so it must be something to do with the script. Sorry if its a really simple mistake but I cant seem to fix it.
Edit:
I was putting the input in a if clause that wasnt working so I fixed that but it still doesnt work, here is the new HTML code ($info7 = notice id ):
<input id='noticeid' class='form-control' style='position:absolute;left:500%;' type='text' name='myname' value='$info7'>
You are trying to get a dom element by ID, but only name is set in your html:
<td><input class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
You should add id, e.g.:
<td><input id='noticeid' class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
And then you will be able to successfully do:
var getcontent = $("#noticeid").val();
Try getting the value by name as you don't have an id in your input type
$( "#remove" ).click(function() {
var getcontent = $("input[name='noticeid']").val();
$.get("admincp.php", { removeid : getcontent } ,function(){
});
});
You are trying to retrieve
$("#noticeid").val();
But there is no element with this Id in the DOM. Please add id attribute.
<input class='form-control' id="noticeId" style='position:absolute;left:500%;' type='text' name="noticeid" value='$editid'>
If you don't want to add id then you can do like this
var getContent=$("input[name='noticeId']").val();
else
var getcontent=document.getElementByTagName("noticeId").value;
else
var getcontent=document.querySelector("input[name='noticeId']").value;