dimanche 19 avril 2015

Inserting data into a WordPress table on form submit, using PHP

I have a form with some fields, and a wpdb table which corresponds to the fields. I want the data from the form to be submitted into the table when the submit button is clicked on the form.


Here is the form:



function display_form(){
echo '
<form action="insertrow.php" method="post">
<p>Ticket id: <br />
User id: <br />
Description: <textarea class="widget" rows="4" cols="1"
name="ticket_description"></textarea>
Priority: <select name="ticket_priority" placeholder="Select">
<option value="critical">Critical</option>
<option value="urgent">Urgent</option>
<option value="important">Important</option>
<option value="standard" selected>Standard</option>
</select>
Status: <select name="ticket_status" placeholder="Select">
<option value="planned">Planned</option>
<option value="in progress">In Progress</option>
<option value="on hold">On Hold</option>
<option value="completed">Completed</option>
<option value="ready for invoice">Ready for Invoice</option>
<option value="to be invoiced as per attached">To be invoiced as per
attached</option>
</select>
</p>
<input type="submit" name="submit" value="submit">
</form>
';
}


the form calls the insertrow.php file:



if(isset($_POST['submit']))
{
insert_row();
}

function insert_row()
{
global $wpdb;
require_once('../../../wp-config.php');
$tablename = 'st_support_ticket';

$data = array(
'ticket_id' => '1',
'ticket_user_id' => '1',
'ticket_description' => $_POST['ticket_description'] ,
'ticket_priority' => $_POST['ticket_priority'],
'ticket_status' => $_POST['ticket_status'] );

$wpdb->insert($tablename, $data);
}


Just trying to get this to enter the description, priority and status into the st_support_ticket table.


At the moment when I click submit the url suffix changes to insertrow.php and displays a blank page. The data is not entered into table (checking by opening it up in phpmyadmin).


Am I missing something?


Aucun commentaire:

Enregistrer un commentaire