#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Shady Grove
    Posts
    98
    Rep Power
    17

    using ajax to display result from php script


    Hi All, my first foray in to ajax and I'm trying to get ajax to display a value from a php script call. I've looked at other scripts and not been able to figure out what I'm doing wrong
    So any help with explanations would be great

    Ajax
    [JavaScript]
    function GetName(){
    $.ajax({
    method: "POST",
    url: '_JQuery/script.php',
    data: [ClientID: 'Value here', Bot_Glue: ''],
    dataType: 'html',

    success: function(data) {
    $('#result').html(data);
    alert (data);
    }
    });
    }
    [/JavaScript]

    PHP script
    PHP Code:
    if (empty($_POST['ClientID'])) {
        echo 
    'hello we have success!';
        exit;
    } else {
        echo 
    'Not the result we wanted';

  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,286
    Rep Power
    4194
    Your javascript code is invalid, specifically:
    Code:
    data: [ClientID: 'Value here', Bot_Glue: ''],
    [...] denotes an array which takes a simple list of values, but your trying to do name/value pairs.
    {...} denotes an object and is what you want. It's what allows for a list of name: value pairs

    Code:
    function GetName(){
        $.ajax({
            method: "POST",
            url: '_JQuery/script.php',
            data: {ClientID: 'Value here', Bot_Glue: ''},
            dataType: 'html',
            success: function(data) {
                $('#result').html(data);
                alert (data);
            }
        });
    }
    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,966
    Rep Power
    9647
    Use your browser's Javascript console to look for errors.

    Or that.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Typically something like:

    var someVariable;

    $.ajax({
    type: "POST",
    url: 'scripts/get_images.php',
    dataType: "json",
    data: {
    PostsomeVariable: someVariable;
    },
    success: function(data) {
    $('#result').html(data);
    }
    });
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    And in your PHP script, grab the value of the lavue passed by using:

    $PostsomeVariable = $_POST['PostsomeVariable'];
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    And in your PHP script, grab the value passed by PostsomeVariable using:

    $PostsomeVariable = $_POST['PostsomeVariable'];

IMN logo majestic logo threadwatch logo seochat tools logo