Unable to post simple form data in HTML

I created simple contact me form and want to post form data to PHP page where data will be stored.

I am unable to figure out where to upload create_form.php file. Blow is my code.

<form name="customer-details" method="POST" onsubmit="return  formValidation() action="contact_form.php">
  <fieldset>
<legend>Personal information:</legend>
Name:*<br>
<input type="text" id="name" name="name" placeholder="Your Name"><br>
Company Name:<br>
<input type="text" id="company" name="company" placeholder="Your company Name"><br>
Email Address*:<br>
<input type="email" id="email" name="email" placeholder="Your Email Address"><br>
Interested in:<br>
<input type="text" id="interest" name="interest" placeholder="Interested in"><br>
Your query*:<br>
<textarea id="query" name="query" placeholder="Your query here(more then 10 words)..."></textarea><br>
<button id="submitBtn" name="submitBtn" type="submit">Submit Form</button>
</fieldset>
</form>

I saved contact_form.php file inside my themes folder. When I click on submit button, page is not found. Which is correct path to put contact_form.php file.

1 Answer
1

You would never create a create-form.php, doing so would introduce a security problem.

For example, lets say your client changed themes, this contact form would still work. Even with the theme deactivated. The same is true of any form handler, AJAX handler, or endpoint for a remote service that directly accesses a PHP file inside a theme or plugin.

WordPress is a CMS, it’s supposed to handle all the requests made to it, not individual files in a plugin or theme

Instead, don’t specify an action on your form, include a hidden input to check against, and then look for that input to handle your form input, e.g.:

// Template
<form method="POST">
    <input type="hidden" name="aviras_contact_form" value="contact_form">
    ....
</form>

// functions.php
add_action( 'init', function() {
    if ( empty( $_POST['aviras_contact_form'] ) ) {
        return;
    }
    // handle form submission
}

For AJAX, use register_rest_route ( consider asking a new question on how to do that ). For 3rd party call ins or handlers, use rewrite rules or check for parameters

Also, indent your code, all the major popular editors today all auto-indent for you and have packages to reindent and beautify automatically ( PHPStorm, Netbeans, Sublime Text, Coda, etc etc )

Leave a Comment