【发布时间】:2018-04-11 18:52:07
【问题描述】:
我需要以下代码中的一些帮助。我想解析来自 custom_field (tech_spec_table) 的信息,其中包含一个带有博客/产品的 html 标签的表(例如技术规范),只有在用户检查特定产品/产品并通过表单中的表单提交(使用按钮)之后博客文章。我创建了一个页面“comparison-page.php”作为模板页面,并尝试在此页面上链接表单的操作。 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>";
comparison-page.php 文件位于 :wp-content/themes/custom-theme 下的自定义主题中。 这是比较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(); ?>
虽然当我点击按钮导致 404 未找到 1)我应该怎么做才能使路径不可见并正确连接到页面? 2)变量“比较”是否会保存表单中的所有信息,以便我可以获取 post_meta 信息并在比较页面中进行操作?
【问题讨论】:
标签: php wordpress forms custom-wordpress-pages