Reputation: 11
I built a form inside a wordpress page and I want it to send the inputted data into custom build tables in my wp database. I am able to get specified in code data to insert but I cannot figure how to get my code to take in the data. I have tried putting the code inside the page and outside in the main WP folder and still I can't get anything to work.
My form
<form method="post" action="setLiquor.php">
Add a New Liquor Type</br></br>
<p>Name: <input type="text" name="name"/></p>
<p>Description <input type="text" name="description"/></p>
----------------------------------------------</br>
<input type="submit" name="Submit">
</form>
The setLiqour.php file.. this code works when i change the insert data to text and put it into the wp page but I don't know how to get the html form to send the data to it.
<?php
$lq_name = sanitize_text_field( $_POST['name']);
$description = $_POST['description'];
$table_name = $wpdb->prefix . "Liquor_Type";
$wpdb->insert( $table_name, array(
'lq_name' => $lq_name,
'description' => $description
) );
?>
Upvotes: 0
Views: 4650
Reputation: 1383
You can post the data to the same page put the code from the setLiqour.php inside this page template file at the top. and set the action to the_premalink()
or just empty action.
Or you can create Thank you page and put this code in the thank-you page template and submit to this /thank-you page.
You also need to validate that the form is submitted you can do it by wrapping you code like this:
if(isset($_POST['name'])) {
$lq_name = sanitize_text_field( $_POST['name']);
$description = $_POST['description'];
$table_name = $wpdb->prefix . "Liquor_Type";
$wpdb->insert( $table_name, array(
'lq_name' => $lq_name,
'description' => $description
) );
}
Better change the $_POST['name']
to other name like $_POST['lq_name']
.
Upvotes: 2