gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:21:35 25 4
gpt4 key购买 nike

我正在开发一个 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>";

这是我的另一个页面的代码,它应该处理表单。但不幸的是它返回 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
));
}

}

?>

最佳答案

Wordpress 有一个通用的处理程序来处理所有表单 - admin-post.php

如果您在表单中包含一个名为 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>";

然后在您的 functions.php 文件(或您通过 functions.php 包含的任何其他 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 }

}

我不确定您在到达所需目的地后是否需要重定向,但如果需要,则很容易解决。

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

关于php - 如何在wordpress插件中将表单提交到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com