I have a button into a while loop on a PHP file.
The button is associated to a PHP value that is changing with mysqli_fetch_array.
I load only 3 values in my mysqli_fetch_array.
My ajax function has data in it, my problem is that it is only accepting first row's value, and not other rows value. In fact, the function is ok just at the first row of the mysqli_fetch_array, and it seems that my code is not working on the other rows, like if PHP loop stopped at first row.
Here is my javscript code (it is declared on the same page of my loop, after the while):
<script type="text/javascript">
$(document).ready(function() {
$("#up").click(function() {
var varno_commentaire = $("#no_commentaire").val();
$.ajax({
url: "upvoteCommentaire.php",
type: "POST",
data: {
no_commentaire: varno_commentaire
},
async: false,
success: function(result) {
alert(result);
}
});
});
});
</script>
And here is my PHP loop :
<?php
while($comm = mysqli_fetch_array($res2))
{
echo
'<label class="text-muted">'.$comm['desc_commentaire'].'</label>'.
'<p>'.
''.'#'.$comm['login'].''.
'<p>'.
'<label>'.$comm['vote_commentaire'].' points'.'</label>'.
'<p>'.
'<input type="text" value="'.$comm['no_commentaire'].'" id="no_commentaire"/>'.
'<button class="btn btn-default" name="up" id="up">+</button>'.
' '.
'<button class="btn btn-default" name="down" id="bold" onclick="downvote()">-</button>'.
'<p>'.
'<hr/>';
}
?>
I hope it will be easy to understand. Any help will be appreciated, I've been stuck on it for a moment. Thanks a lot.
First of all, you can't declare more than once an id. ID should be unique. You can pass the ID into a HTML attribute.
Your loop:
<?php
while($comm = mysqli_fetch_array($res2))
{
echo
'<label class="text-muted">'.$comm['desc_commentaire'].'</label>'.
'<p>'.
''.'#'.$comm['login'].''.
'<p>'.
'<label>'.$comm['vote_commentaire'].' points'.'</label>'.
'<p>'.
'<input type="text" value="'.$comm['no_commentaire'].'"/>'.
'<button class="btn btn-default btn-up" data-id="'.$comm['no_commentaire'].'">+</button>'.
' '.
'<button class="btn btn-default btn-down" data-id="'.$comm['no_commentaire'].'">-</button>'.
'<p>'.
'<hr/>';
}
?>
Your script:
<script type="text/javascript">
$(document).ready(function() {
$("button.btn-up").click(function() {
var varno_commentaire = $(this).data('id');
$.ajax({
url: "upvoteCommentaire.php",
type: "POST",
data: {
no_commentaire: varno_commentaire
},
async: false,
success: function(result) {
alert(result);
}
});
});
});
</script>
ID are meant to be used only once on the entire page. In your case every element in while loop of PHP has same ID. So when you take the value of element by $("#no_commentaire").val(), it takes the first element's value ignoring the rest of the elements on the page.
Solution to your problem is to have a class instead of ID of the elements and access the same by parent child releationship in the jQuery. So you only get the value of selected/clicked section.
Your PHP code would look something like this,
<?php
while($comm = mysqli_fetch_array($res2))
{
echo
'<div class="parent-class">'.
'<label class="text-muted">'.$comm['desc_commentaire'].'</label>'.
'<p>'.
''.'#'.$comm['login'].''.
'<p>'.
'<label>'.$comm['vote_commentaire'].' points'.'</label>'.
'<p>'.
'<input type="text" value="'.$comm['no_commentaire'].'" class="no_commentaire"/>'.
'<button class="btn btn-default up" name="up">+</button>'.
' '.
'<button class="btn btn-default" name="down" id="bold" onclick="downvote()">-</button>'.
'<p>'.
'</div>'.
'<hr/>';
}
?>
and jQuery code would look something like this,
<script type="text/javascript">
$(document).ready(function() {
$(".up").click(function() {
var varno_commentaire = $(this).parent().child(".no_commentaire").val();
$.ajax({
url: "upvoteCommentaire.php",
type: "POST",
data: {
no_commentaire: varno_commentaire
},
async: false,
success: function(result) {
alert(result);
}
});
});
});
</script>
Related
I want to display pdf icon only when the field (WO_doc) is not empty. I don't know what I am doing wrong while writing the if statement. Please help.
<script type="text/javascript">
$( "select[name='PEF_id']").change(function () {
var Pefid = $(this).val();
if(Pefid) {
$.ajax({
url: "<?php echo site_url('Admin/showwo');?>",
dataType: 'Json',
data: {'PEF_id': Pefid},
success: function(response){
$(".displayNone").removeClass("displayNone").addClass("displayHidden");
$.each(response.wo,function(key, value) {
$('.wodata').append('<tr>\
<td>'+value['WO_no']+'</td>\
<td>'+value['WO_date']+'</td>\
<td>'+value['Agency_name']+'</td>\
<td>'+value['WO_amount']+'</td>\
<?php if("?>'+value['WO_doc']+'<?php" != null) { ?>\ ***this if condition is not working***
<td></i></td> <?php } else { ?> <td></td> <?php } ?>\
<td><button type="submit" name="submit" class="btn btn-success">Update</button></td>\
<td><button type="submit" name="submit" value="'+value['WO_no']+'" class="button-del btn btn-danger">Close this WO</button></td>\
</tr>');
});
}
});
}
});
</script>
I'm making an click event in which I send the id of the employee. I can see the value using console.log(); the employee id is getting shown, but I keep getting an index :id error.
<input type="button" class="btn btn-primary" value="Appointment Letter" onclick="downloadempappointmentletter('<?php echo $data->employee_id;?>')">
function downloadempappointmentletter(id) {
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { id: id },
success: function (data) {
console.log(data);
}
});
}
public function downloadappointmentletter()
{
$empid = $_POST['id'];
}
I keep getting error:
undefined index :id
However when I echo $data->employee_id; it shows 1 etc. What am I missing?
You just comment $empid = $_POST['id']; this line and var_dump($_POST); it will shows all $_POST values from ajax
look through browser Network tab ,check ajax post id or not after that we can follow up the problem
try this
<input type="button" class="btn btn-primary" value="Appointment Letter" onclick="downloadempappointmentletter('<= $data->employee_id ?>')">
<input type="button" class="mybutton btn btn-primary" value="Appointment Letter"
data-value="<?php echo $data->employee_id; ?>">
now
$(document).ready(function() {
$(".mybutton ").click(function(){
let id = $(this).attr('data-value');
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { id: id },
success: function (data) {
console.log(data);
}
});
});
});
function downloadempappointmentletter(id) {
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { "id": id },
success: function (data) {
console.log(data);
}
});
}
Try this. I think you forgot ("") when sending data
I fixed it instead of an onclick event ,i made it work through calling it in <a href="">
function downloadempappointmentletter(id) {
var ID = id;
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { id: ID },
success: function (data) {
console.log(data);
}
});
}
I am able to get the value of data id in the second script, However, the code is not able to retrieve the value in the first script.
$(function() {
$(document).on('click', '.upload', function(e) {
e.preventDefault();
$('#uploaddocument').modal('show');
var value = $(this).data('id');
$.ajax({
type: 'POST',
url: 'users_upload.php',
data: {
id: value
},
dataType: 'json',
success: function(response) {
$('.userid').val(response.value);
}
});
});
});
Above script is not working while below one is working
$(function() {
$(document).on('click', '.transact', function(e) {
e.preventDefault();
$('#transaction').modal('show');
var id = $(this).data('id');
$.ajax({
type: 'POST',
url: 'transact.php',
data: {
id: id
},
dataType: 'json',
success: function(response) {
$('#date').html(response.date);
$('#transid').html(response.transaction);
$('#detail').prepend(response.list);
$('#total').html(response.total);
}
});
});
$("#transaction").on("hidden.bs.modal", function() {
$('.prepend_items').remove();
});
});
HTML BUTTONS IS BELOW
<button type="button" class="btn btn-info btn-sm btn-flat upload" data-value="".$row["id"].""><i class="fa fa-upload"></i> Upload</button>
I think the problem is in the button's html line where you are trying to concatenating the value for data element from your php variable.
Try changing it to use single quote for the value and wrap your php variable into curly braces like this
<button type="button" class="btn btn-info btn-sm btn-flat upload" data-value='{$row["id"]}'><i class="fa fa-upload"></i> Upload</button>
Edit:
Just realized, you are getting wrong data element in the first script part. It should be like
var value = $(this).data('value');
here i want to passing the product_id value into addcart.php file through the jQuery Function.here 'Add Cart' button generated dynamically by php while loop. every button set as a invidual product id, there is fine until this..
what's my problem is All Button pass the same value to addcart.php file when i clicked each one...i want to pass the value of individual one(button)..
Please Give me a Solution... addcart.php just contain code like
"echo $_POST['pid']; only"
<?php
include('includes/db-config.php');
$shop_id=$_GET['sid'];
$get_product_q = mysql_query("select product from shops where sid = $shop_id");
$get_product_f = mysql_fetch_array($get_product_q);
$product_id = explode(',', $get_product_f['product']);
$count_product_id = count($product_id);
for($i=0;$i<$count_product_id;$i++){
$get_product_q = mysql_query("select * from products where pid = $i");
$get_product_f = mysql_fetch_array($get_product_q);
$product_id = $get_product_f['pid'];
?>
<script>
$(document).ready(function(){
$('.but').click(function(){
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : '<?php echo $product_id?>'},
success:function(result){
$("#div1").html(result);
}});
});
});
</script>
<?php
$product_name = $get_product_f['name'];
$product_description = $get_product_f['description'];
$product_price = $get_product_f['price'];
echo $product_name;
echo '<br>';
echo $product_description;
echo '<br>';
echo $product_price;
echo '<br>';
echo '<input class="but" type="button" value="Add Cart" />';
echo '<br><br>';
}
?>
<div id="div1"></div>
You are repeating the entire Javascript for every product. Move the Javascript out of the loop and set the product ID as a data-productid attribute on the button:
echo '<input class="but" type="button" value="Add Cart" data-productid="' . htmlspecialchars($product_id) . '" />';
Then when you make the ajax call (on click), read the product ID and set it as the POST data:
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : $(this).data('productid') },
success:function(result){
$("#div1").html(result);
}
});
Try this :
replace : echo '<input class="but" type="button" value="Add Cart" />'; by this : echo '<input class="but" type="button" rel="'.htmlspecialchars($product_id).'" value="Add Cart" />';
and your script :
<script>
$(document).ready(function(){
$('.but').click(function(){
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : $(this).attr('rel'),
success:function(result){
$("#div1").html(result);
}});
});
});
</script>
The problem is that you're binding it to a click on all elements with the class ".but", if you on the other hand gave the button an id like
echo '<input data-pid="'. sha1($product_id) . '" class="but" type="button" value="Add Cart" />';
and then in your jquery function would do the following (outside the loop):
<script>
$(document).ready(function(){
$('.but').click(function(){
$.ajax({
type: 'POST',
url:"addcart.php",
data : $(this).data(),
success:function(result){
$("#div1").html(result);
}
});
});
});
</script>
Then you would get around the problem, as it then listens for the click event on each element, but the data is varying on each element.
I hope this helps.
in you loop ,.but was bind an function each time,so the last bind one works .try to give the .but elements an unique id such as "id=$product_id";
and you can bind the function once (outside the loop).this simply like:
$(document).ready(function(){
$('.but').click(function(){
var data = $(this).attr("id");
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : data},
success:function(result){
$("#div1").html(result);
}});
});
});
be lucky.
<?php $count = 1; foreach ($settings[$slug] as $slug): ?>
<div data-toggle="buttons-radio" class="btn-group">
<button id="enable" value="1" class="btn btn-success active" type="button" name="includeicon"><i class="icon-ok icon-white"></i></button>
<button id="disable" value="0" class="btn" type="button" name="includeicon"><i class="icon-remove"></i></button>
</div>
<?phpendforeach?>
$(document).ready(function(){
$("#enable").click(function(event) {
var slugid = $('#slugid').val();
$.ajax({
url: SITE_URL + 'admin/PluginConf/enable/?'+slugid,
type: "POST",
data : {'slugid' : slugid},
success: function(data) {
}
});
});
$("#disable").click(function(event) {
var slugid = $('#slugid').val();
$.ajax({
url: SITE_URL + 'admin/PluginConf/disable/?'+slugid,
type: "POST",
data : {'slugid' : slugid},
success: function(data) {
}
});
});
});
I am passing button id in jquery, but ajax is getting called only for first button. For second and others it is not getting called, can anyone help me out in this?
Thanks
You have an id of enable and disable in a foreach. If the loop ever executes more than once you will then have multiple buttons with the same id. IDs have to be unique! Rather use something else to identify the click:
I suggest adding an enable class to the enable button and a disable class to the disable button, then catch the events like this:
$('.enable').click(function() {
// code in here
});
$('.disable').click(function() {
// code in here
});
Don't bind the click event(s) for multiple items by id! Ids have to be unique idetifiers.
<?php $count = 1; foreach ($settings[$slug] as $slug): ?>
<div data-toggle="buttons-radio" class="btn-group">
<button value="1" class="enable btn btn-success active" type="button" name="includeicon"><i class="icon-ok icon-white"></i></button>
<button value="0" class="btn disable" type="button" name="includeicon"><i class="icon-remove"></i></button>
</div>
<?phpendforeach?>
$(document).ready(function(){
$(".enable").click(function(event) {
var slugid = $('#slugid').val();
$.ajax({
url: SITE_URL + 'admin/xervPluginConf/enable/?'+slugid,
type: "POST",
data : {'slugid' : slugid},
success: function(data) {
}
});
});
$(".disable").click(function(event) {
var slugid = $('#slugid').val();
$.ajax({
url: SITE_URL + 'admin/xervPluginConf/disable/?'+slugid,
type: "POST",
data : {'slugid' : slugid},
success: function(data) {
}
});
});
});
You are using id what is not correct when several elements in your case buttons have the same id.When you use foreach loop you create multiple buttons with the same particular id. ID means that something is unique, and only one element can have that specyfic id. I assume you should make a class for this buttons and change your script code with having this in mind. Here what I suggest. Hope it helps.
<?php $count = 1; foreach ($settings[$slug] as $slug): ?>
<div data-toggle="buttons-radio" class="btn-group">
<button value="1" class="btn btn-success active enable" type="button" name="includeicon"><i class="icon-ok icon-white"></i></button>
<button value="0" class="btn disable" type="button" name="includeicon"><i class="icon-remove"></i></button>
</div>
<?php endforeach; ?>
$(document).ready(function(){
$(".enable").click(function(event) {
var slugid = $('#slugid').val();
$.ajax({
url: SITE_URL + 'admin/xervPluginConf/enable/?'+slugid,
type: "POST",
data : {'slugid' : slugid},
success: function(data) {
}
});
});
$(".disable").click(function(event) {
var slugid = $('#slugid').val();
$.ajax({
url: SITE_URL + 'admin/xervPluginConf/disable/?'+slugid,
type: "POST",
data : {'slugid' : slugid},
success: function(data) {
}
});
});
});