Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to call and execute a segment of PHP code located on an external file. I understand the way of doing that involves jQuery. Here's is my code so far:

<html>
<head>
    <title>test</title>
</head>

<body>

    <script src="jquery.js"></script>
    <script language="javascript" type="text/javascript"> 
         $.post("ext.php", { name: "John", time: "2pm" })
        .done(function(data) {
          alert("Data Loaded: " + data); });

        $(document).ready(function(){
    $(this).load('ext.php');});

    </script>  

</body>

And here is the external file (ext.php):

<?php
echo $_POST['name'] . ' and- ' . $_POST['time'];
?>

The code should print the variables name and time on the screen (as well as in the popup window). The popup works fine, but the code doesn't print the veriables on the screen.

Thanks :)

share|improve this question
1  
ext.php is unequal ofek.php. In the callback you pass to $.post, insert something into the DOM. Why would you expect this to put the "variables on the screen"? –  Janus Troelsen Jun 4 '13 at 17:43
1  
Try $("body").load("ext.php"); Or better yet a specific element in the DOM. –  Dave Jun 4 '13 at 17:44

3 Answers 3

Forgive me if you know this already, but in case you don't:

You are using $.post() in your example, which is a streamlined form of the $.ajax() jQuery method. I suggest using this easier (but larger) format until you are very confident with AJAX. $.ajax() also allows you to do much more than the streamlined (quick) $.post method.

I added a couple of input boxes for you to type some information. Also, you need a place to put the stuff to appear on the screen, so I created a DIV for that.

Ajax posts data to an external php file, which processes the data it receives, and sends back an answer. It looks like this:

FILE #1:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {

                $('#mybutt').click(function() {
                    var nom = $('#yername').val();
                    var tim = $('#thetime').val();

                    $.ajax({
                        type: "POST",
                        url: "receiving_file.php",
                        data: 'name=' + nom + '&time=' +tim,
                        success:function(data){
                            alert('This was sent back: ' + data);
                            $('#report').html(data);
                        }
                    }); //END ajax

                }); //END click fn

            }); //END document.ready
        </script>
    </head>
<body>

Name: <input type="text" id="yername"><br>
Time: <input type="text" id="thetime"><br><br>
<input type="button" id="mybutt" value="Go">
<br><Br>
<div id="report"></div>

FILE #2: receiving_file.php

<?php
    echo 'You entered: ' . $_POST['name'] . ' and- ' . $_POST['time'];
share|improve this answer
    
I already flagged this as low quality because of its first version. :|now much better +1 :) –  hek2mgl Jun 4 '13 at 17:46

You can use post with load that way i think:

$(document).ready(function () {
    $(this).load('ext.php', {
        name: "john",
        time: "2pm"
    }, function (data) {
        $('body').append(data);
    });
});
share|improve this answer

Try this:

<script type="text/javascript"> 
    $(document).ready(function(){        
        $.post("ext.php", { name: "John", time: "2pm" }).done(function(data) { 
            alert("Data Loaded: " + data);
            $('body').html(data);
        });
    });
</script>  
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.