Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-19-2012, 09:02 PM   PM User | #1
waps2
New Coder

 
Join Date: Aug 2002
Posts: 86
Thanks: 2
Thanked 1 Time in 1 Post
waps2 is an unknown quantity at this point
ajax call to php script

Hi Guys,

Im new to ajax so need some help.

I tried to create the code below to call to a php file and us the result given back to perform an action. But its not working ??

The code is

Code:
<script type="text/javascript"><!--
$('#button-cart').bind('click', function() {
$.ajax({
            action:'checksess.php',
            cache:false,
            success:function(data){
                // Do something with the result
                if(data=="true"){
                    $('#product_data').hide();
                }else{
                    $('#product_data').show();
                }
            }
        );

});
//--></script>

and my php code in the file is:

PHP Code:
<?php  session_start() if(isset($_SESSION['hiredate'])) { echo "true"; } ?>
Can you see what is wrong with it please ?

waps2 is offline   Reply With Quote
Old 01-19-2012, 10:09 PM   PM User | #2
waps2
New Coder

 
Join Date: Aug 2002
Posts: 86
Thanks: 2
Thanked 1 Time in 1 Post
waps2 is an unknown quantity at this point
Ok so I got that working now.

How do I activate another ajax call on success of this one ??

Code:
$('#button-cart').bind('click', function() {
$.ajax({
			url: 'checksess.php',
            cache:false,
            success:function(data){
				if (data.msg == 'yes') {
                    $('#product_data').hide(); /// want to activate another ajax call now here 
                }else{
                    $('#date_data').hide();
                }
            }
        });

});
waps2 is offline   Reply With Quote
Old 01-20-2012, 07:42 AM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
By starting $.ajax() inside of the success callback of the other one

Code:
$.ajax({
			url: 'checksess.php',
            cache:false,
            success:function(data){
				if (data.msg == 'yes') {
                    $('#product_data').hide(); /// want to activate another ajax call now here 
                }else{
                    $('#date_data').hide();
                }

                // start next one here
                $.ajax(.....);
            }
        });

});
devnull69 is offline   Reply With Quote
Old 01-20-2012, 09:30 PM   PM User | #4
waps2
New Coder

 
Join Date: Aug 2002
Posts: 86
Thanks: 2
Thanked 1 Time in 1 Post
waps2 is an unknown quantity at this point
Ok thanks

Another bit im stuck with now is passing data from a form input field to a named function within my php file.

This is all the code I have so far, but as Im new to it not sure if its the right way.

The script is:

Code:
$('#add-dates').bind('click', function() {
$.ajax({
			url: 'checksess.php',
			action: 'addhiredates',
			type: 'post',
            cache:false,
			data: $('.datepicker input[type=\'text\']'),
            success:function(data){
				if (data == 'yes') {
                    add();
					$('#date_data').hide();
                }else{
                    $('#product_data').show();
					$('#date_data').show();
                }
            }
        });

});
My php code is:
PHP Code:
function addhiredates() {
if (isset(
$this->request->post['hiredate'])) {
$hiredate 'yes'; }
echo 
'yes';

And the html code is:
Code:
  <div id="datepicker" class="datepicker">
  <input type="text" id="acdate" name="hiredate" class="hasDatepicker">
  <a id="add-dates" class="button-cart"></a>
  </div>
waps2 is offline   Reply With Quote
Old 01-20-2012, 10:03 PM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
This
Code:
data: $('.datepicker input[type=\'text\']')
is a jQuery object and NOT the value of this object. Try this
Code:
data: {"data" : $('.datepicker input[type=\'text\']').val()}
You can access the data inside PHP using $_POST["data"]
devnull69 is offline   Reply With Quote
Old 01-20-2012, 11:50 PM   PM User | #6
waps2
New Coder

 
Join Date: Aug 2002
Posts: 86
Thanks: 2
Thanked 1 Time in 1 Post
waps2 is an unknown quantity at this point
Hi devnull69 thanks for your help.

How do I target a php function in my file ?? The code I have built so far is not working.

Code:
$('#add-hiredates').bind('click', function() {
$.ajax({
			url: 'checksess.php?test=adddates',
			type: 'post',
            cache:false,
			data: {"data" : $('.datepicker input[type=\'text\']').val()},
            success:function(data){
				if (data == 'yes') {
					$('#product_data').show();
					$('#date_data').hide();
					additemtosession();
                }else{
                    $('#product_data').show();
					$('#date_data').show();
                }
            }
        });

});
PHP Code:
$destination $_GET['test'];
switch (
$destination){
    case 
"adddates":
        
addhiredates();
        break;    
}

function 
addhiredates() {
$hiredate $_POST['data'];
if (!isset(
$hiredate) || ($hiredate '')) {
echo 
'no';
}
else {
echo 
'yes';
}

waps2 is offline   Reply With Quote
Old 01-21-2012, 11:03 AM   PM User | #7
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Hm you are mixing a POST with a GET request ... I don't know where this will end up. Either make it a full GET request or add the test parameter to the "data" parameter list for the POST request.

Generally your code should work ... it should either give you "no" or "yes". What does it give you?
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:54 AM.


Advertisement
Log in to turn off these ads.