Grouping multiple form values into a single value

So this is a long one…I’m working in Wordpress and mainly when it comes to forms I’ll just go with a plugin (form processing is not my strong suit), but currently I’m putting together a custom form for a client and need some expert advice.

Just to give you a quick rundown of what the form is and what I’d like it to do:

  • It’s a rental quote form.
  • The customer should be able to select different furniture pieces and input desired quantities.
  • When the form is submitted, the most important data (besides the customer’s contact info) is the item’s name, that item’s ID number (which is supplied as meta data from that item’s post in WP), and the quantity of that specific item.

Now the trickiest part I’ve come across is trying to group the data for the item name, id, and quantity. And secondly, trying to render it nicely in the email that gets sent to the client. Here’s what I’ve got so far:

/*
Template Name: Quote Form Page Template
*/
?>

<?php
 if(isset($_POST['submit']))
 {
$name = $_POST['renter_name'];
$email = $_POST['renter_email'];
$message = $_POST['renter_message'];
$subject = "Quote Request";

// $items  = implode(',', $_POST['item']);

$msg  = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<p><strong>Sent by:</strong> ".$name."</p>";
$msg .= "<p><strong>Items:</strong></p>";


//$msg .= "<ul>";

//foreach($_POST['item'] as $item){
//  $msg .= "<li>".$item."</li>";
//}

foreach($_POST['item'] as $item){
    $msg .= "<p>".$item['name']."</p>";
    $msg .= "<p>".$item['id']."</p>";
    $msg .= "<p>".$item['quantity']."<p>";  
}

//$msg .= "</ul>";
$msg .= "<p><strong>Message:</strong> ".$message."</p>";
$msg .= "</body></html>";

$to = "myemail@whatever.com";
$headers = "From: " . strip_tags($_POST['renter_email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['renter_email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

     if(wp_mail($to, $subject, $msg, $headers)){
             ?>
                <script type="text/javascript">
                    window.location = "http://www.somewebsite.com/wordpress/thank-you/";
                </script>      
    <?php
     }
     else{
         echo "Mail not sent!";
     }
 }
?>


<?php get_header(); ?>

<form action="" id="quote-form" method="post">
    <label>Name: <label/><input type="text" name="renter_name"/>
    <br/>
    <label>Email: <label/><input type="text" name="renter_email"/>
    <br/>
            <section id="chairs">
        <div class="item-display">Chairs and Sofas</div>
        <div class="closed">
            <?php query_posts( array(
                'category_name'  => 'upholstered-seating',
                'posts_per_page' => -1,
                'order'=> 'ASC'

            ) ); ?>


            <?php while ( have_posts() ) : the_post(); ?>

                <input type="checkbox" name="item[][name]" value="Name: <?php the_title(); ?>"/>
                <input type="hidden" name="item[][id]" value="ID: <?php echo get_post_meta($post->ID, 'id_number', true); ?>"/>
                <label for="<?php echo get_post_meta($post->ID, 'id_number', true); ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" class="form-image" /></label>
                <input type="text" name="item[][quantity]"/>

            <?php   ?>

            <?php endwhile; ?>


            <?php wp_reset_query(); ?>
            </div>
    </section>

    <textarea name="renter_message" id="message" cols="10" rows="10">Questions?</textarea>
    <br/>
    <input type="submit" name="submit" value="Send">

</form>

<?php get_footer(); ?>

I know that must look incredibly slapdash. I’m basically learning form processing as I go. This is very much a prototype as you can see by the commented out parts I wanted to keep around for reference. Those will be removed in the final code.

Right now I’m passing the ID numbers as a hidden input, which isn’t great because all the id inputs are getting included in the final rendering. I know that’s the nature of a hidden input, but I couldn’t come up with a better solution. Any suggestions there?

Is there a better way to group these values (checkbox, text input, and supplied value) together and render them? I feel like my loop could be way better. Or maybe there’s a better way to include more data values into the inputs themselves?

This is all working by the way, I’m able to submit the form and get an email:

outputemail

But as you can see the formatting is not great at all. As mentioned before I’m getting all the hidden values (for example that “ID number: 35” is from a non-selected item), and I’m also unable to add the text “Quantity” in front of it’s corresponding value without completely breaking the loop. So right now it’s just the number that was entered in the form.

Any help and/or advice would be greatly appreciated! If you need any more info just let me know.
Btw I have posted this to stackoverflow almost a week ago and I haven’t gotten any hits yet. I’m hoping maybe someone here could steer me in the right direction :laughing:

@dlyons hi I’m new here will you help me

Hey @UNKNOWN123! I would suggest making a new forum post if you need help with something. The members here are awesome. I’m not sure if there’s a guide to posting on the forum, but it’s pretty straight forward nonetheless, basically just add your topic, select the forum section you want it to be tagged with and then add your post message.

I’m just going to bump this once and then let it go. Pretty much you can ignore the WP stuff. Really it’s the PHP and form processing that I need help with.