I have the template indexSuccess.php with an AJAX function:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#test<?php $mensajes->getIdmensajes() ?>').click(function(){
jQuery('#contenido<?php $mensajes->getIdmensajes() ?>').load(this.href);
return false;
});
});
</script>
<h2>Listado de mensajes emitidos</h2>
<h3>En orden cronológico inverso (más nuevo primero)</h3>
<table id="enviados" width="60%" border="1" cellpadding="8">
<thead>
<tr>
<th>Fecha Creación</th>
<th>Contenido del mensaje</th>
<th>Destinatarios</th>
</tr>
</thead>
<tbody>
<?php foreach ($mensajess as $mensajes): ?>
<tr>
<td width="10%" align="center"> <?php echo date('d/m/Y', strtotime($mensajes->getFechaalta())) ?></td>
<td bgcolor="<?php echo $mensajes->getColores()->getCodigo() ?>"><?php echo $mensajes->getCuerpo() ?></td>
<td width="20%" id="contenido<?php echo $mensajes->getIdmensajes() ?>">
<a id="test<?php echo $mensajes->getIdmensajes() ?>" href="<?php echo url_for('mensajes/receptores?idmensajes='.$mensajes->getIdmensajes()) ?>">Ver receptores</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I want to pass the value $ message-> getIdmensajes () to the AJAX function. I will have a different ID for each row, but this does not work. But the function works well when I set the value. For example: jQuery ('# test24') and jQuery ('# contenido24') works well for the value Idmensajes=24 . How do I pass the value $ message-> getIdmensajes () to AJAX function?
Your question is not so clear but you wrote
jQuery ('#contenido24') works well for the value Idmensajes=24
Also, you have this
jQuery('#test<?php $mensajes->getIdmensajes() ?>').click(function(){
jQuery('#contenido<?php $mensajes->getIdmensajes() ?>').load(this.href);
return false;
});
So, I think you have elements with similar ids, such as contenido24, contenido25 and so on as data container and links with ids like #test24, #test25 an so. If this is the case then you can simply use
jQuery(document).ready(function(){
// Register click handler for all a tags whose id begins with 'test'
jQuery("a[id^='test']").click(function(e){
e.preventDefault(); // instead of return false
jQuery('#contenido'+this.id.match(/\d+/)[0]).load(this.href);
});
});
jQuery('contenido'+this.id.match(/\d+/)[0]) will select elements with id like #contenido24, contenido25 depending on the ID of a, if an a tag has id='test20' then it'll select #contenido20 and load content from ajax call into this element.
Related
Hy,
I want to ban a user when the admin clicks on "bannir" (this will collect the id of the user and put "banned" at 1 in the database)
to print the user informations I'm using a while loop, but when I try to collect the id of the user where in the html tag with the class "idUser", it is always sending the first id and I don't know why..
image of membership area
<div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Prénom</th>
<th>Nom</th>
<th>Email</th>
<th>Admin</th>
<th>Banni</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while($userInfo = $req->fetch()){?>
<tr>
<td class="idUser"><?= $userInfo['id_user'] ?></td>
<td><?= $userInfo['last_name'] ?></td>
<td><?= $userInfo['first_name'] ?></td>
<td><?= $userInfo['email'] ?></td>
<td><?php if($userInfo['admin'] == 1){ ?>
<img src="../img/ok.png">
<?php } else { ?>
<img src="../img/no.png">
<?php } ?></td>
<td><?php if($userInfo['banned'] == 1){ ?>
<strong style="color:#E04949;">OUI</strong>
<?php } else { ?>
<strong style="color:#6AC259;">NON</strong>
<?php } ?></td>
<td>Modifier | <?php if($userInfo["banned"] == 0){ ?> Bannir <?php }else{ ?> Débannir <?php } ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
$('.banMemberJquery').click(function(){
var idUser = $( ".idUser" ).html();
alert('Utilisateur Numero ' + idUser + ' banni avec succès.');
$.ajax({
url:"banMemberRequest.php",
data:'idUser='+idUser,
}).done(function(data){
$('#result').html(data);
});
});
PS : When I click on "bannir", the request in the file "banMemberRequest.php" is working correctly.
Thank's in advance for helping
The issue is because you're selecting all .idUser elements. Calling html() on that will only read the HTML of the first one in that collection.
To fix this you need to use DOM traversal to find only the .idUser element which is related to the .banMemberJquery element which was clicked. To do that you can use closest() and find(), like this:
$('.banMemberJquery').click(function() {
var idUser = $(this).closest('tr').find('.idUser').text(); // note text(), not html()
$.ajax({
url: 'banMemberRequest.php',
data: { idUser: idUser },
}).done(function(data) {
$('#result').html(data);
});
});
When a user hovers the mouse over a table row, to trigger a preview event, only it doesn't react in any way. I think the problem is that the table rows are generated via PHP.
HTML & PHP
<table>
<thead>
<tr>
<th width="200">User</th>
<th width="900">Description</th>
</tr>
</thead>
<tbody>
<?php
require_once './Classes/MainController.php';
$mc = MainController::getInstance();
$threads = $mc->getAllThreads();
foreach ($threads as $thread) {
?>
<tr id='thread_video_preview'>
<td><?php print $thread->original_poster; ?></td>
<td><?php print $thread->description; ?></td>
<td id='see_thread_video_url' class="hide"><?php print $thread->url; ?></td>
</tr>
<?php } ?>
<script type="text/javascript" src="./Javascript/MainFunctions.js"> HoverPreview();
</script>
</tbody>
</table>
Jquery
function HoverPreview() {
$('tr#thread_video_preview').hover(function() {
var url = $(this).$('#see_thread_video_url').html();
$('#dynamicPreview').html('<iframe width="320" height="240" src="//www.youtube.com/embed/' + url + '?autoplay=1" frameborder="0""></iframe>')
});
}
There are several issues here. Firstly, id attributes should be unique. If you want to group elements, use a class. Secondly, the location of your script tag could cause issues. You should place it in the head, or just before </body>. Finally, when you set a src property of a script tag, you cannot place any code in it. You need a separate script tag for that. With all that in mind, try this:
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="./Javascript/MainFunctions.js"></script>
<script type="text/javacript">
$(function() {
$('tr.thread_video_preview').hover(function() {
var url = $(this).find('.see_thread_video_url').html();
$('#dynamicPreview').html('<iframe width="320" height="240" src="//www.youtube.com/embed/' + url + '?autoplay=1" frameborder="0""></iframe>')
});
});
</script>
</head>
In the body:
<?php
require_once './Classes/MainController.php';
$mc = MainController::getInstance();
$threads = $mc->getAllThreads();
foreach ($threads as $thread) { ?>
<tr class='thread_video_preview'>
<td><?php print $thread->original_poster; ?></td>
<td><?php print $thread->description; ?></td>
<td class='see_thread_video_url' class="hide"><?php print $thread->url; ?></td>
</tr>
<?php }
?>
Change this
<tr id='thread_video_preview'>
To :
<tr class='thread_video_preview'>
And in jquery event use
$(".thread_video_preview").
And I think it is good practice to use properties or event listener in td instead of tr
I want to check the table row value on page load..
Table example:
Name || Status || Set
John || Pass || [ ] (checkbox)
Chris || Fail || [ ] (checkbox)
When the status is 'Fail' I want to disable the checkbox..
Now I'm using this jQuery:
<script>
$(document).ready(function() {
if(getElementsByClassName('paket_ava').value=='kosong'))
{
document.getElementById("checkboxx").disabled=true;
}
});
</script>
and this is my PHP table code :
<tr>
<td><?php echo $paket['id_paket'];?></td>
<td><?php echo $paket['nama_paket'];?></td>
<td><?php echo $paket['keterangan_paket'];?></td>
<td><?php echo $paket['harga'];?></td>
<td><img src='<?php echo $paket['gambar_paket']?>' width='120' height='120'></td>
<td class="paket_ava"><?php echo $paket['ketersediaan_paket'];?></td>
// class on the table data
<td><?php echo $paket['status_harian_paket'];?></td>
<td><input type="checkbox" name="chkDel[]" id="checkboxx" class="aku" value="<?=$paket["id_paket"];?>"></td>
<?php
echo ("<td><a href='edit_data_paket.php?id_paket=$paket[id_paket]'>Edit</a></td>");
?>
</tr>
The code above is not working, but if I change to:
if(getElementsByClassId('paket_ava').value=='kosong'))
{
document.getElementById("checkboxx").disabled=true;
}
(of course I change the class in the table into Id)
When the page load its acting strange and the checkbox on the first data is disabled..
How to do this properly?
Try like below.... It will help you...
Fiddle Example: http://jsfiddle.net/68wbx/126/
Suppose your HTML Table was like below:
HTML:
<table id="datapaket" border="1">
<tr>
<th>Name</th><th>Status</th><th>Set</th>
</tr>
<tr>
<td>John</td><td class="paket_ava">Pass</td>
<td><input type="checkbox" name="chkDel[]" id="checkboxx" class="aku" value='sam'/></td>
</tr>
<tr>
<td>Chris</td>
<td class="paket_ava">Fail</td>
<td><input type="checkbox" name="chkDel[]" id="checkboxx" class="aku" value='sam'/></td>
</tr>
</table>
and try the Below Jquery :
$(document).ready(function() {
$('#datapaket tr').each(function() { //Looping Every Table Row
//Get the TD Value that have Classname ".paket_ava"
var str = $(this).find('.paket_ava').html();
if(typeof str !== 'undefined'){
if (str.indexOf("Fail") >= 0)
$(this).find('td:nth-child(3) input:checkbox').attr("disabled", true);
};
});
});
traverse through each element having class 'paket_ava' and do your stuff inside it. like
$('.paket_ava').each(function(i, obj) {
// your stuff...
});
Reference : jQuery to loop through elements with the same class
I would like to ask how can I call Sql query when HTML button is pressed. I watched a couple tutorials, but never got it to work correct, thats why I would like to ask here.
So..I have a list of items displayed, and the list is generated from database. Here is how the code looks like:
<table width="100%" border="1" cellspacing="1" cellpadding="5" style="background-color:#FFC" >
<td>Id</td>
<td>Text</td>
<td>Solved?</td>
<td></td>
<?php
while( $array = mysql_fetch_assoc($queryRun) ) {
?><tr>
<td><?php echo $id = $array['id'].'<br />';?></td>
<td><?php echo $text = $array['text'].'<br />';?></td>
<td><?php echo $reseno = $array['solved'].'<br />';?></td>
<td><input type="button" value="Solve it" /></td>
</tr><?php
}?>
</table>
Now each item that is generated inside this loops has a button "Solved" at the end of the table, and I want to make it so when user click on that button, do a Sql query which changes a value of solved from 0 (false) to 1 (true).
You need ajax. write button onclick and make ajax call.
I've not tested this, but it should work.
Basically I've applied a class to anything to easily select stuff in jQuery:
<?php while ($array = mysql_fetch_assoc($queryRun)) { ?>
<tr class="row">
<td class="cell_id">
<?php echo $array['id']; ?>
</td>
<td class="cell_text">
<?php echo $array['text']; ?>
</td>
<td class="cell_solved">
<?php echo $array['solved']; ?>
</td>
<td class="cell_solve_it">
<input class="button_solve_it" type="button" value="Solve it" />
</td>
</tr>
<?php } ?>
Then I've created this short jQuery script:
$('.row').each(function(){
$('.button_solve_it', this).click(function(e){
e.preventDefault();
$.post(
'solve_query.php',
{
id: $('.cell_id', this).html(),
text: $('.cell_text', this).html()
},
function (data) {
$('.cell_solved', this).html('1');
}
);
});
});
In short:
when you click a .button_solve_it button, you make an AJAX request to solve_query.php, where you perform your SQL query.
Then, it sets the content of the row in Solved? column to 1 (or to true or whatever you want).
i have an table
<table class="oldService">
<thead>
<th>name</th>
<th>age</th>
<th>action</th>
</thead>
<tbody>
<?php foreach($array as $k=>$v){ ?>
<tr>
<td><?php echo $k[name] ?></td>
<td><?php echo $k[age]?></td>
<td id="<?php $k[id]" class="delme">X</td>
</tr>
<?php } ?>
</tbody>
<table>
now i want to delete any row by clicking on X of each row except first and last row,
and also need to confirm before deletion.
i used below jquery
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('table.oldService>tbody tr').not(':first').not(':last').click(function(){
if(confirm('want to delete!')){
jQuery(jQuery(this).addClass('del').fadeTo(400, 0, function() {
jQuery(this).remove()}));
jQuery.get('deleteService.php', {id:jQuery(this).attr('id')});
}
else return false;});
});
</script>
this is working perfect,but it execute by click on that row( means any td), i want that this event only occour when user click on X(third td) .
please suggest me how to modify this jquery so that the event occur on click of X.
UPDATE:
i want that minimum one row should be in tbody,
means if there is only one row then no function to delete that row, and if there is many rows then any row he can delete but not the all rows.
You can do this with a bit less code like this:
jQuery('table.oldService').delegate('td.delme', 'click', function() {
if(jQuery(this).parent().siblings(":not(.del)").length === 0) return;
if(!confirm('want to delete!')) return;
jQuery.get('deleteService.php', { id:this.id });
jQuery(this).parent().addClass('del').fadeTo(400, 0, function() {
jQuery(this).remove();
});
});
This attaches one event handler for the table instead of 1 per <td>. First we check if there are any siblings left that aren't deleted (prevent last deletion). Then check if they confirm, then delete the row.
You also need a few html fixes based on the posted question. The last tag needs to be </table> instead of <table>, and those <td> elements in the <thead> need to be wrapped in <tr></tr>. Let me know if you have any trouble, you can see a working demo of this here.
I didn't implement all your animation and AJAX logic, but here is how you could do this in general:
$(function() {
$('table.oldService td.delme:not(:first):not(:last)').click(function() {
$(this).closest('tr').remove();
});
});
Also, I highly recommend you run your code through JSLint. The code sample you posted has a massive number of errors in it.
Try this :
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('table.oldService>tbody tr img.delete').not(':first').not(':last').click(function () {
if(confirm('want to delete!'))
{
jQuery(jQuery(this).addClass('del').fadeTo(400, 0, function() {
jQuery(this).parent().parent().remove()}));
jQuery.get('deleteService.php', {id:jQuery(this).attr('id')});
}
else return false;});
});
</script>
html :
<table class="oldService">
<thead>
<th>name</th>
<th>age</th>
<th>action</th>
</thead>
<tbody>
<?php foreach($array as $k=>$v){ ?>
<tr>
<td><?php echo $v['name'] ?></td>
<td><?php echo $v['age']?></td>
<td><img id="<?php echo $v['id']?>" class="delete" src="del.gif" /></td>
</tr>
<?php } ?>
</tbody>
<table>