I am trying to update my database when the user clicks a certain link (links have different values, so when user clicks one link number goes up by 1, another by 2, and so on).
I need to submit a form to another page containing the data from #form with a variable behind it, but this function doesn't seem to be working.
JavaScript
function update_number(x) {
$.ajax({
type: "POST",
url: "update_likes.php",
data: $("#Form"+x).serialize(),
dataType: "json",
return false;
};
HTML
<input type='image' src='...' onclick'update_number(2);' />
Any help appreciated.
It seems that your JavaScript is not closed correctly.
Try changing your JavaScript with this:
function update_number(x) {
$.ajax({
type: "POST",
url: "update_likes.php",
data: $("#Form"+x).serialize(),
dataType: "json",
success: function(json) {
alert( json );
return false;
}
});
}
Related
I want to alert the return value from a php method, but nothing happens. Here is the ajax and php methods. Can anyone see what I am doing wrong?
--------------------------------------…
Ajax script
$.ajax({
type: 'get',
url: '/donation/junk/4',
data: datastring,
success: function(data) {
alert(data');
}
});
--------------------------------------…
php method
function junk($id)
{
return "works11";
}
in PHP, you can't simply return your value and have it show up in the ajax response. you need to print or echo your final values. (there are other ways too, but that's getting off topic).
also, you have a trailing apostrophe in your alert() call that will cause an error and should be removed.
Fixed:
$.ajax({
type: 'get',
url: '/donation/junk/4',
data: datastring,
success: function(data) {
alert(data);
}
});
PHP:
function junk($id)
{
print "works11";
}
You have an extra ' in there on the alert(data') line
This should work
$.ajax({
type: 'get',
url: '/donation/junk/4',
data: datastring,
success: function(data) {
alert(data);
}
});
And your PHP code should call the method also and echo the value
function junk($id) {
return 'works11';
}
exit(junk(4));
All you're doing currently is creating the method
ajax returns text, it does not communicate with php via methods. It requests a php page and the return of the ajax request is whatever the we babe would have showed if opened in a browser.
I want to make a follow/unfollow user on button click. However the current code only works until the page refreshes. If I refresh the page the follow text comes back. I want it to have unfollow when I refresh the page.
Do I need to have a Session for users in my PHP? Do I have to create a value for a button? Will it remember the click by multiple users?
<button class="user">Follow</button>
<p></p>
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'echocurrent.php',
success: function(data) {
$("p").text(data);
}
});
});
$('.user').click(function() {
var $this = $(this);
$this.toggleClass('user');
if ($this.hasClass('user')) {
//unfollows
$.ajax({
type: 'POST',
url: 'UnFollow.php',
success: function(data) {
$("p").text(data);
}
});
$this.text('Follow');
} else {
//follows
$.ajax({
type: 'POST',
url: 'follow.php',
success: function(data) {
$("p").text(data);
}
});
$this.text('UnFollow');
}
});
UPDATE :
I want to add this jquery to every user with a session in PHP.
How can I do that?
I'm assuming you're updating a value in "UnFollow.php" you should return this value in "echocurrent.php" and based on it show/hide the class "user"
Update:
add logic to in "echocurrent.php" to return both the number of followers and if the current user is following this user
$.ajax({
type: 'POST',
url: 'echocurrent.php',
success: function(data) {
$("p").text(data.followers);
if(data.isFollowing){
$this.toggleClass('user');
}
}
});
I have a php page which spits out a profile picture URL from a instagram account that is supplied. For example if you went..
.com/ajax_check_instagram.php?username=tom
the page would return
http://instagram.com/profilepic.png
I then have an ajax call which im trying to manipulate so the sucess function updates an image source.
//Add a loading image in the span id="availability_status"
var link = '';
$.ajax2({ //Make the Ajax Request
type: "POST",
cache: false,
dataType: "json",
url: "/ajax_grab_instagram.php", //file name
data: "username="+ username, //data
success: function(server_response){
link = server_response;
$("#profilepic").attr("src","link");
}
});
}
How would I format the server_response line and jquery line?
In your PHP/AJAX file you need to return the URL of the picture. Then in your jQuery AJAX request...
It's pretty much there as is except you have link encapsulated within double quotes.
$.ajax({
type: "POST",
cache: false,
dataType: "json",
url: "/ajax_grab_instagram.php",
data: "username="+ username,
success: function(response) {
$("#profilepic").attr("src",response);
}
});
I have changed it to response in my example.
link is a variable, not a string.
$("#profilepic").attr("src", link); // link should not be in quotes
Hey guys and gals,
I have a form that I would like to use AJAX to submit. For the callback function basically what I want to happen is, the form elements disappear (.hide) and are replaced by a div element (which will ready success or something of that nature). I then want the function to pause for 5 seconds and then redirect to a different URL.
The AJAX request successfully submits (as I receive an e-mail to the specified address as I should) but the callback function is not carrying out properly. Any suggestions?
$(document).ready( function () {
var $form = $('form');
function register($form) {
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
contentType: "application/json; charset=utf-8",
success: function(data){
$("#mc-embedded-subscribe-form").hide("slow");
$("#form").html('<div class="some-class">Some text that shows up upon successful AJAX request</div>')
setTimeout(5000);
$(".button").addEventListener("click", function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
})
}
})
}
}
**
UPDATE #1
**
This is the only error code that I am getting when I initially load the website (I've heard these errors only pop up in the dev tools and do not affect the user?)
But then when I submit the form I get this error code
I've also updated my JavaScript to the below code, taking advice from a few people who posted, it's still not working 100% though
$(document).ready(function() {
var $form = $('form');
$form.submit(function(){
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
success: function(data){
$("#mc-embedded-subscribe-form").hide("slow");
$("#form").html('<div class="some-class">Some text that shows up upon successful AJAX request</div>')
}
});
setTimeout(function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
}, 5000)
});
});
Joey
setTimeout usage is wrong.
See :
$(document).ready( function () {
var $form = $('form');
function register($form) {
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
contentType: "application/json; charset=utf-8",
success: function(data){
$("#mc-embedded-subscribe-form").hide("slow");
$("#form").html('<div class="some-class">Some text that shows up upon successful AJAX request</div>')
setTimeout(function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
}, 5000);
}
})
}
});
Further, the following should ideally be declared separately, if you want the user to click a link and go after ajax success.
$(".button").addEventListener("click", function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
})
HIi Folks i am having one issue please help me i want to send some data to my controller using ajax call i have written all code but for data field
<?php echo $this->Js->get('.menu')->event('click',$this->Js->request(array('controller' => 'restaurants', 'action' => 'getItem'),array('async' => true,'method'=>'POST','update' => '#HFNames','data'=>'$(this).attr(id)')),false); ?>
when ajax call hits it take "$(this).attr(id)" as a params but i need the value of cureent click
js helper genrate this
if we remove that double quote from this genrated script then its working
data: "$(this).attr(id)",
why this get quoted
<script type="text/javascript">
$(".menu").bind("click", function (event) {
$.ajax({
async: true,
data: "$(this).attr(id)",
dataType: "html",
success: function (data, textStatus) {
$("#HFNames").html(data);
},
type: "POST",
url: "\/foodkingkong\/restaurants\/getItem"
});
return false;
});
try this instead.
echo $this->Js->buffer('$(".menu").bind("click", function (event) {
$.ajax({
async: true,
data: $(this).attr(id),
dataType: "html",
success: function (data, textStatus) {
$("#HFNames").html(data);
},
type: "POST",
url: "/foodkingkong/restaurants/getItem"
});
return false;
});'
);
Hi Himanshu we had planned to do the same directly using jquery but wanted to do the same via cakephp bind function only.
What will be the difference if we pass the js in buffer.