Change DIV content using ajax, php and jQuery
<script>
function getSummary(id)
{
$.ajax({
type: "GET",
url: 'Your URL',
data: "id=" + id, // appears as $_GET['id'] @ your backend side
success: function(data) {
// data is ur summary
$('#summary').html(data);
}
});
}
</script>
And add onclick
event in your lists
<a onclick="getSummary('1')">View Text</a>
<div id="#summary">This text will be replaced when the onclick event (link is clicked) is triggered.</div>
How can I change div contents using jquery and PHP
Its simple,
you can add after the following content in "showSaved" to the "removeSaved" div and make "removeSaved div hide.
eg as follows :-
$.ajax({
method: "POST",
url: "deleteLink.php",
dataType: "json",
data: { 'deletedID' : deletedID },
success: function(deleteIDFunc){
var deleteIDFunc = $.trim(deleteIDFunc);
if(deleteIDFunc){
$('#removeSaved').html(deleteIDFunc); //<-how can I change the div so that it will display the other icon
$('#removeSaved').hide();
$("#removeSaved").after('<span id="showSaved"><a href="javascript:void();" class="pull-right" onclick="saveLink('+deletedID +');">Save <i class="fa fa-heart"></i></a></span>');
}
else {
alert('Your saved post was not removed. Please try again');
}
}
});
Please note the ID to pass, if you want pass the id to the ajax function deleteLink()
how can i replace to div using ajax and php
There are certain things missing in the code you've posted. Lets just simplify the code a little bit with some modifications.
index.php
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> <!-- Added jQuery Reference -->
</head>
<body> <!-- Added body tag -->
<?php
echo "<input type='hidden' id='idnya' value='value_posted_for_idnya'><div id='box'> Replace Me </div> <a class='readmore' href='javascript:void(0)'>ReadMore</a>";
?>
<script>
$(document).ready(function(){ // Wrapped the script in $(document).ready function
$('.readmore').click(function(){
$.ajax({
type: "POST",
url: "readmore.php",
data: "idnya=" + $("#idnya").val(), //say you have only one field to post for now
success: function(returnedData) {
$('#box').html(returnedData);
}
});
})});
</script>
</body>
</html>
readmore.php
Can stay the same.
Try the code above and see what you get.
EDIT
If you have multiple fields to post with ajax function you can use serializeArray()
method.
Simply wrap your fields with in a form
tag as follows
<form id="frm">
...form controls
</form>
This time you have to specify name
attributes in order for serializeArray()
to work.
Then your new ajax function becomes
$(document).ready(function(){
$('.readmore').click(function(){
$.ajax({
type: "POST",
url: "readmore.php",
data: $("#frm").serializeArray(), //get all name-value pairs within form
success: function(returnedData) {
$('#box').html(returnedData);
}
});
})});
</script>
Related Topics
How to Remove Extension from String (Only Real Extension!)
How to Get Rid of Eval-Base64_Decode Like PHP Virus Files
How to Emulate a Get Request Exactly Like a Web Browser
How to Enable Curl in Wamp Server
How to Check If a Word Is Contained in Another String Using PHP
Passing Multiple Variables to Another Page in Url
Remove Warning Messages in PHP
How to Sort a Multidimensional Array by One of the Fields of the Inner Array in PHP
Why PHP Script Is Not Workig in a Web Browser
How to Check If a File Is Mp3 or Image File
Explode String into Array With No Empty Elements
Add 'Watermark' to Images With PHP