I have a foreach loop in php like below.
<?php
$i = 0;
foreach ($query as $value) { ?>
<button id="show[<?php echo $i;?>]" class="btn btn-success" type="button">View</button>
<div id="blah[<?php echo $i;?>]">Joel</div>
<?php
$i++
} ?>
Now this loop work fine and I am getting id for each buttons and divs with unique id. But I want to add a jquery click function like below.
$("#show").click(function(){
$("blah").hide();
});
But since it is inside a loop and have different id's with them how to add this jquery function for each buttons?
$(".btn").click(function(){
var id = $(this).attr('id');
var n = id.replace("show",'');
$("#blah"+n).hide();
});
Also replace your buttons with this code
<button id="show-<?php echo $i;?>" class="btn btn-success" type="button">Clear</button>
<div id="blah-<?php echo $i;?>">Joel</div>
for php:
<?php foreach ($query as $key => $value) {?>
<button class="btn btn-success blah-toggler" type="button" data-target="blah-<?php echo $key ?>">
<div id="blah-<?php echo $key ?>">Joel</div>
<?php }?>
then in jquery:
$(".blah-toggler").on("click", function(){
var t = $(this);
$('#' + t.data('target')).hide();
});
Use a class
<button id="show[<?php echo $i;?>]" class="btn btn-success mybutton" type="button">
Then
$(".mybutton").click(function(){
$("blah").hide();
});
Alternatively you could also use a class. Then the usual:
<?php $i = 0; ?>
<?php foreach ($query as $value): ?>
<button class="btn btn-success toggle" type="button">
<div id="blah[<?php echo $i;?>]">Joel</div>
</button> <!-- missed a closing button tag -->
<?php $i++; ?>
<?php endforeach; ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.toggle').on('click', function(){
$(this).children('div').hide();
});
});
</script>
u should use following
PHP:
<?php
$i = 0;
foreach ($query as $value) { ?>
<button id="show[<?php echo $i;?>]" class="btn btn-success clickEvent" type="button">
<div id="blah[<?php echo $i;?>]">Joel</div>
<?php
$i++
} ?>
Here you are adding class="clickEvent" for the buttons . And At the end u will have jScript file with following content .which will take care what should happen , when the button with that class is clicked.
jQuery:
$(".clickEvent").click(function(){
$(this).next('div').hide();
});
PHP:
<?php
$i = 0;
foreach ($query as $value) { ?>
<button id="show_<?php echo $i;?>" onclick=show(this.id) class="btn btn-success" type="button">
<div id="blah_<?php echo $i;?>">Joel</div>
<?php
$i++
} ?>
javascript:
function show(id)
{
var arr = id.split('_');
$("#blah_"+arr[1]).hide("slow");
}
Related
I have the problem that I print the DIV but the content of the foreach is empty and I print NaN, I started to move the accents and quotation marks and now I throw error of a token <. I have been fighting with this for 2 hours and I can't figure out how to make it work.
function activateButtonSend(action){
var addButton = "";
var php = "";
if (action === true) {
php += #php
App\Models\Activity::getOptionsForMail()->map(function($name,$id){
echo "<a class='dropdown-item btn-act-".$id."' href='javascript:sendComments(".$id.");'>".$name."</a>";
});
#endphp
addButton = "<div class='f-flex float-right' id='dropdown-activities'>\
<button id='content-message' style='display: none' type='button' class='btn btn-lg iq-bg-primary-abs dropdown-toggle'\
data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>\
Guardar\
</button>\
<div class='dropdown-menu' aria-labelledby='btnGroupDrop1'>'"+php+"'</div>\
</div>";
}else{
addButton = '<button type="submit" id="content-message" style="display: none" class="btn iq-bg-primary-abs btn-lg float-right action-button-submit">Guardar</button>';
}
$('#type_buttons').html(addButton);
}
Message error console
It prints the results but throws an error because it does not take the tilde to join it inside the php variable of jquery.
#php is the abbreviation of <?php ?>
You're generating JavaScript code, but you're not putting quotes around the JS string. The best way to turn a PHP value into a JavaScript literal is to call json_encode() when echoing.
function activateButtonSend(action){
var addButton = "";
var php = "";
if (action === true) {
php += #php
App\Models\Activity::getOptionsForMail()->map(function($name,$id){
echo json_encode("<a class='dropdown-item btn-act-".$id."' href='javascript:sendComments(".$id.");'>".$name."</a>");
});
#endphp
addButton = "<div class='f-flex float-right' id='dropdown-activities'>\
<button id='content-message' style='display: none' type='button' class='btn btn-lg iq-bg-primary-abs dropdown-toggle'\
data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>\
Guardar\
</button>\
<div class='dropdown-menu' aria-labelledby='btnGroupDrop1'>'"+php+"'</div>\
</div>";
}else{
addButton = '<button type="submit" id="content-message" style="display: none" class="btn iq-bg-primary-abs btn-lg float-right action-button-submit">Guardar</button>';
}
$('#type_buttons').html(addButton);
}
i am new to ajax i am trying to build a website for movies so i need ajax to change the servers for the video but i can not do it how much i tried the div that i want to show is not being shown at all here is my codes
HTML
<?php
$get = $data->show(" SELECT * FROM servers ");
foreach ($get as $row) {
$id=$row['server_id'];
$name=$row['server_name'];
$link=$row['link'];
$movie=$row['film_name'];
?>
<button type="button" id="btn" class="btn btn-warning"><?php echo "$name"; ?></button>
<input type="hidden" id="serverid" value="<?php echo $id ?>">
<?php } ?>
<div id="show">
</div>
AJAX
<script type="text/javascript">
$(document).ready(function (){
$('#btn').click(function (){
var serverid = $('#serverid').val()
$.ajax({
url:"../../control/operation/index/view_movie.php",
method:"POST",
data:{serverid:serverid},
success:function(data)
{
$("#show").html(data);
}
});
});
});
</script>
view_movie.php
if (isset($_POST['serverid'])) {
$id=$_POST['serverid'];
$getuser = $data->getdata(" SELECT * FROM servers WHERE server_id='$id' ");
$link=$getuser['link'];
} ?>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="<?php echo $link ?>" allowfullscreen></iframe>
</div>
</div>
nothing is shown on the div that called show
Hi I'm new in Jquery and i want once the user selects an item I show a table contains information that the user could modify.I want that the table will automatically update when he clicks on "modifier" button.
I get tables using php and I update the div contents using Jquery :
$("#upd_pr").html(data);
That work perfectly however once i click on the button it does not function?
It seems to me like if doing $("#upd_pr").html(data); lets the button does not function but i do not know how to solve this problem can someone help please.
Here is my php file:
//That's what i put after with Jquery on the appropriate div using $("#upd_pr").html(data);:
<?php
require_once("C:/wamp/www/Mini_Prj/controllers/mainController.php");
if((isset($_POST['abrv1']))){
$abrv1=$_POST['abrv1'];
$bqs1=MainController::getTarifBqCompare($abrv1);
$i=0;
echo '<h3>Gestion et tenue de compte:<h3>';
$bq1=$bqs1->fetch();
fillHead($bq1,"prest1");
fillTable($bq1,$i);
$i++;
while ( ($bq1=$bqs1->fetch()) )
{
if (strstr($bq1['type'],"Monétique"))
break;
else
{
fillTable($bq1,$i);
$i++;
}
}
echo '</tbody >';
echo '</table >';
echo '<h3>Opération de paiement:<h3>';
fillHead($bq1,"prest2");
$i=0;
fillTable($bq1,$i);
$i++;
while ( ($bq1=$bqs1->fetch()) )
{
if (strstr($bq1['type'],"Opération de paiement"))
break;
else
{
fillTable($bq1,$i);
$i++;
}
}
echo '</tbody >';
echo '</table >';
echo '<h3>Opération de paiemen</h3>';
fillHead($bq1,"prest3");
$i=0;
fillTable($bq1,$i);
$i++;
while ( ($bq1=$bqs1->fetch()) )
{
fillTable($bq1,$i);
$i++;
}
echo '</tbody >';
echo '</table >';
}
function fillHead($bq1,$idtab)
{
echo"
<table class='tableau table-bordered' id='$idtab'>
<thead>
<tr>
<th style='display:none'>idbq</th>
<th style='display:none'>idp</th>
<th> Prestation</th>
<th>Tarif</th>
<th>Option(gratuit,An,ttc..)<th>
<thModifier<th>
</tr>
</thead>
<tbody >";
}
function fillTable($bq1,$i)
{
echo"
<tr>
<td style='display:none'>".$bq1['idb']."</td>
<td style='display:none'>".$bq1['idp']."</td>
<td>".$bq1['nomp']."</td>
<td contenteditable='true'>".$bq1['tarif']."</td>
<td contenteditable='true'>".$bq1['opt']."</td>
<td> <button id='modifpr$i' class='btn btn-info modif_prst' name='button'>Modifier</button></td>
</tr>
";
}
And Here is the jquery file that allows put those tables on the "upd_pr" div:
//This works perfectly
$("#afichprst").click(function () {
if(abr1===undefined)
{
alert("Choisir une banque d'abord");
}
else {
$.post(basUrl+'views/component/prest_bq.php',
{
abrv1:abr1,
}, function(data) {
$("#upd_pr").html(data);
});
}
});
//Here is the problem : once I click on the button it does nothing:
$(".modif_prst").click(function () {
$choix=confirm("voulez vous vraiment sauvegarder les modifications");
if ($choix)
{
var idbn=($(this).parent().parent().find('td:eq(0)').html());// the value in the 1st column.
var idprs=($(this).parent().parent().find('td:eq(1)').html());
var trf=($(this).parent().parent().find('td:eq(3)').html());
var opttr=($(this).parent().parent().find('td:eq(4)').html());
console.log(idbn);
console.log(idprs);
console.log(trf);
console.log(opttr);
}
});
And here is the generated html :
<h3>Modifier les prestations d'une banque: </h3>
<div class="updTarif">
<form method="post">
<div class="input-group">
<div class="input-group-btn">
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span id="modifprst">Choisir une banque</span><span class="caret"></span></button>
<ul id="udateprest" class="dropdown-menu">
<?php
$rows=MainController:: getBanque();
while ($row =$rows->fetch())
{
if($row['supr']!=1)
echo '<li><a>'.$row['abrvb'].'</a></li>';
}
?>
</ul>
</div>
</div>
<input type="button" id="afichprst" class="btn btn-info" name="filter" value="Afficher">
</div>
</form>
<!--here is where i put the tables-->
<div id="upd_pr">
</div>
</div>
You are overwriting the element's thus the event handler is not working.
Using .on() method with Event Delegation approach, when generating elements dynamically or replacing them.
Use
$("#upd_pr").on('click', ".modif_prst", function(){
//Rest of your code
});
I want to display a time in place of button when button clicked then
button fadein and time show in place of button but my script not
working button fadein only and time didn't show in place of button and
"Time shown when i refresh the whole page but not a particular part
refreshing."
foreach($leads as $val):?>
<tr><td class = 'account-name sort-value'>
<span style='float:left;width:47%;'>
<?php echo $val->first_name .' '. $val->last_name;?></span>
<?php
if($val->checkin_time != '') {?>
<span class = 'Dischktime' id = 'dischktime{<?php echo $val->lead_id;?>}' ><?php echo $val->checkin_time;?>
</span>
<?php
}
else {?>
<span style='float:left;width:26%;'>
<button id ='checkin{<?php echo $val->lead_id;?>}' class='btn btn-default' name='checkinbt' value='<?php echo$val->lead_id; type='button'>CheckIn
</button>
</span>
<?php }
?>
<span>
<?php echo $_SESSION['drivername'];?>
</span>
</td></tr>
<?php endforeach;
}
?>
<script type="text/javascript">
$(document).ready(function() {
$("[id=checkin]").on("click" , function() {
//$(this).fadeOut();
$(this).fadeOut('slow', function() {
//alert("it's working");
$("#dischktime").show();-------(Not working)
});
$('button').submit(function(e) {
e.preventDefault();
return false;
});
//alert($(this).attr('value'));
var lead_id = $(this).attr('value');
//alert(lead_id);
$.ajax({
method: "POST",
url: 'search_db_test.php',
data: 'checkinbt=' + lead_id,
success: function(data) {
$(this).html(data);
//$("#dischktime").show(data);-----(Not working)
}
});
});
});
</script>
You are creating element based on condition. When button is shown your Time span having id : dischktime is not created and thus its not available in DOM. Modify you foreach :
foreach($leads as $val):?>
<tr><td class = 'account-name sort-value'>
<span style='float:left;width:47%;'>
<?php echo $val->first_name .' '. $val->last_name;?></span>
<?php
if($val->checkin_time != '') {?>
<span class = 'Dischktime' id = 'dischktime{<?php echo $val->lead_id;?>}' ><?php echo $val->checkin_time;?>
</span>
<?php
}
else {?>
<span style='float:left;width:26%;'>
<button id ='checkin{<?php echo $val->lead_id;?>}' class='btn btn-default' name='checkinbt' value='<?php echo$val->lead_id; type='button'>CheckIn
</button>
</span>
**<span style="display:none;" class = 'Dischktime ' id = 'dischktime{<?php echo $val->lead_id;?>}' >**
</span>
<?php }
?>
<span>
<?php echo $_SESSION['drivername'];?>
</span>
</td></tr>
<?php endforeach;
}
Try Css Property
$("#dischktime").css('display', 'block') instead of $("#dischktime").show();
Hello guys I'm trying to delete a record/row from my table using a modal delete confirmation. This is what I've tried so far:
<script type="text/javascript">
$(function(){
$(".btn-show-modal").click(function(e){
e.preventDefault();
$("#dialog-example").modal('show');
});
$("#btn-delete").click(function(e) {
$("#dialog-example").modal('hide');
});
});
</script>
<table class="table table-bordered">
<?php
$stmt2 = $conn->prepare( "SELECT project_code, description
FROM tblprojects" );
$stmt2->execute();
for($i=0; $row2 = $stmt2->fetch(); $i++){
$project = $row2['project_code'];
$desc = $row2['description'];
?>
<tr>
<td><a href="project-detail.php?code=<?php echo $project; ?>">
<?php echo $project; ?></a></td>
<td><?php echo $desc; ?></td>
<td><a href="update-project.php?code=<?php echo $project; ?>" title="Update record">
<i class="icon-edit icon-white">
</i>
</a></td>
<td><a href="#<?php echo $project; ?>"
id="<?php echo $project; ?>"
data-id="<?php echo $project; ?>"
class="btn-show-modal" data-toggle="modal" title="Delete record">
<i class="icon-trash icon-white"></i></a></td>
<div class="modal hide fade" id="dialog-example">
<div class="modal-header">
<h5>Confirm Delete</h5>
</div>
<div class="modal-body">
<p class="modaltext">Are you sure you want to delete this record?</p>
</div>
<div class="modal-footer">
<a href="#" data-dismiss="modal" class="btn btn-info">No<a>
<a href="delete-project.php?code=<?php echo $project; ?>"
class="btn btn-danger" id="btn-delete">Yes<a>
</div>
</div>
</tr>
<?php
}
?>
</table>
But the problem is, when I am about to delete the last row the one that gets deleted is the first row. Why is it like that? Any ideas? Help is much appreciated. Thanks.
The problem lies in the modal generation and passing the $project value.
You are using a loop as
for($i=0; $row2 = $stmt2->fetch(); $i++){
$project = $row2['project_code'];
$desc = $row2['description'];
?>
And inside the above loop you are generating the modals so basically you will have many modals which are equal to the num of rows in the query.
Now all of them are having the same "id" i.e. "dialog-example" and once you click on the delete it pop ups the first modal from the DOM and is deleting wrong data.
Solution
For each modal you give the id as
<div class="modal hide fade" id="dialog-example_<?php echo $project; ?>">
Then in the blow code
$(".btn-show-modal").click(function(e){
e.preventDefault();
$("#dialog-example").modal('show');
});
Get the id of the element using attr("id") and append this at the end of
"dialog-example_"+{id you received}
The same thing you need to do for the hide modal as well.
UPDATE ON HOW TO DO IT
Give the modal div id as
<div class="modal hide fade" id="dialog-example_<?php echo $project; ?>">
Then in the click function to as
$(".btn-show-modal").click(function(e){
e.preventDefault();
var id = $(this).attr('id');
var modal_id = "dialog-example_"+id;
$("#"+modal_id).modal('show');
});
Change
<a href="delete-project.php?code=<?php echo $project; ?>"
class="btn btn-danger" id="btn-delete">Yes<a>
to
<a href="delete-project.php?code=<?php echo $project; ?>"
class="btn btn-danger" id="<?php echo $project ;?>">Yes<a>
AND finally
$("#btn-delete").click(function(e) {
$("#dialog-example").modal('hide');
});
to
$(".btn btn-danger").click(function(e) {
var id = $(this).attr('id');
var modal_id = "dialog-example_"+id;
$("#"+modal_id).modal('hide');
});
because you generate modal for every row this is wrong !
and show modal(first modal show) this is wrong!(this delete first row)
create one modal and set parameter with jquery e.g
$(".btn-show-modal").click(function(e){
e.preventDefault();
$("#dialog-example").modal('show');
$("#dialog-example #btn-delete").attr('href','delete-project.php?code='+$(this).attr('data-id'));
});
good luck
On topic
Problem is you have multiple modals. But select it on 1 id. So jQuery will select the first value.
A solution would be to put the delete url in an hidden input field. Then when the user clicks the open delete modal you select the url and put it in the a tag.
Example time
JavaScript Part
$(function(){
$(".btn-show-modal").click(function(e){
// put the right url in the delete
$("#dialog-example .delete-url").attr('href', $(this).attr('data-delete-url');
$("#dialog-example").modal('show');
return e.preventDefault();
});
$("#btn-delete").click(function(e) {
$("#dialog-example").modal('hide');
});
});
PHP part
I assume $stmt is prepared etc
<ul>
<? foreach($stmt->fetchAll() as $record) : ?>
<li>
delete
</li>
<? endforeach; ?>
</ul>
<div class="modal hide fade" id="dialog-example">
<div class="modal-header">
<h5>Confirm Delete</h5>
</div>
<div class="modal-body">
<p class="modaltext">Are you sure you want to delete this record?</p>
</div>
<div class="modal-footer">
<a href="#" data-dismiss="modal" class="btn btn-info">No<a>
<a class="delete-url btn btn-danger">Yes<a>
</div>
</div>
Code explaind
It is not handy to put an bootstrap modal in an foreach/for/while unless you change the id. But then again lot of duplicate code.
What the code does it changes the delete-url on the fly depending on which a the user clicked
Off topic
I highly recommend using the foreach for your iteration of the data records instead of a for loop.
$stmt2 = $conn->prepare( "SELECT project_code, description
FROM tblprojects" );
$stmt2->execute();
for($i=0; $row2 = $stmt2->fetch(); $i++){ /** code **/}
would be
$stmt2 = $conn->prepare( "SELECT project_code, description
FROM tblprojects" );
$stmt2->execute();
foreach($stmt2->fetchAll() as $record){}
Because when you open the modal, it open the first #dialog-example div.
<script type="text/javascript">
$(function(){
$(".btn-show-modal").click(function(e){
e.preventDefault();
$(this).parent().find("#dialog-example").modal('show');
});