tags:

views:

40

answers:

2

Sorry i am a bit of a newbie.

I have a form with some inputs which are arrays and some which are not

<input type="text" name="name" value="name1111" />
<input type="text" name="email" value="email1111" />

<input type="text" name="model[]" value="model1111" />
<input type="text" name="serial[]" value="serial1111" />
<input type="text" name="read[]" value="read1111" />

<input type="text" name="model[]" value="model2222" />
<input type="text" name="serial[]" value="seria2222" />
<input type="text" name="read[]" value="read2222" />

<input type="text" name="model[]" value="model3333" />
<input type="text" name="serial[]" value="serial3333" />
<input type="text" name="read[]" value="read3333" />

<!-- model4444 etc.. dynamically added with jquery as needed -->

when i post them to read.php and use foreach the output repeats the name and email instead of posting once. i am not sure what to do to make it work.

this is what i am trying

$name = $_POST['name'];
$email = $_POST['email'];
$model = $_POST['model'];
$serial = $_POST['serial'];
$read = $_POST['read'];

$a = "Meter Readings From Customer - $name with contact $email";

foreach ( $model as $key => $n ) {
$b = "Model - " . $n . "with Serial No. " . $serial[$key] . " has a Reading of - " .    $read[$key] . ". End of Reading\n";

echo $a . $b;
}

i want it to come out like this

Meter Readings From Customer - name1111 with contact email1111

Model - model1111 with Serial No. serial1111 has a Reading of read1111. End of Reading

Model - model2222 with Serial No. serial2222 has a Reading of read2222. End of Reading

Model - model3333 with Serial No. serial3333 has a Reading of read3333. End of Reading

with the end goal being able to get it into $msg then mail it and return a success message with this

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

    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!';

    }
}

I would be so grateful if someone could help me with this

+1  A: 

It looks like it would be far simpler to simply append an incrementing number to the end of the names and check the $_POST array for that.

Something like:

<input type="text" name="model1" value="model1111" />
<input type="text" name="serial1" value="serial1111" />
<input type="text" name="read1" value="read1111" />

And:

$b = "Model - " . $n . "with Serial No. " . $_POST["serial".$key] . " has a Reading of - " .    $_POST["read".$key] . ". End of Reading\n";
peachykeen
But then how do you loop through them?
yjerem
<input type="text" name="item[0][model]" value="model1111" /><input type="text" name="item[0][serial]" value="serial1111" /><input type="text" name="item[0][read]" value="read1111" /><input type="text" name="item[1][model]" value="model2222" /><input type="text" name="item[1][serial]" value="serial2222" /><input type="text" name="item[1][read]" value="read2222" />would be better, then you can loop $_POST['item']
Scuzzy
+1  A: 

I'd build the $msg like this:

$msg = "Meter Readings From Customer - $name with contact $email\n\n";

foreach ($model as $key => $n) {
  $msg .= "Model - " . $n . "with Serial No. " . $serial[$key] . " has a Reading of - " . $read[$key] . ". End of Reading\n";
}

In case you don't know the .= operator, it's adding the string on the right to the $msg variable on the left. After the loop is done, your $msg should be complete and ready to send.

yjerem
perfect, thank you so much had been seeing .= in a few examples but wasn't sure exactly what it was doing. cheers
thom