Results 1 to 10 of 10

Thread: Ajax with PHP

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default Ajax with PHP

    I am not entirely sure where to post this but i have a sample code. I am trying to use ajax to get information from a php page. However, I think I am either doing something wrong with the php or AJAX (Mostly the ajax I'm guessing).

    Sample php page called ip.php:
    PHP Code:
    $advert = array(
            
    'ajax' => 'Hello world!',
            
    'advert' => 'adverts'
         
    );
        echo 
    json_encode($advert); 
    Sample ajax with html:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Get Ping</title>
    <script src="../js/jquery.js"></script>
    <script>
    $(function(){
    	$.ajax({
            url : 'ip.php',
            type : 'POST',
            data : data,
            dataType : 'json',
            success : function (result) {
               $('#check').text(result['ajax']) // "Hello world!" displayed
            },
            error : function () {
               $('#check').text("error");
            }
        })
    })
    </script>
    </head>
    <body>
    
    <div id="check"></div>
    
    </body>
    </html>
    Any help would be wonderful.
    Thanks.
    Last edited by Deadweight; 05-09-2014 at 06:45 AM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Get rid of the highlighted:

    Code:
    $(function(){
    	$.ajax({
            url : 'ip.php',
            type : 'POST',
            data : data,
            dataType : 'json',
            success : function (result) {
               $('#check').text(result['ajax']) // "Hello world!" displayed
            },
            error : function () {
               $('#check').text("error");
            }
        })
    })
    Next time use your error console, it identified that line right away.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Ah yeah thanks; however, it still out puts 'error' on the webpage instead of Hello World
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not here. I get Hello World! (I'm using wamp, so I cannot link you to a demo, sorry.)

    Your php code is invalid without php tags though, just saying. If you use ip.txt:

    {"ajax":"Hello world!","advert":"adverts"}
    for the url it will work. (jQuery version 1.8.3)



    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
    Last edited by jscheuer1; 05-08-2014 at 07:43 PM. Reason: detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Website URL: http://thebcelements.com/dhtml_test/.
    Also there are php tags
    The whole php page
    PHP Code:
    <?php

    $advert 
    = array(
            
    'ajax' => 'Hello world!',
            
    'advert' => 'adverts',
         );
        echo 
    json_encode($advert);
    ?>
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    On ip.php, get rid of everything except the PHP. That means remove:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pinger</title>
    
    </head>
    <body>
    and:

    HTML Code:
    </body>
    </html>
    If you don't have anything like that in the file, the server must be adding it. I doubt that though. Could happen I guess.

    And, although it might not be necessary, remove any leading or trailing whitespace. Here is the full code of my ip.php file:

    PHP Code:
    <?php
    $advert 
    = array(
            
    'ajax' => 'Hello world!',
            
    'advert' => 'adverts'
         
    );
        echo 
    json_encode($advert);
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Deadweight (05-08-2014)

  8. #7
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Thanks again. If i wanted to send data to the php i would add data back and send it to the php page how would i do that?
    EG add this to the php file:
    PHP Code:
    $ip =  $_REQUEST["ip"]; 
    and this to the jquery:
    data: {ip:"0.0.0.0"} //I know this is wrong.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's OK (using a valid object). You can also use a string (its syntax is like a query string without the preceding ? character):

    Code:
    data: 'ip=0.0.0.0'
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #9
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Okay I've tried that and I am receiving another error.

    HTML URL: http://thebcelements.com/dhtml_test/
    PHP URL: http://thebcelements.com/dhtml_test/ip.php

    JQuery:
    Code:
    $(function(){
    	$.ajax({
            url : 'ip.php',
            type : 'POST',
    	data: "ip=www.google.com",
            dataType : 'json',
            success : function (result) {
               $('#check').text(result['ajax']) // "Hello world!" alerted
            },
            error : function () {
               $('#check').text("error");
            }
        })
    })
    PHP:
    PHP Code:
    <?php
    $ip 
    $_REQUEST["ip"];
    //$ip = "108.163.135.166";
    exec("ping -n 5 $ip"$output$status);

    $check 0==$status?"Success":"Failed";

    $advert = array(
            
    'ajax' => $ip.' - '.$check
         
    );
         
        echo 
    json_encode($advert);
        echo 
    print_r($output);
        echo 
    '<br />'.$ip.' - '.$check.' - '.$status;
    ?>
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  11. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you are instructing jQuery to make the AJAX request one of dataType json, ip.php must output a valid json string and only a valid json string. It cannot if it includes any preceding or trailing output that is not a part of the valid json string. In this particular case, the following must be removed:

    Code:
        echo print_r($output); 
        echo '<br />'.$ip.' - '.$check.' - '.$status;
    If you want to keep that code there for diagnostic purposes, it has to be turned off somehow during the actual AJAX json request. Or only turned on if - say a diagnostic param is passed to the file.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  12. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Deadweight (05-09-2014)

Similar Threads

  1. Resolved best practice login (secure data) via ajax - POST JSON AJAX
    By lse123 in forum JavaScript
    Replies: 12
    Last Post: 01-01-2013, 12:22 AM
  2. Replies: 5
    Last Post: 08-09-2012, 12:10 PM
  3. Dynamic Ajax Content & Ajax tabs running slowly on IE
    By Monclee in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 03-12-2012, 12:32 AM
  4. DHTML CrossBrowser Ajax Simulator - Ajax-style photo uploading example
    By diltony in forum Submit a DHTML or CSS code
    Replies: 4
    Last Post: 01-08-2012, 12:07 PM
  5. [Ajax] Ajax Messages (Ajax Demonstration)
    By iMarc in forum Submit a DHTML or CSS code
    Replies: 2
    Last Post: 03-22-2007, 03:31 AM

Bookmarks

Posting Permissions

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