I have a page which I want user to be able to post info which I then upload into a database. I am doing my requests via ajax and I am trying to establish my database connection and sql querying via an action hook. First off is this a good pratice?
My code (the relative part):
HTML:
<div id="user_info">
<input class="form-control" type="text" id="name" placeholder="Fullständigt namn" required autocomplete="off"/>
<input class="form-control" type="text" id="email" placeholder="Email" required autocomplete="off"/>
<input class="form-control" type="text" id="address" placeholder="Adress" required autocomplete="off"/>
<input class="form-control" type="text" id="co_address" placeholder="C/O Adress" autocomplete="off"/>
<input class="form-control" type="text" id="city" placeholder="Namn" required autocomplete="off"/>
</div>
JS:
function updateDB(name, email, address, co_address, city){
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'post',
dataType: 'json',
data: {
action: 'det_lilla_extra',
name: name,
email: email,
address: address,
co_address: co_address,
city: city
},
});
PHP (action hook):
<?php
add_action('wp_ajax_det_lilla_extra', 'det_lilla_extra');
add_action( 'wp_ajax_nopriv_det_lilla_extra', 'det_lilla_extra' );
function det_lilla_extra(){
global $wpdb;
//var_dump($_POST);
$wpdb->insert($wpdb->prepare("INSERT INTO orders (name, email, address, co_address, city) VALUES ('%s','%s','%s','%s','%s')",
$_POST['name'], $_POST['email'], $_POST['address'], $_POST['co_address'], $_POST['city']));
wp_die();
}
I've been able to get this far by following and looking at others. But my problem now is that for my action hook to work I need to create a plugin. I dont know how to do this, I've been googling to find turorials but I dont really know what to look for, so its kinda hard to pinpoint what to do.
So my questions:
Is this "good" pratice what i have done? Could I do it another way (regarding having an action hook for database connection)
Is a plugin what i need here? And if so, how do i go about creating it?
ALSO: Currently my action hook does not work since I have not enabled the plugin (i think this is the reason). But everything else should work though.
/* Plugin Name: Vonhacts plugin */
at the top then place the file in the plugins folder, there's no special voodoo needed. Also there's no reason this wouldn't work infunctions.php
, but you shouldn't be creating tables, and updating them in a theme