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

Path for php file for inserting data through html form

programmeradmin1浏览0评论

I have a HTML form which inserts data into a custom table. The code for inserting the data is in a PHP file .

My question is where do I place the PHP file in the WordPress filesystem? I have tried placing the file in the root, does not work. Placed it in the wp-content/themes/mytheme folder. Does not work.

I get an error Page could not be found.

<form id="XXXForm" method="POST" action="/xxx_insert.php">

Any help is greatly appreciated.

David

I have a HTML form which inserts data into a custom table. The code for inserting the data is in a PHP file .

My question is where do I place the PHP file in the WordPress filesystem? I have tried placing the file in the root, does not work. Placed it in the wp-content/themes/mytheme folder. Does not work.

I get an error Page could not be found.

<form id="XXXForm" method="POST" action="/xxx_insert.php">

Any help is greatly appreciated.

David

Share Improve this question asked Apr 10, 2019 at 17:39 David D'LimaDavid D'Lima 33 bronze badges 5
  • It will work in your theme folder. are you trying to directly call it or are you including it from another php? – rudtek Commented Apr 10, 2019 at 17:48
  • Directly calling it. So i should put it in the wp-content/themes/mytheme folder? Also do I call it as /xxx_insert.php or just xxx_insert.php? – David D'Lima Commented Apr 10, 2019 at 17:52
  • Call it like /wp-content/themes/mytheme/xxx_insert.php – mrben522 Commented Apr 10, 2019 at 18:07
  • Get a HTTP 500 error. – David D'Lima Commented Apr 10, 2019 at 18:21
  • Calling a PHP file in your theme or plugin directly is not the way to do this, and can be a major security hole. You don't need to create a dedicated PHP file to handle the form – Tom J Nowell Commented Apr 10, 2019 at 18:22
Add a comment  | 

2 Answers 2

Reset to default 2

You need to integrate your form and php form processor with Wordpress. One method to do this in your theme as follows.

Create a page template in your theme folder, as described here. And put your form markup in it.

Don't specify an action on your form, and add a hidden field to your form with arbitrary name and value. We will check against, and then look for that input to handle your form input, For example:

<?php 
       /* Template Name: My Form 
*/ 

?>

<form method="POST">
    <input type="hidden" name="my_hidden_field" value="xyz">
    ....
</form>

Now create a page in WP Admin and assign above created template. Load this page in browser to display your form.

Next put this code in functions.php of your theme

add_action( 'init', function() {
    if ( empty( $_POST['my_hidden_field'] ) ) {
        return;
    }

    // handle form submission
    include "xxx_insert.php"; // Replace xxx_insert.php with exact path to php file
}

Other methods to achieve the result is writing a plugin or shortcode.

I hope this may helps!

You don't need a dedicated PHP file to put in your form tag. The code that generates the form should also handle it. That way action=""

For example, here is a page template with a basic form:

<?php
/**
 * Page Template: Test Form
 */

if ( empty( $_POST['submitted'] ) ) {
    // the form hasn't been submitted, so display it
    ?>
    <form method="POST" action="">
        <input type="hidden" name="submitted" value="yes"/>
        Name: <input type="test" name="name" value=""/>
        <input type="submit">
    </form>
    <?php
} else {
    // it's been submitted, process the form
    $name = $_POST['name'];
    ?>
    <p>You said <?php echo esc_html( $name ); ?></p>
    <?php
}

Using a dedicated PHP file to submit your forms too is a security problem, and comes with maintenance problems ( WP functions won't be available so you'd have to bootstrap WP and all the problems that come with it ). Putting PHP files in the root of your host to be used in a theme or plugin is also a bad idea

发布评论

评论列表(0)

  1. 暂无评论