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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2006
    Location
    Fostoria, Ohio
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Ajax to PHP, strange issue

    Hey Guys,
    I have a strange issue that is going on here, and I am missing it. Let me lay down the example:

    I have an index.php:
    PHP Code:
        $postdata = isset($_POST['sysinfo']) ? $_POST['sysinfo'] : include('javascript.php');
       echo 
    "Returned from Javascript :".$postdata."<br /><br />"
    Basically this is testing POST['sysinfo'], if it is set, then it will equal the value and then it should display the results with the echo, if not it should include the javascript.php file.

    javascript.php:
    Code:
    <script type="text/javascript" src="js/jquery.js"></script>
    $.post("index.php", { sysinfo: 'HELLO WORLD'});
    This file should load jquery, and send POST back to the index.php with sysinfo='HELLO WORLD'

    Using Firebug, I see the POST data is this:
    Returned from Javascript :HELLO WORLD<br /><br />

    and the screen is blank, so basically it sent the POST back to the PHP file, although it is placing all of the file in POST, instead of just displaying it. Can someone point me in the direction of what I am doing wrong?

    Thanks,
    Jeff

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    $.post() is the method that executes the so called Ajax request which seems to run quite fine. But then your code fails to "do something" with the resulting output.
    Code:
    $.post("index.php", { sysinfo: 'HELLO WORLD'}, function(data) {
       // data contains the HTML output from the server script
       // show it inside an element on the current page with id="IdOfElement"
       $('#IdOfElement').html(data);
    });

  • #3
    New Coder
    Join Date
    Sep 2006
    Location
    Fostoria, Ohio
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That makes sense if I want to just display the data, although I want to work with it. With PHP, it data needs to be processed.

    $parsedata = explode("|", $postdata);
    # Gather all other collected data, and then add to database

    There is where the problem is coming in at. Any thoughts?


  •  

    Tags for this Thread

    Posting Permissions

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