I am developing a wordpress plugin , which submits a form to another page. But when I try to submit the form to another page , then that page returns some php error. My form code is below
echo "<form action='".plugins_url()."/wp_voting_poll/frontend_poll_process.php' method='post'>";
echo "<input type='hidden' name='hide' value='$ques' />";
$total_vote_count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_result WHERE question_uid='$ques'" );
if($ques!=""){
echo "<table>";
foreach($ans_data as $ans_res){
// $ans=$ans_res->answer;
$answer_id=$ans_res->id;
$type=$ans_res->answer_type;
$vote_count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_result WHERE answer_id='$answer_id'" );
if($vote_count==0){
error_reporting(0);
}
$vote_percent=($vote_count*100)/$total_vote_count;
echo "<tr> <td>";
echo "<div class='answer_div'>";
if($type==1){
echo "<div class='input'><input type='radio' name='ans_name[]' value='$answer_id'/>".$ans_res->answer."<br/></div>";
}
elseif($type==0){
echo "<div class='input'><input type='checkbox' name='ans_name[]' value='$answer_id'/>".$ans_res->answer."<br/></div>";
}
if($backend==0){
echo "</td> <td>";
echo "<h4> total vote counted $vote_percent% </h4>";
// echo "<img src='$url' width='$width_img'/>";
$bar=$vote_percent*5.9;
echo "<img src='$url' height='10' width='$bar' />";
echo "</td></tr>";
}
}
echo "</table>";
echo "<input type='submit' value='Submit vote' />";
echo "</form>";
And this is my code of another page , which should process the form . But unfortunately it returns php error.
<?php
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
));
}
}
?>
In this case, you'll need to set up a manual redirect without a plugin in WordPress. Go to Tools > Redirection and scroll down to the Add new redirection section. In the Source URL field, type or paste in the URL you want to redirect from. In the Target URL field, type or paste in the URL you want to redirect to.
In your WordPress dashboard, go to Appearance » Widgets and navigate to the WPForms widget. Then, drag the widget to the sidebar area on the right-hand side of the page. Add the title for your widget and select your form from the drop down menu and click the Save button. That's it!
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 in
add_action('admin_post_nopriv_submit-form', '_handle_form_action'); // If the user in not logged in
function _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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With