On page load I use this code to display data from database:
<script>
$(function feed(){
var page = 1;
var type = '<?php echo $filter ;?>';
var theData = {};
theData['page'] = 'profile';
theData['type'] = type;
theData['username'] = '<?php echo $user_data->username; ?>';
theData['get_activities'] = 'true';
$.ajax({
type: "POST",
url: "data.php",
data: theData,
dataType:'json',
success: function(data){
$("#activities").html(data.activity_feed);
if(page < data.total_pages){
$("#activities").after('<div id="loader"><button id="load_more_activities">Load more</button></div>');
}
}
});
$("#activity_container").on("click", "#load_more_activities", function(){
var next = page+=1;
var type = '<?php echo $filter ;?>';
var theData = {};
theData['page'] = 'profile';
theData['type'] = type;
theData['username'] = '<?php echo $user_data->username; ?>';
theData['get_activities'] = 'true';
theData['page_num'] = next;
$.ajax({
type: "POST",
url: "data.php",
data: theData,
dataType: "json",
success: function(data){
$("#activities").append(data.activity_feed);
if(next == data.total_pages){
$("#loader").html("No more data");
} else {
$("#loader").html('<div id="loader"><button id="load_more_activities">Load more</button></div>');
}
},
});
});
});
</script>
Everything work fine, but if I refresh page more than 5 or 6 times or if load_more_activities function is called more than 5 or 6 times...then post is not executed and I don't get any data displayed...
Is something wrong with this code or there are maybe some restrictions from my host provider?
When post is executed:
When post is not executed:
My host provider has a protection from several sending ajax data with variables named "username" or "password". So when this happens, server detect some kind »brute-force« and then drop-all-packets.
Related
My PHP is returning this data to Ajax...
echo $data6['favorite_properties_id'];
I am updating it in one function and trying to send it to another using following html and jquery .
<img class="<?php if($favorite == 1){ echo 'alreadyfavorite';} else { echo 'addtofavorite';} ?>" pid="<?php echo $propertyid; ?>" fpid="<?php while($data5=$select5->fetch()){echo $data5['favorite_properties_id'];} ?>" src="../images/system/addtofavorite.png">
This is my jquery...
$('.alreadyfavorite1').click(function() {
event.preventDefault();
var del_id = $(this).attr('fpid');
var $ele = $(this).parent().parent().parent();
var reference = this;
$.ajax(
{
type: 'POST',
url: '../controllers/favoritesaddremove.php',
data:
{
del_id: del_id
},
success: function(data)
{
$ele.fadeOut(1000).delay(1000).remove(1000);
}
});
});
// On Search Property Results Page - Add to Favorite Button (Heart)
$('.addtofavorite').click(function() {
event.preventDefault();
var ins_id = $(this).attr('pid');
var del_id = $(this).attr('fpid');
var reference = this;
/* alert(del_id);
alert(ins_id); */
$.ajax(
{
type: 'POST',
url: '../controllers/favoritesaddremove.php',
data:
{
ins_id: ins_id
},
success: function(data)
{
$(reference).toggleClass("addtofavorite alreadyfavorite");
$('.alreadyfavorite').attr('fpid', data);
}
});
});
The second function is not working, but if i refresh the page then the second function is working...
First assign some id to the image and change fpid to data-fpid(data-attribute):
<img class="asdasd" id="aid" data-fpid="something">
In your success try:
success: function(data)
{
$('#aid').data('fpid', data); //this should update the value in data-fpid
}
I'm working on the codeigniter web application I need to get users' data from the database but based on user id?
The problem is that ajax posting value to the index page browsers, network showing that Ajax posting value to the index page, When I try to echo or print variable on the index page.
Index Page Showing Error After Echo leadID
Notice: Undefined index: leadID and leadID = 0
Index Page
$addnote ="<a class='btn' data-popup-open='popup-1' href='#'
data-id='$lead->id'>Add Note</a>";
Ajax Posting
$('.btn').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-open');
$('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);
var leadID = $(this).attr('data-id');
var dataString = 'leadID=' + leadID;
$.ajax({
type: "POST",
dataType: 'text',
url: "index.php",
data: dataString,
success: function(response) {
}
});
});
Database Query
$leadID = intval($_POST['leadID']);
$sql = "select Note from {$dbPre}users where leadID='$leadID'";
$exists = $db->extQueryRowObj($sql);
$displaynote = $exists->Note;
If you need simple php solution then try this code
$('.btn').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-open');
$('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);
var leadID = $(this).attr('data-id');
$.ajax({
type: "POST",
dataType: 'text',
url: "index.php",
data: {
leadID : leadID
}
success: function(response) {
alert(response);
}
});
});
Now in index.php
$leadID = intval($_POST['leadID']);
$sql = 'select Note from '.$dbPre.'users where leadID='.$leadID;
$exists = $db->extQueryRowObj($sql);
$displaynote = $exists->Note;
echo $displaynote;
I have got this html
<div class="Likes" data-i=<?php echo $row[8];?>>
<img src="../img/like.png">
<p class="L_c"><?php echo $row[4];?></p>
</div>
And this jquery/ajax
$(".Likes").click(function() {
var i = $(this).attr("data-i");
$.ajax({
type: "GET",
url: '../connect.php',
data: "I=" + i,
success: function(data) {
$(this).children(".L_c").html(data);
}
});
});
Connect.php
if (isset($_GET["I"]) && !isset($_GET["C"])) {
$RandS=$_GET["I"];
$query3=$con->query("SELECT id,likes FROM uploads WHERE Rand='$RandS'");
$row=$query3->fetch_row();
$IdU=$row[0];
$Likes=$row[1];
$Sel2=$con->query("SELECT id FROM likes WHERE User_id='$NameId' AND Post_id='$IdU'");
$num_rows=$Sel2->num_rows;
if ($num_rows>0) {
echo $Likes;
}else{
$query=$con->query("INSERT INTO likes (Post_id,User_id,`DATE`) VALUES('$IdU','$NameId',NOW())");
$query=$con->query("UPDATE uploads SET Likes=$Likes+1 WHERE Rand='$RandS'");
echo $Likes+1;
}
}
But it does not return anything untill i refresh the page
The this keyword inside the $.ajax methods callback is not the element, but the ajax call itself.
You have to either set context, or just store the outer this -value
$(".Likes").on('click', function() {
var me = $(this);
var i = me.attr("data-i");
$.ajax({
type: "GET",
url: '../connect.php',
data: "I=" + i,
success: function(data) {
me.find(".L_c").html(data);
}
});
});
index.html:
Function drawChart() {
var jsonData = $.ajax({
url: "server.php",
dataType: "json",
async: false
}).responseText;
var obj = jQuery.parseJSON(jsonData);
var data = google.visualization.arrayToDataTable(obj);
var options = {
title: 'Number of visitors / <?php echo $unit; ?>'
};
var chart = new google.visualization.BarChart(
document.getElementById('chart_div'));
chart.draw(data, options);
}
}
server.php:
$SQLString = "SELECT (...)'".$_POST['value']."' (...)
$result = mysql_query($SQLString);
(...)
$data[$i] = array(...)
echo json_encode($data);
So, index.html get data from server.php right?
Can I send some values to server.php which are important to do the query before index.html do the jsonData...etc? How?
Thanks :)
Example of a query parameter:
var jsonData = $.ajax({
url: "server.php?someQuery=" + query,
dataType: "json",
async: false
}).responseText;
You can send using post method via ajax. Here is a JQuery example:
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
http://api.jquery.com/jQuery.ajax/
you can use a library that will do it automatically for you, using phery http://phery-php-ajax.net, in your case it would be:
the event phery:json will deal with the JSON sent from the server
var remote = phery.remote('data', {'argument': 'you want to send'}, {'target':'server.php'}, false);
remote.bind('phery:json', function(event, obj){
var data = google.visualization.arrayToDataTable(obj);
var options = {
title: 'Number of visitors / <?php echo $unit; ?>'
};
var chart = new google.visualization.BarChart(
document.getElementById('chart_div'));
chart.draw(data, options);
});
remote.phery('remote'); // call the remote AJAX function
in your server.php
function data($data){
$r = new PheryResponse;
// $data['argument'] will have 'you want to send'
$SQLString = "SELECT (...)'".$_POST['value']."' (...)"
$result = mysql_query($SQLString);
(...);
$data[$i] = array(...);
return $r->json($data);
}
Phery::instance()->set(array(
'data' => 'data'
))->process();
acctually i am not familier much with jquery.. i got this jquery script this is passing variables to the file which is showing data in json format.. but here i'm unable to show that data..plz see this piece of code
$(document).ready(function() {
var globalRequest = 0;
$('#search').bind('keyup', function(event) {
if (event.keyCode == 13) {
searchAction();
}
});
$('#search-link').bind('click', function(event) {
searchAction();
});
var searchAction = function() {
var value = $('#search').val();
var cat = $('#category').val();
var country = $('#country').val();
var page = $('#page').val();
var resultContainer = $('#results');
if (value.length < 3 && globalRequest == 1) {
return;
}
_gaq.push(['_trackEvent', 'Search', 'Execute', 'Page Search', value]);
globalRequest = 1;
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
success: function(data){
globalRequest = 0;
resultContainer.fadeOut('fast', function() {
resultContainer.html('');
console.log(data.length);
for (var x in data) {
if (!data[x].price)
data[x].price = 'kA';
if (!data[x].img)
data[x].img = 'assets/images/no.gif';
var html = '<div class="res-container">';
html += '<h2>'+data[x].Title+'</h2>';
html += '<img src="'+data[x].img+'">';
html += '<h3>Price: '+data[x].price+'</h3>';
html += '</div>';
resultContainer.append(html);
}
resultContainer.fadeIn('fast');
});
}
});
};
});
in search.php data is in simple echo.. how to get data from search.php and show here..
sorry for bad english
First,
you shouldn't concatenate your parameters but use a hashmap:
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
data: {
q : value,
category : cat,
country : country,
page : page }
As your method is (type: 'GET'), just use the ($_GET[param] method) in the php file
<?php
$value = htmlentities($_GET['q']);
$category = htmlentities($_GET['category ']);
$country = htmlentities($_GET['country ']);
In the js callback function, this is how you log the whole response ('something' is a tag) :
success: function(data){
var $xml = $(data);
console.log($xml); // show the whole response
console.log($xml.find('something')); // show a part of the response : <something>value</something>
});
It is a bit hard to understand what your problem is but my guess is that you need to json encode the data before echoing it back in search.php.
simplified example......
eg.
<?php
$somevar = $_GET['a']
$anothervar = $_GET['b']
//Do whatever
$prepare = array('a'=>$result1,'b'=>$result2) //etc..
$json = json_encode($prepare);
echo $json;
exit();
?>
Then you can access the results in the javascript with:
success: function(data){
var obj = $.parseJSON(data);
alert(data.a);
$("#some_element").html(data.b);
}