tags:

views:

47

answers:

2

Hi, i have been struggling for a while now trying to finish this, i am new to a lot of this and although i have had some tips i think i am getting myself more lost. So i have stripped the code to a point where it almost works.

DEMO IS HERE: http://designsbythom.com/meterread/test.html

I have a form a customer fills out to submit printer meter readings. Certain printer models have more than 1 reading, up to 3. When the customer clicks 'add printer' jQuery adds content and they select the model from a drop-down and the appropriate number or meter reading inputs appear. They then can click 'add printer' again for multiple printers.

The data is then posted to contact.php where it is validated and then sent by email in a readable format.

The problem i am getting is that i only get the first printer added and not the others. I think i need to use an input array but i don't know how exactly.

I currently get this in the email (only first printer displays):

Meter Reading submitted by Craig. Account No. 123456, readings are as follows.

Printer Model = Phaser 2010, with Serial No. 101010. Mono Reading = 1234, Colour Reading = 5678 , Express Reading = .

Customer Craig email, [email protected]

--> Notice Express Reading is blank because it was needed. I deally this would not be there at all.

I would like it to come out something like this (when four printers added, two the same model):

Meter Reading submitted by Craig. Account No. 123456, readings are as follows.

Printer Model = Phaser 2010, with Serial No. 101010. Mono Reading = 1234, Colour Reading = 5678

Printer Model = Phaser 2010, with Serial No. 202020. Mono Reading = 910, Colour Reading = 1112

Printer Model = ColorQube, with Serial No. 303030. Mono Reading = 1234, Colour Reading = 5678, Express Reading = 91011 .

Printer Model = WorkCentre 7150, with Serial No. 404040. Mono Reading = 1234

Customer Craig email, [email protected]

Below is the code for contact.php:

<?php

if(!$_POST) exit;

    $name = $_POST['name'];
    $account = $_POST['account'];
    $email = $_POST['email'];
    $printer = $_POST['printer'];
    $serial = $_POST['serial'];
    $mono = $_POST['mono'];
    $colour = $_POST['colour'];
    $express = $_POST['express'];

    if(trim($name) == '') {
        echo '<div class="error_message">Attention! You must enter your name.</div>';
        exit();
        } else if(trim($email) == '') {
            echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
            exit();
        } else if(!isEmail($email)) {
            echo '<div class="error_message">Attention! You have entered an invalid e-mail address, try again.</div>';
            exit();
        }

    if($error == '') {

        if(get_magic_quotes_gpc()) {
            $comments = stripslashes($comments);
        }

    // Configuration option.
    //$address = "[email protected]";
    $address = "[email protected]";

    // Example, $e_subject = '$name . ' has contacted you via Your Website.';
    $e_subject = 'Meter Reading submitted by - ' . $name . '. Account No.' . $account . '.';

    // Configuration option.
    $e_body = "Meter Reading submitted by $name. Account No. $account, readings are as follows.\r\n\n";

    // THIS NEEDS TO ONLY SHOW COLOUR AND EXPRESS READING IF THAT PRINTER REQUIRES IT - and - REPEAT FOR EACH ADDED PRINTER
    $e_content = "Printer Model = $printer, with Serial No. $serial. Mono Reading = $mono, Colour Reading = $colour , Express Reading = $express.\r\n\n";

    $e_reply = "Customer $name email, $email";

    $msg = $e_body . $e_content . $e_reply;

    if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")) {


    // Email has sent successfully, echo a success page.

    echo "<fieldset>";          
    echo "<div id='success_page'>";
    echo "<h1>Email Sent Successfully.</h1>";
    echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
    echo "</div>";
    echo "</fieldset>";

    } else {

    echo 'ERROR!';

    }

}

function isEmail($email) { // Email address verification, do not edit.

return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));

}
?>

I would be so grateful for some help with this. Please NOTE the linked file "jquery.jigowatt.js" as it also uses post.

I someone can help and sorry it is such a big one to explain and show. If you need any more info to help please let me know.

Thom

A: 

Try naming the fields as arrays like printer[] and serial[].

When posted, you'll get an array of values instead of just the one.

$_POST['printer'][0], $_POST['printer'][1], et cetera

Do that for the reading inputs as well, and you should be in good shape.

joshtronic
thanks so much for reply but changing the names of fields did not work.do i need to put - $_POST['printer'][0], $_POST['printer'][1] - in the code or was that just to show how it would come ou?
thom
Yeah, you'll have to change your code to generate the email a bit to loop through the arrays: foreach ( $_POST['printer'] as $key => $value )Inside the foreach you can pull the other variables using the $keySorry for not elaborating in my original reply
joshtronic
A: 

You should name the HTML fields as arrays. For example, the printer input should be printer[] and the serial input should be serial[], otherwise you will only receive one value once the form is submitted.

For example:

<select name="printer[]" type="text" id="printer" class="printer">
    ...
</select>

Then, on contact.php you can loop through the data building your content string.

For example,

for ($i = 0; i < count($_POST['printer']); $i++) {
    $e_content .= "Printer Model = $_POST['printer'][i], with Serial No. $_POST['serial'][i]..."
}

That's assuming that every printer has a serial number and that the arrays will be the same length.

birderic