Sokratis V
Sokratis V

Reputation: 101

Send a variable as array from a checkbox form to another page

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?

Upvotes: 0

Views: 60

Answers (2)

Rad80
Rad80

Reputation: 833

The error seems to be on line 3, when you define the form tag. You cannot open php tags inside a string! Instead, you can concatenate the string with the result of the bloginfo function with the . operator.

Upvotes: 1

Eligos
Eligos

Reputation: 1132

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.

Upvotes: 1

Related Questions