How can i display time in place of button? - php

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();

Related

PHP and AJAX - like system - like the twitter system

I was making a 'like system' - like the twitter system - with PHP and AJAX. In a way that I can't understand, it applies to all buttons on the page when I press only one button. Please help.
enter image description here
index.php
<div id="rezbtn">
<span id="rezarea">
<button style="font-size:15px; float:left; <? if($sorus["soru_id"] == $rezs['soru_id'] && $user_id == $rezs['user_id']){ echo 'color:green;';}else{ echo ''; } ?>" class="btn rezle" id="rezle" type="button" title="<? echo $sorus["soru_id"]; ?>">
<span>#Rezle <? echo $rez["rez_id"]; ?></span>
</button>
</span>
</div>
ajax.php
if($rezuscount > 0)
{
echo "<span style='color:black;'>#Rezle ", $rezcount-1,"</span>";
}
else
{
echo "<span style='color:green;'>#Rezle ", $rezcount+1,"</span>";
}
like.js
$('.rezle').click(function(){
var soruid = $(this).attr("title");
$.ajax({
type:"POST",
url:"ajax.php",
data:{"soru":soruid},
success:function(e)
{
$('.rezle').html(e);
}
})
});
My codes are like this.
Your problem here is that you're referring to all elements with the class rezle and updating them all with the ajax html response:
$('.rezle').html(e);
try this:
$('.rezle').click(function(){
const rezle_button = this;
var soruid = $(this).attr("title");
$.ajax({
type:"POST",
url:"rezle.php",
data:{"soru":soruid},
success:function(e)
{
$(rezle_button).html(e);
}
})
});

Update and Display value without refreshing the page

I want to change the status (Active/Deactive) of records in database without actually submitting(refreshing page) data, using jquery.
In my script Status is getting changed in database but as it is not refreshing, display status is not updating. is there a way to display the changed status without refreshing? i mean as soon as it updates in database, it should reflect in the status.
Here is my script
<table class="table table-bordered table-condensed table-striped mb-6">
<thead>
<tr>
<th>Firstname</th>
<th>Mobile</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
//$CQuery = ""; my query
while($ConRow = DB_fetch_array($CQuery ))
{
?>
<tr>
<td><?php echo $ConRow['fname']; ?></td>
<td><?php echo $ConRow['mobile']; ?></td>
<?php if($ConRow['status']=='A') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-danger btn-sm">De-Activate</button> </td>
<?php } else if($ConRow['status']=='D') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-success btn-sm">Activate</button> </td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
Script
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = $(this).attr("id");
var data1 = 'id=' + del_id;
if (confirm("Sure you want to De-Activate this?.")) {
$.ajax({
type : "POST",
url : "DisableContact.php",
data : data1,
success : function() {
}
});
$(this).parents(".record").animate("fast").animate({
opacity : "hide"
}, "slow");
}
return false;
});
});
</script>
PHP
$id = $_POST['id'];
$sel1 = "SELECT status FROM contacts WHERE con_id=".$id."";
$Sel = DB_query($sel1);
$srow = DB_fetch_array($Sel);
if($srow['status']=='A')
{
$sql = "UPDATE contacts SET status='D' WHERE con_id=".$id;
}
else
if($srow['status']=='D')
{
$sql = "UPDATE contacts SET status='A' WHERE con_id=".$id;
}
$result = DB_query($sql);
In database , Status is getting changed, but in table it is not showing up.
//Updated
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
if (confirm("Sure you want to De-Activate this?.")) {
$.ajax({
type : "POST",
url : "DisableContact.php", //URL to the delete php script
data : info,
success: function(data) {
var d = $.trim(data); //triming value if there is any whitespaces
if (d == "A") {
//means data is activate so show that button
$("#"+del_id+ ".btn-success").show();
//hiding other
$("#"+del_id +".btn-danger").hide();
} else {
//show deactivate buttton
$("#"+del_id +".btn-danger").show();
//hide other button
$("#"+del_id +".btn-success").hide();
}
}
});
$(this).parents(".record").animate("fast").animate({
opacity : "hide"
}, "slow");
}
return false;
});
});
</script>
PHP
if($srow['status']=='A')
{
$sql = "UPDATE cust_contacts SET status='D' WHERE con_id=".$id;
echo 'A';
}
else
if($srow['status']=='D')
{
$sql = "UPDATE cust_contacts SET status='A' WHERE con_id=".$id;
echo 'D';
}
$result = DB_query($sql);
You can pass some value from php to ajax call and depending on that the required button will get displayed .So your php code will look like below :
..
if($srow['status']=='A')
{
$sql = "UPDATE contacts SET status='D' WHERE con_id=".$id;
echo "D";//will get passed as response to ajax
}
else
if($srow['status']=='D')
{
$sql = "UPDATE contacts SET status='A' WHERE con_id=".$id;
echo "A";//will get passed to ajax as response
}
Your ajax success function will look like below :
..
success: function(data) {
var d = $.trim(data); //triming value if there is any whitespaces
if (d == "A") {
//means data is activate so show that button
$("#"+del_id+ ".btn-success").show();
//hiding other
$("#"+del_id +".btn-danger").hide();
} else {
//show deactivate buttton
$("#"+del_id +".btn-danger").show();
//hide other button
$("#"+del_id +".btn-success").hide();
}
}
Update 1:
As you have use if-else to show button so i forgot here that other button will not exist in this case thats the reason jquery is not able to find other button and display blank.Now, to solve this you need to make some changes in your php code where you are displaying your table.Changes you need to make are as follows :
Change this :
<?php if($ConRow['status']=='A') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-danger btn-sm">De-Activate</button> </td>
<?php } else if($ConRow['status']=='D') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-success btn-sm">Activate</button> </td>
<?php } ?>
to below :
<td> <div class="<?php echo $ConRow['con_id']; ?>"> <?php if($ConRow['status']=='A') { ?>
<button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-danger btn-sm">De-Activate</button>
<?php } else if($ConRow['status']=='D') { ?>
<button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-success btn-sm">Activate</button>
<?php } ?> </div> </td>
Now ,in ajax success function we will use .html to add button inside <div></div> .So ajax will look like below :
if (d == "A") {
$("." + del_id).html('<button id="' + del_id + '" class="delbutton btn btn-danger btn-sm">De-Activate</button>');
} else {
$("." + del_id).html(' <button id="' + del_id + '" class="delbutton btn btn-success btn-sm">Activate</button> ');
}

How to prevent Jquery script loading twice in ajax call or submit JQuery Function to Ajax Called Page

Hi guys i got a problem with my Code. I load new Page Content on click of Button and got a JQuery Function with onclick which changes a love or like count on Page. If i dont include the JQuery Function into the Ajax loaded page it doesn`t work but if i do he acts twice the function any idea how to fix this or solutions?
This is the JQuery Script from page:
$(".toggle").on("click", function(e) {
$('.alert-box').hide();
$( '.alert-box' ).remove();
var $this = $(this);
var $lovecount = $this.closest("div").find(".lovecount");
if ($(e.target).hasClass('love')) {
//alert('Liebe genommen');
var val = +$lovecount.text() + (this.checked ? -1 : -1);
$(e.target).prop( "checked", false );
} else {
//alert('Liebe gegeben');
var val = +$lovecount.text() + (this.checked ? 1 : 1);
$(e.target).prop( "checked", true );
};
//var val = +$lovecount.text() + (this.checked ? 1 : -1);
$lovecount.text(val)
$('.alert-box').toggle(!this.checked);
$(this).toggleClass("love");
var postid = $this.attr("data-post");
var userid = $this.attr("data-userid");
dataString = 'userid=' + userid + '&postid=' + postid;
// Are you not supposed to send the new value to the server?
$.ajax({
type: 'POST',
url: 'inc/give-love.php',
data: dataString,
success: function(response) {
$('<div class="alert-box success">'+response+'</div>').prependTo('body').delay(3000).fadeOut(1000, function() {
$('.alert-box').hide();
$( '.alert-box' ).remove();
});
}
})
})
with this i change and toggle input from this HTML Code Structure.
<!-- POST ITEM START -->
<div class="post-item">
<div class="post-asset image">
<img src="uploads/<?php echo $row['picture']; ?>">
</div>
<div class="post-header">
<h3 class="post-title"><?php echo $row['title']; ?></h3>
<span class="post-category">
<a class="favorite-button" href="#" data-post="<?php echo $row['id']; ?>" data-userid="<?php echo $_SESSION['user_id'];?>"><span class="favorite-button-icon fa fa-star-o"></span></a>
</span>
<span class="post-date font17"><i class="fa fa-clock-o"></i> <?php $timeago=get_timeago(strtotime($row['created'])); echo $timeago;?></span>
<span class="post-comments font17"><i class="fa fa-comments-o"></i> 1,3k Reaktionen</span>
</div>
<div class="post-footer">
<a href="#" class="post-author">
<span class="author-img"><img src="img/avatar.png"></span>
<span class="author-name">OnePost von<b><?php echo $row['poster']; ?></b></span>
</a>
<div class="post-extra">
<!--<div class="add-favorite" id="heart-container"><span class="lovecount">112</span><input data-post="123" data-user="Jessica Jones" type="checkbox" id="toggle" onclick="myFunction(this)" checked><div id="twitter-heart" class=""></div></input></div>-->
<div class="add-favorite" id="heart-container"><span class="lovecount"><?php echo $row['loves']; ?></span>
<!--<input data-post="<?php echo $row['id']; ?>" data-userid="<?php echo $_SESSION['user_id'];?>" type="checkbox" id="toggle" class="toggle love" onclick="myFunction(this)">-->
<?php
if($row['is_liked'] != 1){ ?>
<input data-post="<?php echo $row['id']; ?>" data-userid="<?php echo $_SESSION['user_id'];?>" type="checkbox" id="toggle" class="toggle">
<?}else{?>
<input data-post="<?php echo $row['id']; ?>" data-userid="<?php echo $_SESSION['user_id'];?>" type="checkbox" id="toggle" class="toggle love" checked="true">
<?php }?>
<div id="twitter-heart" class=""></div></input></div>
<div class="post-share"><i class="fa fa-share-alt"></i>
<div class="social-links">
<i class="fa fa-facebook"></i>
<i class="fa fa-twitter"></i>
<i class="fa fa-pinterest"></i>
<i class="fa fa-whatsapp"></i>
</div>
</div>
</div>
</div>
</div>
<!-- POST ITEM END -->
When i include the JQuery script in Ajax page it fires twice the function if not onclick is not working. How to prevent this or make it working the right way?
If you append any element after DOM has loaded, you need to use delegate(); selector.
$(document).ready(function(){
$("#div1").append('<button id="btn">Click</button>');
//You can use delegate() selector and when you click #btn its works.
$("body").delegate("#btn", "click", function(){
alert("Button has working!");
});
});

jquery returning only first form data

i have a dynamically created form that on click event shows/hides submit and cancel button. Its only returning the first form data. i have searched a lot and no luck. already tried unbind and each helpers. here is the code snippet.
<div class="wojo top right floated div">
<?php if (!$row->counter_offer): ?>
<a id="makeCounter" class="wojo positive label" data-id ="<?php echo $row->id; ?> " ><?php echo 'Make Counter'; ?></a>
<form method="post" id="<?php echo "formID" . $row->id; ?>" class="wojo_form" name="wojo_form">
<input class="wojo black label"type="text" placeholder="Counter Offer" name="offer">
<button type="button" data-action="processOffer" name="submitOffer" class="wojo positive label"
data-set='{ "action": "processOffer",
"id": <?php echo $row->id; ?>,
"current_offer" : "<?php echo $row->offer_price; ?>",
"sender_id" : <?php echo $row->sender_id; ?>
}' >Submit Counter</button>
<a id="<?php echo "cancelID" . $row->id; ?>" class="wojo negative label" data-id ="<?php echo $row->id; ?> "><?php echo Lang::$word->CANCEL; ?></a>
</form>
<?php endif; ?>
<a id="rejectOffer <?php echo $row->id; ?>" class="rejectOffer" data-set='{
"parent": ".segment",
"id": <?php echo $row->id; ?>}' data-content="Remove Offer">
<span id="<?php echo "rejectID" . $row->id; ?>" class="wojo negative label"><?php echo 'Reject Offer'; ?></span></a>
</div>
//
$( document ).ready( function() {
$('.wojo_form').hide();
$('#makeCounter').each(function() {
$(this).unbind('click').click( function() {
alert($('#makeCounter').attr("data-id"));
$('#formID' + $(this).data('id')).show();
$(this).hide();
$('#rejectID' + $(this).data('id')).hide();
$('#cancelID' + $(this).data('id')).click( function() {
$('#formID' + $(this).data('id')).hide();
// $('[id = makeCounter] [data('id') = $(this).data('id')] ').show
$('#makeCounter').show
$('#rejectID' + $(this).data('id')).show();
});
});
});
});
</script>
it only works on the first counter offer button and the second counter offer doesn't do any thing. please help. I'm going nuts here for three days.
The "ID" attribute by definition cannot have duplicates! Since JQuery is aware of this, $('#makeCounter') will only select the first occurrence with that ID.
Also, there is no need to bind each event one by one, you can bind an event to an entire class of elements.
To fix the issue, use a classinstead of an id:
in the PHP:
<a class="wojo positive label makeCounter" data-id ="<?php echo $row->id; ?> " ><?php echo 'Make Counter'; ?></a>
You'll also want to set up your cancel button like this:
<a class="wojo negative label cancelButton" data-id ="<?php echo $row->id; ?> "><?php echo Lang::$word->CANCEL; ?></a>
At this point, the each is unnecessary, you can bind all of them at once like this:
$(".makeCounter").click( function() {
alert($('#makeCounter').attr("data-id"));
$('#formID' + $(this).data('id')).show();
$(this).hide();
$('#rejectID' + $(this).data('id')).hide();
});
$('.cancelButton').click( function() {
$('#formID' + $(this).data('id')).hide();
$('#rejectID' + $(this).data('id')).show();
// check out the JQuery selector here:
$('.makeCounter[data-id="'+$(this).data('id')+'"]').show();
});
(you don't need the unbind either)

Change input value when i press <a> Attribute

Ok guys already search for it but no results at all, and cant figured it out how to do this, so my problem is:
I have this form:
<form method="POST" action="search.php" name="form" id="form">
<input type="text" name="search" id="search" placeholder="Introduza a sua Localização">
<input type="submit" name="Submit" value="Pesquisar">
</form>
And what i would like to is when i press this:
<a onClick="">City</a>
The input value change to what i want in this case "City" and then automatically submit the form.
I already made a research about this and theres nothing i could take, any help would be appreciated.
Cumps.
Guys thanks to your help now its working but someone tell me please what the problem in this code:
<?php if (mysql_num_rows($tournaments) !=0){
do { ?>
<div id="mainContainer">
<div id="leftContainer"><img src="images/tournaments/<?php echo $row_tournaments['logo']; ?>.png"></div>
<div id="rightContainer">
<div id="rightContent">
<p><?php echo $row_tournaments['description']; ?></p>
<div id="galleryButton"><p>Entrar no torneio</p></div>
</div>
<div id="rightDetails">
<i class="fa fa-gamepad"></i> <?php echo $row_tournaments['game']; ?><br>
<i class="fa fa-calendar-o"></i> <?php echo $row_tournaments['date']; ?><br>
<i class="fa fa-pencil-square-o"></i> Sem comentários<br>
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
<i class="fa fa-map-marker"></i> <a onClick="giveThatInputAValue()"><?php echo $row_tournaments['city']; ?></a><br>
<img src="images/<?php echo $row_tournaments['online']; ?>.png"> <?php echo $row_tournaments['online']; ?>
</div>
</div>
</div>
<?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
} else {
?>
<div id="noresults"><p>Sem torneios</p></div>
<?php
}
?>
Everything is looped fine but that part:
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
Its not looping it only shows the first record why is it?
Any help would be appreciated.
in your js file you can check if the value of the textbox is equal to the cityname you want. if it is then submit the form. Attach the function to your a tag
function SubmitRightCity(){
if(document.getElementById('search').value == "citynamehere"){
document.forms["form"].submit();
}
}
in html
<a onClick="submitRightCity()">City</a>
In the case where you want your a tag to give the input a value, you attach a function to your tag, once the link is clicked, then the function will give your input a value.
<a onClick="giveThatInputAValue()"> City</a>
in javascript file you will have
<script>
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "My city value";
//if you want to submit the form right after, you add the line below
document.forms["form"].submit();
}
</script>

Categories