0

I'm trying to send over the data from my textarea using an ajax call to my PHP file, but we aren't getting any response back from the PHP file.

<textarea id="area" cols="70" rows="30"></textarea>
<button id="submit">Submit</button>

<script>
$('#submit').click(function (e) {
    e.preventDefault();

    var info = $('#area').val();
    $.ajax({
        type: "POST",
        url: 'pages/assignments/response.php',
        data: {area: info}
    });
});
</script>
<?php
    if (!empty($_POST['area'])) {
        $success = json_encode('succes');
        return $succes;
    };
?>

-- # Answer # --

I thought I had already tried an echo in this piece of code, but I think I just missed the output on the webpage and thought it wasn't working.

<?php
    if (!empty($_POST['area'])) {
        echo "success";
    };
?>
5
  • Add : , success : function(response) { console.log(response); }in your ajax call after the data part, what do you get? If you still have nothing, try echo $success in your php Commented Sep 18, 2018 at 7:48
  • 1
    Check the browser network tab for a 404. It could be that the path/url to your php file is incorrect if it "never reaches your php file". Commented Sep 18, 2018 at 7:53
  • Thanks!, The echo did the work! Commented Sep 18, 2018 at 7:54
  • So it was reaching your php?? and "it never reaches my php file" was just to try and fool us? Commented Sep 18, 2018 at 7:55
  • We thought we tried an echo before but i guess we never did. Commented Sep 18, 2018 at 7:58

2 Answers 2

2

Thanks Mickael for the answer!

I completely forgot to add an echo to my code.

<?php
    if (!empty($_POST['area'])) {
        $succes = json_encode('succes');
        echo $succes();
    };
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Nope because json_encode function encodes array data or object
please try my answer.
0

Your ajax post :

$('#submit').click(function (e) {
    e.preventDefault();

    // information to be sent to the server
    var info = $('#area').val();
    $.ajax({
        type: "POST",
        url: 'pages/assignments/response.php?return=1',
        data: {area: info},
        dataType:'json',
        success:function(r){
         console.log(r);
        }
    });
});

and your php response will be like

Php file:
<?php
if ( $_GET['return'] == 1 && isset($_GET['return']) ) {
    echo json_encode('succes');
    exit;
};
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.