How to select perticular class in jquery from a dynamic table - php

I am trying to create a system to put sequence number of lessons, and i tried to make it unique as u can see that i take a variable $i but no luck . i am new in jquery. dont know how to use (this) in this situation. i am going to use ajax to save the data.
<?php
$chapter_id = $_GET['id'];
$lesson = mysqli_query($conn_course, "SELECT * FROM `lesson_list` WHERE `chapter_id` = '$chapter_id' order by `sequence` asc");
$i=0;
while ($info1 = mysqli_fetch_array($lesson)) {
$lesson_id = $info1['lesson_id'];
$lesson_info = mysqli_query($conn_course, "SELECT * FROM `lesson` WHERE `lesson_id` = '$lesson_id'");
$info = mysqli_fetch_array($lesson_info);
$i++;
?>
<tr>
<td>
<form>
<input type="hidden" class="lesson_id<?=$i?>" value="<?=$lesson_id?>">
<input type="hidden" class="sno" value="<?=$i?>">
<input type="hidden" id="chapter_id" value="<?=$chapter_id?>">
<input class="form-control sequence_no" style="width:27%;" type="number" value="<?= $info1['sequence'] ?>">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
<?= $info['lesson_name'] ?>
</button>
</a>
</td>
<td>
<?= strtoupper($info['code']) ?>
</td>
<td>
<?php if($info['video_url'] == 0){}else{ ?>
<a href="https://player.vimeo.com/video/<?= $info['video_url'] ?>" target="blank"><button type="button"
class="btn btn-outline-light btn-sm">Play Video</button></a>
<?php } ?>
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="<?=$lesson_id?>">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
<?php } ?>
myjquery is
$(document).ready(function () {
$('.sequence_no').on("change", function () {
var sequence_no = $('.sequence_no').val();
var s_no = $('.sno').val();
var chapter_id = $('#chapter_id').val();
var lesson_id = $('.lesson_id' + s_no).val();
alert(s_no);
})
})
alert is showing only the first table row s_no. how to select the lesson id of particular tr

You can simply get closest tr using $(this).closest("tr") and then use .find to get required classes values .
Demo Code :
$('.sequence_no').on("change", function() {
var selector = $(this).closest("tr")
var sequence_no = $(this).val();
var s_no = selector.find('.sno').val();
var chapter_id = selector.find('.chapter_id').val();
var lesson_id = selector.find('input[class*=lesson_id]').val();
console.log(s_no, lesson_id);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table>
<tr>
<td>
<form>
<!--added class-->
<input type="hidden" class="lesson_id<?=$i?>" value="22">
<input type="hidden" class="sno" value="1">
<input type="hidden" class="chapter_id" value="1">
<input class="form-control sequence_no" style="width:27%;" type="number" value="2">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
Smething
</button>
</a>
</td>
<td>
Abc
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="1">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
<tr>
<td>
<form>
<input type="hidden" class="lesson_id<?=$i?>" value="26">
<input type="hidden" class="sno" value="2">
<input type="hidden" class="chapter_id" value="2">
<input class="form-control sequence_no" style="width:27%;" type="number" value="2">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
Smething
</button>
</a>
</td>
<td>
Abc2
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="2">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
</table>

Related

why cant i get value of $accept_request_id

<form method="post>
<button input type="submit" id="click" class="btn btn-info " name="<?php echo$accept_request_id; ?>"></button>
</form>
Why can't I get value of $accept_request_id? I use following code
if(isset($_POST[$accept_request_id])) {
$accept_request_id=$_POST['accept_request_id'];
}
You can use below code to get value of form. This will work
<form method="post>
<input type="hidden" name="accept_request_id" value="<?php echo $accept_request_id ?>" />
<button input type="submit" id="click" class="btn btn-info">Submit
</button>
</form>
if(isset($_POST['accept_request_id'])) {
$accept_request_id = $_POST['accept_request_id'];
}

Here variable 'pass' can't parse to $_post['action']=='edit'

<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="edit" class="btn btn-primary" value="edit"/>
<input type="submit" name="delete" class="btn btn-danger" value="Delete"/>
</div>
</form>
then
if($_POST['action']=='edit'){
echo $rcode1=$_REQUEST['pass'];
$sqlup="select * from transport_details where rcode='$rcode1'";
There is no input named action. Your current code is unreachable.
The $_POST assoc gets its keys by attribute name. E.g.
<input type="text" name="authorName" />
<?php
echo $_POST['authorName'];
?>
Your code should be like (in PHP file)
if (isset($_POST['edit'])) { ... }
OR (in HTML file)
<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="action" class="btn btn-primary" value="edit"/>
<input type="submit" name="action" class="btn btn-danger" value="Delete"/>
</div>
</form>
You can do this in your php...
if (isset($_POST['edit'])) {
echo $rcode1=$_POST['pass'];
$sqlup="select * from transport_details where rcode='".$rcode1."'";
}
assuming your html is like this...
<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="action" class="btn btn-primary" value="edit"/>
<input type="submit" name="action" class="btn btn-danger" value="Delete"/>
</div>
</form>
you should be able to get the value

Request not showing all data in array

I am using laravel 5.1 and I am in the middle of the problem.
<script type="text/javascript">
var counter = 0;
$(document.body).on('click', ".popCategory", function () {
var appendcategory = "";
if (counter < 100) {
counter++;
appendcategory += '<input type="text" class="form-control" name="category[]" placeholder="Category" value="545454" />';
$('.category_append').append(appendcategory);
}
});
</script>
As shown in image I have appended the category with the help of jquery and when I submit the form I get the result as shown in below:
The problem is that I only get one value when I have submitted three value of category . you can see the inspect element below:
<form action="" role="form" method="POST" enctype="multipart/form-data">
<input type="number" class="form-control" name="current_amount" placeholder="Amount" id="current_amount" min="1"/></td>
<td><select name="cash_flow_status" class="form-control">
<option value="cashin">CashIn</option>
<option value="cashout">CashOut</option>
</select></td>
<td class="category_append">
<input type="text" class="form-control" name="category[]" placeholder="Category" value="545454"/>
</td>
<td>
<a href="javascript:void(0)" class="popCategory">
<i class="fa fa-plus"></i></a>
</td>
<td class="item_append"><input type="text" class="form-control" name="items[]" placeholder="Items"/></td>
<td>
<div name="add1" class="btn btn-success cashFlowAddPart" onClick="appendItems()" ><i class="fa fa-plus"></i></div></td>
<td colspan="2">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" name="submit" id="cashflowsubmit" value="Submit" class="btn btn-success"/>
</td>
</form>
Can someone help me?

How can I update all input fields with one button and make a button to delete items individually?

I've been making a shopping cart where I display the products with a delete button for each product and an update button to update all the quantity fields.
The problem is that when I have managed to make update button work the delete button delete the last product not the one that I wanted to.
I know is a problem with the form but i do not know how to resolve it.
$i = 0;
while($row = $result->fetch_assoc()){
//loop product info
echo'<form action="" method="POST">
<input class="inp" type="hidden" name="h_order" value="'.$row['order_id'].'"/>
<input class="inp" type="hidden" name="order[]" value="'.$row['order_id'].'"/>
<input class="inp" style="width:30px;" type="text" name="';
echo 'quantity[$row['order_id']]';
echo 'value="'.$row['quantity'].'" autocomplete="off"/>';
if($i == 0){
echo'<table>';
}
echo '<td>'.$total.'</td>
<td>
<center>
<button class="action red" type="submit" name="delete" title="Delete">
<i class="fa fa-trash-o fa-2x"></i>
</button>
</center>
</td>
</tr>';
$i++;
}
echo '<tr>
<td colspan="6">
<button class="button action blue" style="float:right;" type="submit" name="update" title="Update quantity">
<i class="fa fa-refresh fa-lg"></i>
<a>Update cart</a>
</button>
</td>
</tr>
</table>
</form>';

PHP : How to put and pass variable in modal URL

So, I have a button to give a direct link to modal in same page.
this is the button and url
<a data-toggle="modal"
href="main_user.php?user_id=<?php echo $user['user_id']; ?>#myModal"
class="btn btn-warning">
( I try to echo the $user_id on before #modal ) is it right ?
and after I click the button, the modal will appear.
This is the modal with the form.
<form class="form-login" action="action/doEditUserStatus.php" method="post">
<div class="login-wrap">
<div class="changestatus">
<p>Banning or Activing User Status</p></div>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-success active">
<input type="radio" name="options" value="1" autocomplete="off" checked> Actived
</label>
<label class="btn btn-primary">
<input type="radio" name="options" value="2" autocomplete="off"> Banned
</label>
</div>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
<button class="btn btn-theme" name="modalSubmit" type="submit">Submit</button>
</div>
</form>
And then I try to submit the modal form, but the action cannot read the $user_id which I put before #modal.
UPDATE :
Table code :
So this is my table :
<tr class="success">
<th class="numeric">ID</th>
<th class="numeric">E-mail</th>
<th class="numeric">Name</th>
<th class="numeric">Phone</th>
<th class="numeric">Picture</th>
<th class="numeric">Status</th>
<th colspan="2" class="numeric">Action</th>
</tr>
<tr>
<td class="numeric"><?php echo $user['user_id']; ?></td>
<td class="numeric"><?php echo $user['email']; ?></td>
<td class="numeric"><?php echo $user['name']; ?></td>
<td class="numeric"><?php echo $user['phone']; ?></td>
<td class="numeric"><?php echo $user['picture']; ?></td>
<td class="numeric">
<a data-toggle="modal" href="main_user.php?user_id=<?php echo $user['user_id']; ?>#myModal" class="btn btn-warning">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Change Status
</a>
</td>
</tr>
The main problem is :
When I click the button, then the modal will appear but it can't get the $user_id from that button?
Put a hidden input field with the user_id into your form:
<input type="hidden" name="user_id" value="<?php echo $user['user_id']; ?>">
EDIT: If you mean Bootstrap try this:
Link/Button:
<a data-toggle="modal" data-userid="<?php echo $user['user_id']; ?>"
href="main_user.php#myModal"
class="btn btn-warning">
Hidden Form Field:
<input type="hidden" name="user_id" value="">
Javascript:
$('#myModal').on('show.bs.modal', function(e) {
var userid = $(e.relatedTarget).data('userid');
$(e.currentTarget).find('input[name="user_id"]').val(userid);
});
See also http://getbootstrap.com/javascript/#modals-related-target and Passing data to a bootstrap modal
You should have mentioned Bootstrap in your question. PHP is not the appropriate tag.
This will work if your link will do a redirection :
You will need to add an hidden input within your form with the $_GET['user_id'] variable (that contains the value of the parameter in the url) :
<input type="hidden" name="user_id" value="<?php echo (int)$_GET['user_id'] ?>" />
but, if I'm right, this link is handle by twitter bootstrap that will prevent its default behaviour. So there is no actual redirection. What you can do, is use a custom data attribute in the link :
<a data-toggle="modal"
data-userid="<?php echo $user['user_id']; ?>"
href="#myModal"
class="btn btn-warning">
And handle this with a bit of javascript, to add the hidden input field to the form :
$(document).ready(function(){
$('[data-userid]').click(function(){
var userid = $(this).data('userid');
var form = $('form.form-login');
var input = $('<input type="hidden" name="user_id" value="'+userid+'" />');
if (form.find('input[name="user_id"]').length) {
form.find('input[name="user_id"]').val(userid);
} else {
form.prepend(input);
}
});
});
how about this add the id to the data-target
click me
now in your modal id add this
myModal-<?php echo $user['user_id'];?>
and you can load the data dynamically base on that id

Categories