My first plugin attempt with basic PHP skills and a bunch of WordPress experience… What I want:
User fills out form – form action sends them off to a specialized gateway (got that part ok)
Select data from the form is saved to a custom table
This is going to probably sound stupid but i think i have all the elements I am just missing how to put them together… This is a very basic example of what I want to expand on later.
This is the function (the stuff that makes the table etc already happens earlier)
function rchs_add_donor() {
global $wpdb;
$welcome_name = $_POST['name'];
$welcome_text = $_POST['text'];
$rows_affected = $wpdb->insert( play_donors,
array(
'time' => current_time('mysql'),
'name' => $welcome_name,
'text' => $welcome_text ) );
}
Now the silly part – how/where do i put the form to utilize this function?
I tested the above with straightforward data instead of variables from the $_POST and that writes to the new table fine.
If anyone could point me in the right direction that would be great!
Did more research based on the first answer came up with this:
but need to make sure it is ‘correct’ and ‘secure’ seems like i should be sanitizing data or something before writing it to the dbase?
add_shortcode('donor_form_sc','donor_form');
function donor_form(){
global $wpdb;
echo '<form method="post" action="' . $this_page .'">
<label for="name" id="name">Name: </label>
<input type="text" name="name" id="name" />
<label for="text" id="text">Text: </label>
<input type="text" name="text" id="text" />
<input type="submit" />
</form>';
};
$welcome_name = $_POST['name'];
$welcome_text = $_POST['text'];
$rows_affected = $wpdb->insert( play_donors,
array(
'time' => current_time('mysql'),
'name' => $welcome_name,
'text' => $welcome_text ) );
It writes to the dbase as expected.
Do you mean how you can display the form on your website? If so, write a shortcode for it 🙂 http://codex.wordpress.org/Shortcode_API
Edit: Oh and you might want to add a hook so your function that saves the data is triggered. add_action(‘init’,’rchs_add_donor’) should do. And add a check in your function to check if your POST is set.