php 如何在wordpress插件中将表单提交到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19997913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:20:33  来源:igfitidea点击:

how to submit a form to another page in wordpress plugin

phpwordpressform-submit

提问by Mushfiqul Tuhin

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

我正在开发一个 wordpress 插件,它将表单提交到另一个页面。但是当我尝试将表单提交到另一个页面时,该页面会返回一些 php 错误。我的表单代码如下

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 错误。

<?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
                        ));
 }

}

?>

回答by David Gard

Wordpress has a generic handler to deal with all forms - admin-post.php.

Wordpress 有一个通用处理程序来处理所有表单 - 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.

如果您在表单中包含一个名为 的隐藏字段action,则您可以连接到您选择的功能,其中包含 wordpress 的所有优点。

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.phpfile (or any other phpfile that you have included via functions.php), you can use this method.

然后在您的functions.php文件(或php您通过 包含的任何其他文件functions.php)中,您可以使用此方法。

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...

最后一个问题 - 这个表单是在前端还是在管理区域?并不是说这个答案应该有所作为,我只是很好奇......

回答by RRikesh

Your frontend_poll_process.phppage is getting called out of the WordPress environment, therefore returning an error on $wpdb->get_results().

您的frontend_poll_process.php页面被 WordPress 环境调用,因此在 上返回错误$wpdb->get_results()

You can add your code to a plugin or functions.phpusing hooks:

您可以将代码添加到插件或functions.php使用钩子:

<?php
add_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
      ) );
    }
  }
}