how to submit a form to another page in wordpress plugin
Wordpress has a generic handler to deal with all forms - admin-post.php
.
If you include a hidden field in your form called action
, you can then hook in to a function of your choice with all the goodness of wordpress included.
echo "<form action='".get_admin_url()."admin-post.php' method='post'>"; echo "<input type='hidden' name='action' value='submit-form' />"; echo "<input type='hidden' name='hide' value='$ques' />"; { Enter the rest of your first block of code from above here }echo "</form>";
And then in your functions.php
file (or any other php
file that you have included via functions.php
), you can use this method.
add_action('admin_post_submit-form', '_handle_form_action'); // If the user is logged inadd_action('admin_post_nopriv_submit-form', '_handle_form_action'); // If the user in not logged infunction _handle_form_action(){ { Enter your second block of code from above here }}
I'm not sure if you require a redirect once you reach your desired destination, but that can be easily accounted for if you do.
And one final question - is this form on the front end, or in the admin area? Not that it should make a difference that this answer, I'm just curious...
Your frontend_poll_process.php
page is getting called out of the WordPress environment, therefore returning an error on $wpdb->get_results()
.
You can add your code to a plugin or functions.php
using hooks:
<?phpadd_action( 'after_setup_theme', 'so_19997913' ); function so_19997913() { require_once("function_ip.php"); $vote_result = $_POST['ans_name']; $uid = uniqid(); global $wpdb; $table_vote = $wpdb->prefix . "poll_answer_result"; $count = count( $vote_result ); $hidden = $_POST['hide']; $ans_data = $wpdb->get_results( "SELECT * FROM $table_vote WHERE question_id='$hidden'" ); if ( $count > 0 ) { foreach ( $vote_result as $vote_arr ) { $wpdb->insert( $table_vote, array('answer_id' => $vote_arr, 'ip' => get_client_ip(), 'question_uid' => $hidden ) ); } }}