简体   繁体   中英

How to get an email with all the dynamic checkbox values in php

I'm new to PHP and have seen a few similar queries, but mine involves 2 arrays and I'm not sure how to put them next to each other. Can someone help? very much appreciated! Im moving out and making a site to sell my stuff, I've got a select item and offer amount section on the form. I want them to appear next to each other on the email.

You can see the site i'm making here: http://www.stasale.com

I have the following code:

<table border="1">
<tr>
<th>Image</th>
<th>Name</th>
<th>Condition</th>
<th>Description</th>
<th>Asking Price</th>
<th>Select Item</th>
<th>Offer Value</th>
</tr>

<?php   
 require_once('appvars.php');
 require_once('connectvars.php');
 // Connect to the database 
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
$query = "SELECT * FROM stasale ORDER BY date DESC";
$data = mysqli_query($dbc, $query);

while ($row = mysqli_fetch_array($data)) { 

  if (is_file(GW_UPLOADPATH . $row['image']) && filesize(GW_UPLOADPATH . $row['image']) > 0) {
  echo '<tr><td><img src="' . GW_UPLOADPATH . $row['image'] . '" alt="' . $row['image'] . '" width="100" height="100"/></td>
  <td>' . $row['name'] . '</td>
  <td>' . $row['condition'] . '</td> 
  <td>' . $row['description'] . '</td> 
  <td>' . $row['askingprice'] . '</td> 
  <td><form method="post" action="mail.php"><input type="checkbox" name="itemname[]" value="' . $row['id'] . '">Select</td> 
  <td>Offer: <input type="text" name="offer[]"></td></tr>';
}
else {
  echo 'Invalid row';
}
} ?>

<tr><td colspan="3">Contact Name:</td><td colspan="4"><input type="text" name="coname" width="300" ></td></tr>
<tr><td colspan="3">Contact Number:</td><td colspan="4"><input type="text" name="conumber" width="300" ></td></tr>
<tr><td colspan="3">Contact Email:</td><td colspan="4"><input type="text" name="coemail" width="300" ></td></tr>
<tr><td colspan="3">Questions/Comments:</td><td colspan="4"><input type="text" name="comments" width="300" height="100"></td></tr>

<tr><td colspan="7" align="center"><input type="submit" value="Send Request" name="submit" style="width:500px"/></form></td></tr>
</table>

I can't seem to work out how to get the values from itemname[] or offer[] to show up in my mail.php page which is completely wrong I feel:

if (isset($_POST['submit'])) {
$name = $_POST['coname'];
$email = $_POST['coemail'];
$number = $_POST['conumber'];
$comments = $_POST['comments'];
foreach ($_POST['itemname'] as $itemname)
foreach ($_POST['offer'] as $offer)

{

}
$to = 'shinobuden@gmail.com';
$subject = 'STASALE order';
$msg = "$name, $email, $number, $comments \n" .
  "I would like: $itemname" .
  "$offer";

mail($to, $subject, $msg, 'From:' . $email);

$to = "$email";
$subject = 'I have received your request - Si';
$msg = 'Thank you for contacting me! I have received your request and will reply shortly! ~ Si x';
$from = 'shinobuden@gmail.com';

mail($to, $subject, $msg, 'From:' . $from); 

echo '<p><strong>Thanks for your request. I will respond as soon as possible.</strong></p>';


}

?>

Thank you for your help in advance!

You can do something like this:

foreach ($_POST['offer'] as $key => $value) {
        if ($value) {
            $offer_arr[] = $value;
        }
    }

    $str = '';
    for ($i = 0; $i <= count($_POST['itemname']); $i++) {
        if ($_POST['itemname'][$i]) {
            $str .= 'Item: ' . $_POST['itemname'][$i] . ' Offer: ' . $offer_arr[$i] . ', ';
        }
    }

$str will contain all the items and offers next to each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM