Submit Form With Action=“file.php” With No Reload?

2020-02-16 02:48发布

I'm working on a form for my website. The markup for the form tag looks like:

<form method="post" action="contactengine.php">

The form sends an email to me using action="contactengine.php" with the form information when a user submits it. Because of the page layout I have, however, I can't have the page reload when a user submits the form. How can I work around this issue?

标签: php html forms
2条回答
放我归山
2楼-- · 2020-02-16 03:14

Are you able to use javascript + jquery? Posting asynchronously is fairly simple with jquery.

$('#formid').submit(function({ //listens for form submission

    $(this).preventDefault(); //stops the submission

    $.post('actionPage.php', $(this).serialize, function(response){
       //posts the data to actionpage.php, with the form data, and then runs a function handling the response.

    });


}));
查看更多
放我归山
3楼-- · 2020-02-16 03:28

Have the server respond with a 204 No Content response (spec).

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view

Such:

<?php
http_response_code(204);
exit();
查看更多
登录 后发表回答