嗨,我目前有一些PHP代码,这些代码会将先前页面上的复选框中先前选择的时隙放入数据库中,但是我不确定在执行此操作之前如何将每个时隙存储在变量中,以便可以使用它们列出约会位置在我的电子邮件功能中
当我尝试在电子邮件功能中插入“ $ key”时,它仅保留最后选择的值...
我在第一页上的代码为:
<?php
for ($i=0;$i<=31;$i++){ while ($myrow = mysql_fetch_row($result)) {
printf("<tr><td>%s</td><td>%s</td></tr>\n",
$myrow[0]);
}
echo "<td><input type='checkbox' name='$displayTime2' onclick='KeepCount()'></td>";
echo "<td>$pagetime</td>"; ?>
目前,我的代码如下:
<?php
foreach ($_POST as $key=>$value)
{
echo $key; // show times selected on previous page
mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
, Practice_ID, Appointment_ID)
VALUES('$patid','$insertdate','$key','$pracid','$apptype')");
}
?>
我的邮件功能如下:
$to = "$pemail";
$subject = "Talking Teeth Check Up Booking Confrmation";
$message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
booked for ".$insertdate. " at ".$key." at the ".$pracname." practice";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
最佳答案
尽管以上评论是正确的,但让我帮助您。
首先,您需要确定已选中了哪些复选框,因此请执行一些IF语句以在foreach
循环中的每个复选框中进行确定。
尝试这样的事情:
<?php
$savedData = array();
foreach ($_POST as $key=>$value){
$key = mysql_real_escape_string($key);
echo($key. "<br>"); // show times selected on previous page
mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
, Practice_ID, Appointment_ID)
VALUES('$patid','$insertdate','$key','$pracid','$apptype')");
//To save the variables for later:
$savedData[] = $key;
}
?>
现在,您可以使用$ savedData [0] .... $ savedData [1] ... $ savedData [2] ....等访问我们所有的KEY。
因此,您的电子邮件功能可能类似于:
foreach($savedData as $time){
$to = "$pemail";
$subject = "Talking Teeth Check Up Booking Confirmation"; //mis-spelled confirmation =)
$message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
booked for ".$insertdate. " at ".$time." at the ".$pracname." practice";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
因此,这将为
$savedData
中的每个零件发送一封电子邮件-如果确实如此。关于php - 在foreach循环中存储变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15234087/