jquery ajax posting - php

Hey all, I cannot seem to get a simple ajax working between a php page and jquery. I have followed a few tutorials. but having trouble actually understanding what is actually going on.
Javascript (JQuery)
$(function(){
$('#trigger').click(function(){
askajax;
});
function askajax(){
$.post("ask.php",{ question: "canuseeme" } ,function(data)
{
if(data=='yes')
{
alert("answer is yes");
}
else
{
alert("answer is no");
}
});
}
});
The PHP:
if($_POST['question']){
echo "yes";
} else {
echo "no";
}
Any ideas guys?
EDIT: THANKS TO #Jacob
$('#trigger').click(function(){
alert("doing ajax"); // THIS SHOWS
$.post("ask.php",{ question: "canuseeme" }, function(data)
{
if(data=='yes')
{
alert("answer is yes"); // THIS DOESNT
}
else
{
alert("answer is no"); // THIS DOESNT
}
});
});

This'll work:
$('#trigger').click(askajax);
Or this:
$('#trigger').click(function() {
askajax();
});
But I prefer to do it this way:
$(function(){
$('#trigger').click(function(){
$.post("ask.php",{ question: "canuseeme" } ,function(data)
{
if(data=='yes')
{
alert("answer is yes");
}
else
{
alert("answer is no");
}
});
});
});
Reason being that you don't ever use the askajax function again, and it will only be defined in the ready function's scope, so why even bother with a standalone function definition? Just use an anonymous function and be done with it. :)

On line 3, change askajax; to askajax();

Related

Ajax in codeigniter always returns false

I need to compare two datetime objects but the ajax ain't working.
javascript
<script>
$(document).ready(function(){
$('.datepicker').change(function(){
var date=$(this).data('datepicker').getFormattedDate('yyyy-mm-dd');
$(this).attr('value',date);
});
$('#collectDate').change(function(){
var collectDate=$('#collectDate').val();
var expiryDate=$('#expiryDate').val();
$.post("index.php?ajaxController/fine",{collectDate:collectDate,expiryDate:expiryDate},
function(data){
if(data){
$('#fine').hide();
$('#collectFine').removeAttr('required');
}
else if(!data){
$('#fine').show();
$('#collectFine').attr('required');
}
});
});
$('#fine').hide();
});
</script>
Controller
<?php
class ajaxController extends CI_Controller{
public function fine() {
$data['collectDate']= $this->input->post('collectDate');
$data['expiryDate']= $this->input->post('expiryDate');
$this->load->view("backend/admin/fine",$data);
}
}
View
<?php
$collectDate=$collectDate;
$date= explode('/',$expiryDate);
$expiryDate1=new DateTime($date[2]."-".$date[0]."-".$date[1]);
$diff=$collectDate->diff($expiryDate);
if($diff>=0){
echo true;
}
else {
echo false;
}
I've also tried simple comparison operators but all in vain. It always executes the else part in success function.
It always returns false because you cannot really catch true/false in php code with ajax. What that code in your view does, is make some calculations, return true or false AND THEN RENDER AN HTML PAGE, an empty one in your case.
Calling that page with ajax, means you read the HTML, which if an empty page, means no data returned, so FALSE.
What you need to do is something like this:
In your view:
if($diff>=0){
echo 'true';
}
else {
echo 'false';
}
And in your jQuery:
$('#collectDate').change(function(){
var collectDate=$('#collectDate').val();
var expiryDate=$('#expiryDate').val();
$.post("index.php?ajaxController/fine",{collectDate:collectDate,expiryDate:expiryDate},
function(data){
if(data.indexOf('true') > -1){
$('#fine').hide();
$('#collectFine').removeAttr('required');
}
else if(data.indexOf('false') > -1){
$('#fine').show();
$('#collectFine').attr('required');
}
});
});
This should be fine.

Send radio button value to PHP from form

Trying to learn how to do an autocomplete. I have the following in my html file (the code pasting function won't work so I have an image:
Between the head tags I have the following function:
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("autocomplete_script.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
I want to send the value of the checked radio button to autocomplete_script.php so I can do an if else within the php. I can't find any examples that are helping me, I though this would be an easy one. Can anyone help?
form.serialize() function gets all the data from the form, then you send it to the autocomplete_script.php, your lookup script looks something like this.
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.ajax({
url:'autocomplete_script.php',
type:'POST',
data:$("form").serialize(),
success:function(data)
{
//SUCCESS do your thing, response as data
}
});
}
} // lookup
Hope It Helps

jquery post on document ready

All i want is, a data will pass into this php file: check.php and this php file will pass data into that function when the page is ready. this is just an expirement.
ok I have this code:
$(document).ready(function() {
$.post("check.php", {
name: "log"
}, function(data) {
if (data === "yes") {
alert('has been login');
} else {
alert('has not login');
}
});
});​
and i have this php code ("check.php") where the jquery post will pass the data into.
​
<? //check
$chk = $_POST['name'];
if (isset($chk === "log")) {
if (isset($_COOKIE['admin'])) {
if ($_COOKIE['admin'] === "login") {
echo "yes";
}
} else {
echo "no";
}
} else {
echo "no";
}
?>
but nothing happen, even i change the whole code in check.php into: "echo "yes";" theres nothing happen, i guess, its either there is no data receive or there is no data pass. Hope someone could help me into this simple and cute problem that im stuck with. thank you in advance
I think you missed a closing brace. This just works as expected:
$.post('check.php', { name: 'log' }, function(data) {
if (data === "yes")
{
alert ('has been login');
}
else
{
alert ('has not login');
}
});
Run this JSFiddle http://jsfiddle.net/S53k5/
and you will see the call passing by in Fiddler.
I would guess it's because you're using the header "name" and that is a reserved keyword.

how Mark Checkboxes after create a dynamic form in PHP using JQuery,

I am trying to check some checkboxes based on it value.
In my code, I can update the database using this ckbxs, but when I try load the updated date, the status is showed as uncheck.
So, I am trying to read the html, after create the form, to check the values and then check the ckbx.
What can I do here to fix it?
Thanks in advance
$(document).ready(function(){
$('input[type=checkbox]').each(function () {
if (this.value == 1) {
(function(){this.attr('checked', true);})
}
} )
})
You need to enclose the thiss in a $( ) and change value to val().
$(document).ready(function(){
$('input[type=checkbox]').each(function () {
if ($(this).val() == 1) {
$(this).attr('checked', true);
}
});
});
$(document).ready(function () {
$('input[type="checkbox"][value="1"]').each(function () {
$(this).attr('checked', true);
});
});
Thanks Willian, now I am using a onChange and I got what I need...
function changerCB(idCheck){
$(document).ready(function() {
$.post('ajaxUpdate.php', {
id: document.getElementById(idCheck).id,
value: document.getElementById(idCheck).checked?1:0
}, function(data){
$('#display').html(data.returnValue)
if(data.success) {
} else {
}
}, 'json');
return false;
});
}

How to change a div when user click on Twitter Follow button #anywhere

I am looking to change a DIV when I know a user is following me.
I cant get the code to fire the alerts.
The follow button does show up, but after i click and follow, nothing happens.
twttr.anywhere(function (T) {
T("#followplaceholder").followButton('twitterapi');
function follow(){
T.currentUser.isFollowing('twitterapi'), function(e){
if (e == true){
alert("following");
} else {
alert("not following");
}
};
}
});
Any help would be great!
Here is the code i am trying to use now. Where do i call the function to get it fired? Or should i just take the function part out?
twttr.anywhere(function (T) {
T("#followplaceholder").followButton('twitterapi');
function follow(){
T.currentUser.isFollowing('twitterapi'), function(e){
if (e == true){
alert("following");
} else {
alert("not following");
}
};
}
});
you have an extra ) after 'twitterapi'
try this:
function follow(){
T.currentUser.isFollowing('twitterapi', function(e){
if (e == true){
alert("following");
} else {
alert("not following");
}
};
}
Is the follow funcion even triggered? It's just standing there doing nothing?
And like the others sad there is a piece of PHP inthere..., it has to be a ) i think.
Also watch your console log for errors.
T.currentUser.isFollowing('tweetapi']?>', function(e){
A straight-forward bug. ?> is php code, so you might want to use ) instead of ]?>

Categories