最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How can I process a form submission from an HTTP POST request?

programmeradmin3浏览0评论

I have the following form in HTML on the front-end of a WordPress website which I am trying to convert from a website that is currently built in NodeJS.

<form id="regForm" action="/submit-form" method="POST">
  <label for="name">Name</label>
  <input type="text" id="name" name="name" placeholder="Enter your name">
  <label for="email">Email address</label>
  <input type="email" id="email" name="email" placeholder="Enter your email address">
  <input type="submit" class="submit-button" value="Submit">
</form>

How do I build a handler for the POST request in WordPress? The below is my current attempt but this redirects to my Error 404 page. It is in my function.php file.

function form_submission() {
    // Check if form is submitted
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        // Sanitize input
        $name = sanitize_text_field($_POST['name']);
        $email = sanitize_email($_POST['email']);

        // Perform actions, e.g., send an email or store data
        //wp_mail(get_option('admin_email'), 'New Form Submission', "Name: $name\nEmail: $email\nMessage: $message");
        
        // Redirect after processing
        wp_redirect(home_url('/verify-email'));
        exit;
    }
}
add_action('form-submission', 'form_submission'); 

I have the following form in HTML on the front-end of a WordPress website which I am trying to convert from a website that is currently built in NodeJS.

<form id="regForm" action="/submit-form" method="POST">
  <label for="name">Name</label>
  <input type="text" id="name" name="name" placeholder="Enter your name">
  <label for="email">Email address</label>
  <input type="email" id="email" name="email" placeholder="Enter your email address">
  <input type="submit" class="submit-button" value="Submit">
</form>

How do I build a handler for the POST request in WordPress? The below is my current attempt but this redirects to my Error 404 page. It is in my function.php file.

function form_submission() {
    // Check if form is submitted
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        // Sanitize input
        $name = sanitize_text_field($_POST['name']);
        $email = sanitize_email($_POST['email']);

        // Perform actions, e.g., send an email or store data
        //wp_mail(get_option('admin_email'), 'New Form Submission', "Name: $name\nEmail: $email\nMessage: $message");
        
        // Redirect after processing
        wp_redirect(home_url('/verify-email'));
        exit;
    }
}
add_action('form-submission', 'form_submission'); 
Share Improve this question asked Feb 15 at 13:45 Eind999Eind999 101 1
  • Make sure not to use reserved names for your form fields. name and email are used internally already and will always lead to a 404. – fuxia Commented Feb 15 at 17:58
Add a comment  | 

1 Answer 1

Reset to default 0

you can use admin-post.php to process the form.

you can set the form like that :

<form
    action="<?= htmlspecialchars(admin_url("admin-post.php?action=MY_PLUGIN__my_action"))?>"
    method="POST"
>
    
    <button name="send">go</button>
    
</form>

and then you link the code with that :

add_action("admin_init", function () {
    
    if (    !isset($_GET["action"])
        ||  ("MY_PLUGIN__my_action" !== $_GET["action"])
    ) {
        return;
    }
    
    
    /*  */
    // debug
    var_export($_POST);
    exit();
    /*  */
    
    
    // data processing
    //...
    
    
    // redirection at the end
    wp_redirection(home_url("done"));
    exit();
    
});
发布评论

评论列表(0)

  1. 暂无评论