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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    907
    Thanks
    301
    Thanked 2 Times in 2 Posts

    Question Passing an expression via ajax

    Hi Fellow Developers!

    Let me explain you my problem with a simple example:

    I have 3 files. Index.html, script.js, ajax.php. The contents of these files are as follows:

    index.html
    Code:
    <html>
    <head>
    <script src="jquery.js"></script>
    <script src="script.js"></script>
    <script>
       function addition(exp){
           
         alert( eval(exp) );
    
       }
    </script>
    </head>
    
    <body>
    
    </body>
    
    </html

    script.js
    Code:
                $.ajax({
                    url :  'ajax.php',
                    dataType: 'script',
                    type: 'post',
                    success: function(data){
    
                     addition(data);
    
                    }
                });

    ajax.php
    Code:
    echo "5+10";
    die;
    What I want is based on the output of ajax.php file, the addition function should alert the number 15.

    Can someone pls tell me what am I missing in above code?

    Thanks

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    you call the ajax before you define the callback...

    put your inline script first or move addition() to script.js and it will work (as long as your php does)
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    phantom007 (04-23-2013)

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    907
    Thanks
    301
    Thanked 2 Times in 2 Posts
    HI

    I noticed that it works if I change the content of ajax.php

    From:
    Code:
    echo "5+10";
    die;
    To:
    Code:
    5+10
    Anyone knows why? and yes i m using a webserver on my localhost.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by cancer10 View Post
    HI

    I noticed that it works if I change the content of ajax.php

    From:
    Code:
    echo "5+10";
    die;
    To:
    Code:
    5+10
    Anyone knows why? and yes i m using a webserver on my localhost.
    then php is not working, it's just serving the file.
    or, perhaps you don't have the php tags? ( <?php and ?> )
    i assumed you cleaned them up before posting...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    phantom007 (04-23-2013)

  • #5
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    907
    Thanks
    301
    Thanked 2 Times in 2 Posts

    Question

    Yes you are right, i had removed the php tags in ajax.php file.

    also, what if I have something like this in the ajax.php file:

    Code:
    var d1 = new Date('March 17, 2013 09:22:06');
    var d2 = new Date('March 20, 2013 22:52:43');
    var d3 = new Date('April 09, 2013 12:57:51');
    var d4 = new Date('April 17, 2013 18:03:27');
    var d5 = new Date('April 18, 2013 00:04:55');
    var d6 = new Date('April 22, 2013 21:29:20');
    data = [{'value' : 15, 'date' : d1}, {'value' : 1, 'date' : d2}, {'value' : 5, 'date' : d3}, {'value' : 7, 'date' : d4}, 
    {'value' : 4, 'date' : d5}, {'value' : 2, 'date' : d6}];
    How do I get the value of data in the addition() function?

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by cancer10 View Post
    Yes you are right, i had removed the php tags in ajax.php file.

    also, what if I have something like this in the ajax.php file:

    Code:
    var d1 = new Date('March 17, 2013 09:22:06');
    var d2 = new Date('March 20, 2013 22:52:43');
    var d3 = new Date('April 09, 2013 12:57:51');
    var d4 = new Date('April 17, 2013 18:03:27');
    var d5 = new Date('April 18, 2013 00:04:55');
    var d6 = new Date('April 22, 2013 21:29:20');
    data = [{'value' : 15, 'date' : d1}, {'value' : 1, 'date' : d2}, {'value' : 5, 'date' : d3}, {'value' : 7, 'date' : d4}, 
    {'value' : 4, 'date' : d5}, {'value' : 2, 'date' : d6}];
    How do I get the value of data in the addition() function?
    well, that's js, not php, but as long as "data=" went last, it should work with eval.

    since it's not an expression, i'm not sure what you want to do with the data.

    if you want to add the values, you need to refer to each slot on the array that should be added, then point to the property you want to add:

    Code:
      function addition(exp){
           var arr= eval(exp);
    
         alert( arr[0].value + arr[1].value );
    
       }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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