0

I have create a form on a wordpress page, and I'm trying to pass the values of the form to a php page, so I can send an email with that information, but I don't know how to create the php page, in what folder do I have to save the page.

Thank you (fist time using wordpress);

This is my form: And I have a different page(send-email) that will collect the data.

I know how to do the code for the send-email page..I just nee dto know where to save the php page.

<form method="get" action="send-email.php" name="emailForm" onsubmit="return validate()">   
    <div class="form-group"> 
        <label for="name">Name<span id="req">*</span></label><br> 
        <input type="text" class="form-control" id="name" name="name" placeholder="Quantum Management" value=""> 
    </div>

    <div class="form-group">
        <label for="tel">Phone Number<span id="reqTel">*</span></label><br>
        <input type="text" class="form-control" id="tel" name="tel" placeholder="(999) 123-4567" value=""  >
    </div>

    <div class="form-group">
        <label for="email">Email<span id="reqEmail">*</span></label><br>
        <input type="text" class="form-control" id="email" name="email" placeholder="example@example.com" value="" >
    </div>

    <div class="form-group">
        <label for="questions">Questions<span id="reqQuest">*</span></label><br>
        <textarea class="form-control" rows="7" cols="8" id="questions" name="questions" placeholder="Questions" ></textarea>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-default" name="submit" value="yes">Submit</button>
    </div>
</form>  
0

Here I've added a this hidden input <input type="hidden" name="action" value="your_form_task"> and updated the post URI to <?php echo esc_url( admin_url('admin-post.php') ); ?>.

Here is the updated form-

<form method="get" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" name="emailForm" onsubmit="return validate()">
    <div class="form-group">
        <label for="name">Name<span id="req">*</span></label><br>
        <input type="text" class="form-control" id="name" name="name" placeholder="Quantum Management" value="">
    </div>

    <div class="form-group">
        <label for="tel">Phone Number<span id="reqTel">*</span></label><br>
        <input type="text" class="form-control" id="tel" name="tel" placeholder="(999) 123-4567" value=""  >
    </div>

    <div class="form-group">
        <label for="email">Email<span id="reqEmail">*</span></label><br>
        <input type="text" class="form-control" id="email" name="email" placeholder="example@example.com" value="" >
    </div>

    <div class="form-group">
        <label for="questions">Questions<span id="reqQuest">*</span></label><br>
        <textarea class="form-control" rows="7" cols="8" id="questions" name="questions" placeholder="Questions" ></textarea>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-default" name="submit" value="yes">Submit</button>
    </div>
    <input type="hidden" name="action" value="your_form_task">
</form>

Note that it is very important. Now here is the function where you can put your logics to handle form data-

function the_dramatist_form_post_to_admin() {
    /**
     * At this point, $_GET/$_POST variable are available
     *
     * We can do our normal processing here
     */

    // Sanitize the POST field
    // Generate email content
    // Send to appropriate email
    // Or do whatever you want to do.
}
add_action( 'admin_post_nopriv_your_form_task', 'the_dramatist_form_post_to_admin' );
add_action( 'admin_post_your_form_task', 'the_dramatist_form_post_to_admin' );

Look here is another important thing, after admin_post_nopriv_ and admin_post_ I've glued the your_form_task string what we've declared as hidden input value in the form with input name action. It's actually pointing the form to be submitted in this function. And admin_post_nopriv_{$action} fires on a non-authenticated admin post request for the given action where admin_post_{$action} fires on an authenticated admin post request for the given action. Here $action is your_form_task. Hope that helps.

0

Usually, within WordPress you're not creating an entire page in code, but creating templates that WordPress uses to build the pages. You could theoretically point this form at any other page on the site, and write the code that handles the form data in the functions.php file of your active theme or in a plugin.

But I wouldn't bother with all that work, when you can just use a pre-made solution. This sounds like the exact use case for Contact Form 7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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