Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    86
    Thanks
    2
    Thanked 1 Time in 1 Post

    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 ?


  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    86
    Thanks
    2
    Thanked 1 Time in 1 Post
    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();
                    }
                }
            });
    
    });

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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(.....);
                }
            });
    
    });

  • #4
    New Coder
    Join Date
    Aug 2002
    Posts
    86
    Thanks
    2
    Thanked 1 Time in 1 Post
    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>

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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"]

  • #6
    New Coder
    Join Date
    Aug 2002
    Posts
    86
    Thanks
    2
    Thanked 1 Time in 1 Post
    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';
    }


  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •