Ajax request load data from specific div? [duplicate] - php

my question is very simple how can I retrieve certain div with jquery ajax comand
$.ajax({
url: "test.html",
success: function(){
$(this).addClass("done");
}
});
like with load
$('#targetDiv').load('http://localhost/test.html #sourceDiv');

If the div is part of the AJAX response:
$.ajax({
url: 'test.html',
dataType: 'html',
success: function(html) {
var div = $('#sourceDiv', $(html)).addClass('done');
$('#targetDiv').html(div);
}
});

Here is a slightly different take on it. Also your target div maybe hidden by default so I've added in a Fade In affect to show it. If your html is going to change then you might want to add in a cache: false.
$.ajax({
url: "html.htm",
type: "GET",
dataType: "html",
success: function (res) {
$("#targetDiv").html($(res).find("#sourceDiv")
.addClass('done'))
.fadeIn('slow');
}
});
If you're interested you can take a look at how jQuery does the load method here jQuery Source Viewer

Here is an example that I use on my site for the navigation.
<ul id="nav">
<li><a name="portfolio" href="portfolio.php" class="ajax_nav">PORTFOLIO</a></li>
<li><a name="biography" href="biography.php" class="ajax_nav">BIOGRAPHY</a></li>
</ul>
For the javascript you can try this.
var hash = window.location.hash.substr(1);
var hash2 = window.location.hash.substr(1);
// Menu items to lower main content
var href = $('.ajax_nav').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-4)){
var toLoad = hash+'.html #ajax_content';
$('#ajax_content').load(toLoad)
}
});
// For inner overlay of featured content.
var href2 = $('.feat_item_link').each(function(){
var href2 = $(this).attr('href');
if(hash2==href2.substr(0,href.length-4)){
var toLoadFeatured = hash2+'.html #ajax_featured_content';
$('#ajax_featured_content').load(toLoadFeatured);
}
});
// For Bottom Navigation
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #ajax_content';
$('#content').hide('fast',loadContent);
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href'));
function loadContent() {
$('#content').delay(1000).load(toLoad,'',showNewContent());
}
function showNewContent() {
$('#content').show('normal',hideLoader());
}
function hideLoader() {
$('#load').delay(1000).fadeOut('normal');
}
return false;
});
The id of #load simply uses an animated gif in the CSS.
Place the javascript in the $(document).ready() and you should be all set.

Related

Change HTML element into text from database using AJAX / JQuery according to ID

I'm new to Jquery and ran into a problem in web development. I want to convert html element to paragraph from database based on id from url.
Here my code for now,
<script>
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
$(document).ready(function() {
$("#desc").click(function() {
$("#content").html(id);
$(this).addClass('active');
$("#feature").removeClass('active');
$("#spec").removeClass('active');
});
$("#feature").click(function() {
$("#content").html(id);
$(this).addClass('active');
$("#desc").removeClass('active');
$("#spec").removeClass('active');
});
$("#spec").click(function() {
$("#content").html(id);
$(this).addClass('active');
$("#desc").removeClass('active');
$("#feature").removeClass('active');
});
});
</script>
Here is the display that i wish
I've got the id, but don't know how to display data from the database (tbl_product) based on that id using AJAX / Jquery. I can only change the active class for now, all your advice and suggestions are will be very usefull.
you can use ajax like this
$("#desc").click(function() {
$(this).addClass('active');
$("#feature").removeClass('active');
$("#spec").removeClass('active');
$.ajax({
url: 'product.php?id=' + id
dataType: 'html',
contentType: false,
processData: false,
beforeSend: function() {
$('#content').empty();
},
error: function() {
$('#content').append('error');
},
success: function(data) {
$('#content').append(data);
}
})
});

read more button shows again after click

i have a readmore link that allows people to read more posts once it has been clicked, what i don't want is the button showing again and i also don't want the whole post to show again, i just want the remaining posts to add to the previous that the reader has read or being reading just as its done on facebook page when you click seemore link.
Thanks
$(function(){
$(".readMore").click(function(event){
event.preventDefault();
var link = $(this).attr('href');
var dataString = 'link='+ link;
$.ajax({
type : "POST",
url: "readmore-post.php",
data: dataString,
cache : false,
success: function(html){
$(".read_more").before(html);
}
});
});
});
this is the html part
<?php echo substr($post,0,250); ?>..
<?php
if(strlen($post)>250)
{
echo"<a href='$postID' style='font-size:0.8em;' class='readMore'>readmore</a> </br>";
}
?>
<div class="read_more"></div>
this is the php part
<?php
include('../config/connect.php');
$read_id = $_POST['link'];
$srm = "SELECT * FROM page_timeline WHERE timelineID='$read_id' ORDER BY timelineID DESC";
$sre = $db->query($srm);
while($rowr = $sre->fetch_array())
{
$postID = $rowr['timelineID'];
$post = $rowr['post'];
}
echo "<p>".$post."</p>";
?>
Try this and see inline comments
$(function(){
$(".readMore").click(function(event){
event.preventDefault();
var that=$(this);
var link = $(this).attr('href');
var dataString = 'link='+ link;
$.ajax({
type : "POST",
url: "readmore-post.php",
data: dataString,
cache : false,
success: function(html){
$(".read_more").empty().append(html);
//clear all the contents and append new one to .read_more div
that.hide(); //hide the button on successful load
},
});
});
});

jquery and ajax to load php content

I have a <div class="container"> which will display content from separate php files located in a content folder, that are linked to individual <a> tags presented in a <ul>. Once a link from the list is clicked a loading graphic(processing...)class="process" will display in the container and the php file called upon will display.
HTML(watered down):
<div class="container"></div>
<ul id="yourIdeas">
<li>one</li> <!--content/one.php-->
<li>two</li>
<li>three</li>
<li>four</li>
</ul>
the container/loading graphic, styling etc... is all fine.
JQUERY:
(function() {
var typ = {
loading: $('<div />', { class: 'process' }),
area: $('.container')
}
var file = $('ul#yourIdeas li a').attr('href');
var thePage = $('content/' + file + '.php');
$('ul#yourIdeas li a').on('click', function() {
$.ajax({
url: 'thePage',
beforeSend: function() {
typ.area.append(typ.loading);
},
success: function(data) {
typ.area.html(data);
}
});
});
});
I cannot get the Jquery/Ajax to perform this task;
When a ul#yourIdeas li a is clicked, it grabs the href and associates it to the designated php file, which then loads the 'processing...' in the container, followed by displaying the content of the chosen php file in the container.
I can get it to work if i specifically ask for each link separately;
CODE:
var typ = {
loading: $('<div />', { class: 'process' }),
area: $('.container')
}
$('ul#yourIdeas li a#two').on('click', function() {
$.ajax({
url: 'content/two.php',
beforeSend: function() {
typ.area.append(typ.loading);
},
success: function(data) {
typ.area.html(data);
}
});
});
But i would like to have the one script to encompass all links and files, if possible.
Thanks in advance for any help.
You must move file and thePage variables inside your click function:
(function() {
var typ = {
loading: $('<div />', { class: 'process' }),
area: $('.container')
};
$('ul#yourIdeas li a').on('click', function(e) {
e.preventDefault();
var file = $(this).attr('href');
var thePage = 'content/' + file + '.php';
$.ajax({
url: thePage,
beforeSend: function() {
typ.area.append(typ.loading);
},
success: function(data) {
typ.area.html(data);
}
});
});
});

how to load sql/session content to a div from loaded div

I have been researching for the last two days, and have found nothing.
structure:
index.php:
<head>
<script type="text/javascript" src="JS/jquery-1.6.2.js"></script>
<script type="text/javascript" src="function.js"></script>
</head>
<body>
<div>
<div>Show</div> *-->if I click this link data loads into DIV below by function.js without reloading*
<div id="producten"></div> *-->testpage.php loads here perfect,
the code of testpage.php makes by while loop other links.
Here I want to click on a link to load next data in div id=information
without reloading the index.php so the data in the first DIV stay visible
and I dont know how to do that*
<div id="information"></div> *-->testpage2.php have to load data from sql in this DIV*
</div>
</body>
function.js:
$(document).ready(function() {
$(".testcat").click(function() {
var testid = $(this).attr("id");
var datastring = 'id='+ testid ;
$.ajax({
type: "POST",
url: "testpage.php",
data: datastring,
cache: false,
success: function(res) {
$('#producten').html("<div class='loading'><img src='IMG/loading.gif' /></div>")
.hide()
.fadeIn(2000, function() {
$('#producten').html(res);
})
}
});
return false;
});
});
testpage.php and testpage2.php are PDO code for sql data.
You'll want to attach your click handlers with on so that dynamically added content still has the same ajax handlers available to them:
Add whatever information is needed to differentiate one click from the next, ie
<a href='...' data-resultdiv='production'
Then, cleaning up your handler a bit: I assume you want the ajax request to go to the href of the link, and that you want to show "loading" immediately (instead of waiting for the request to complete).
Finally, to cancel the anchor's default behavior of browsing to the page referenced by the href, you can return false;
$(document).on("click", "a", function() {
var href = $(this).attr("href");
var successDiv = $(this).data("resultdiv");
$('#' + successDiv).html("<div class='loading'><img src='IMG/loading.gif' /></div>");
$.ajax({
type: "POST",
url: href,
data: datastring,
cache: false,
success: function(res) {
$('#' + successDiv).hide().html(res).fadeIn(2000);
}
}
return false;
});
And of course if you only want this to run for certain anchors, you can put a selector on your call to on
$(document).on("click", "a.someClass", function() {

Change DIV content using ajax, php and jQuery

I have a div which contains some text for the database:
<div id="summary">Here is summary of movie</div>
And list of links:
Name of movie
Name of movie
..
The process should be something like this:
Click on the link
Ajax using the url of the link to pass data via GET to php file / same page
PHP returns string
The div is changed to this string
<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>
You could achieve this quite easily with jQuery by registering for the click event of the anchors (with class="movie") and using the .load() method to send an AJAX request and replace the contents of the summary div:
$(function() {
$('.movie').click(function() {
$('#summary').load(this.href);
// it's important to return false from the click
// handler in order to cancel the default action
// of the link which is to redirect to the url and
// execute the AJAX request
return false;
});
});
try this
function getmoviename(id)
{
var p_url= yoururl from where you get movie name,
jQuery.ajax({
type: "GET",
url: p_url,
data: "id=" + id,
success: function(data) {
$('#summary').html(data);
}
});
}
and you html part is
<a href="javascript:void(0);" class="movie" onclick="getmoviename(youridvariable)">
Name of movie</a>
<div id="summary">Here is summary of movie</div>
This works for me and you don't need the inline script:
Javascript:
$(document).ready(function() {
$('.showme').bind('click', function() {
var id=$(this).attr("id");
var num=$(this).attr("class");
var poststr="request="+num+"&moreinfo="+id;
$.ajax({
url:"testme.php",
cache:0,
data:poststr,
success:function(result){
document.getElementById("stuff").innerHTML=result;
}
});
});
});
HTML:
<div class='request_1 showme' id='rating_1'>More stuff 1</div>
<div class='request_2 showme' id='rating_2'>More stuff 2</div>
<div class='request_3 showme' id='rating_3'>More stuff 3</div>
<div id="stuff">Here is some stuff that will update when the links above are clicked</div>
The request is sent to testme.php:
header("Cache-Control: no-cache");
header("Pragma: nocache");
$request_id = preg_replace("/[^0-9]/","",$_REQUEST['request']);
$request_moreinfo = preg_replace("/[^0-9]/","",$_REQUEST['moreinfo']);
if($request_id=="1")
{
echo "show 1";
}
elseif($request_id=="2")
{
echo "show 2";
}
else
{
echo "show 3";
}
jQuery.load()
$('#summary').load('ajax.php', function() {
alert('Loaded.');
});
<script>
$(function(){
$('.movie').click(function(){
var this_href=$(this).attr('href');
$.ajax({
url:this_href,
type:'post',
cache:false,
success:function(data)
{
$('#summary').html(data);
}
});
return false;
});
});
</script>
<script>
function getSummary(id)
{
$.ajax({
type: "GET",//post
url: 'Your URL',
data: "id="+id, // appears as $_GET['id'] # ur backend side
success: function(data) {
// data is ur summary
$('#summary').html(data);
}
});
}
</script>

Categories