Hamed
Hamed

Reputation: 313

jquery - post() method gets `not found` error

I want to send an email address to my submit.php as post and receive a response with jquery.

submit.php and index.html are in website root and script.js is in 'js' folder.

my index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
<input id="email" type="email" placeholder="  Email"><br>
<a href=""><button id="submit-btn">send</button></a>
<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous">
</script>
<script src="js/script.js"></script>
</body>
</html>


my script.js:

$(document).ready(()=>{
    $("#submit-btn").click(()=>{
        $.post("../submit.php", { user_email: $("#email").val()})
            .done((data)=>{
                alert(data);
            })
            .fail((xhr, status, error)=>{
                alert(error);
            });
    });
});

my submit.php:

<?php
$email = (isset($_POST['user_email']) ? $_POST['user_email'] : "nothing");
echo $email;

I expect the output to be my email address, but the actual output is not found. I think my request has failed

Upvotes: 1

Views: 168

Answers (1)

Jan
Jan

Reputation: 2853

Since you say that both files are in the same folder, there is no need for the ../ in front of your request.

$.post("../submit.php", { user_email: $("#email").val()})
$.post("submit.php", { user_email: $("#email").val()})

Upvotes: 1

Related Questions