dsgnze6572 2017-10-30 17:41
浏览 53

将变量作为数组从复选框表单发送到另一个页面

I need some help in the code below. I want to parse information from a custom_field (tech_spec_table) that contains a table with html tags of a blog/product (eg technical specs) only after the user has checked specific product/products and submitted (with a button) through a form in a blog-post. I have created a page "comparison-page.php" as a template page and try to link the action of the form on this page. screenshot

// check if we got posts to display:
if (have_posts()) :

echo "<form method='post' action='<?php bloginfo('template_url'); ?>/comparison-page.php' >";
    while (have_posts()) : the_post();

      //the post

   echo "<article class='"....  >";
   echo "<input type='checkbox' name='comparison[]' />";

     //the post

$post_loop_count++;
    endwhile;

        echo "<input type='submit' name='submit' value='Submit'/>";
        echo "</form>"; 

The comparison-page.php file is located into a custom theme under :wp-content/themes/custom-theme. Here is the code for the comparison-page.php

<?php 
/* Template Name: comparison-page */ 
?>

<?php get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php

           if(isset($_POST['submit'])){    //to run PHP script on submit

   if(!empty($_POST['comparison'])){
     // Loop to store and display values of individual checked checkbox.
     foreach($_POST['comparison'] as $data_specs){

      echo $data_specs."</br>";

    $data_specs = get_post_meta(get_the_ID(), 'tech_specs_table', true);
    $dom = new domDocument;

    @$dom->loadHTML($data_specs);
    $dom->preserveWhiteSpace = false;
    $tables = $dom->getElementsByTagName('table');

    $rows = $tables->item()->getElementsByTagName('tr');

    foreach ($rows as $row) {
        $cols = $row->getElementsByTagName('td');
        echo $cols[];
        }

    }
}
}       
        ?>

    </main><!-- .site-main -->



</div><!-- .content-area -->


<?php get_footer(); ?>

Although when i click the button leads to a 404 not found 1)What should i do to make the path invisible and connect correctly to the page? 2)Will the variable "comparison" save all the information from the form so i can get the post_meta information and manipulate in the comparison page?

  • 写回答

2条回答 默认 最新

  • doufei5315 2017-10-30 17:47
    关注

    So your problem is that WordPress doesn't work that way. You can't submit data to theme files like that.

    What you need to do is have the page that you're submitting to use a custom page template and have your code for processing the form on that new custom page template.

    So you're looking at having two custom pages with custom templates: 1) the form 2) the submission processor

    The action for the form should take you to the submission processor.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么