复选框mysql循环中的数组项目未发布到邮件页面

时间:2017-03-12 13:46:05

标签: php mysql

我有点卡住了。我有一个数据库,其中包含各种医疗方法的清单。医务人员会选择医疗援助,该页面将显示该医疗援助的所有可用治疗方法。然后,官员将勾选他将要执行的处理,上传文件,键入消息,然后将文件,消息和选定的处理提交到邮件页面,邮件将邮寄详细信息。

页面正在读取数据库,并且正在创建所选医疗援助的所有可用治疗的复选框列表,因此,如果“Mighty Med”有10种治疗方法,我将看到10行,每行都有自己的复选框。我遇到的问题是表单只提交文件和消息通过。所选项目不会到达帖子页面。如果我没有从数据库中读取并将所有处理线编码到页面上,那么所有选定的项目都会通过。

这是我从数据库中读取并显示复选框列表的表单:

<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

<title>Mr X-Ray</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
    if(!empty($_POST['Name']) && !empty($_POST['SurName']))
    {
        $MedaidNumber = mysqli_real_escape_string($link, $_POST['MedaidNumber']);
        $Name = mysqli_real_escape_string($link, $_POST['Name']);
        $SurName = mysqli_real_escape_string($link, $_POST['SurName']);
        $MedAid  = mysqli_real_escape_string($link, $_POST['taskOption']);
        $PracticeNum = mysqli_real_escape_string($link, $_POST['PracticeNum']);
        $Date = mysqli_real_escape_string($link, date("Y-m-d"));

        $checkusername = mysqli_query($link,"SELECT * FROM users WHERE PracticeNum = '".$PracticeNum."'");

         if(mysqli_num_rows($checkusername) == 0)
         {
            echo "<h1>Error</h1>";
            echo "<p>Sorry, that your practice number is not valid. Please go back and try again.</p>";
         }
         else
         {
            $registerquery = mysqli_query($link,"INSERT INTO patients (MedaidNumber, Name, SurName, MedAid, PracticeNum, Date) VALUES('".$MedaidNumber."', '".$Name."', '".$SurName."', '".$MedAid."', '".$PracticeNum."', '".$Date."')");


            if ($registerquery)
            {
                echo "<h1>Success</h1>";
                echo "<p>Your $MedAid patient was successfully added. Please select the treament given.</p>";

                // this is where i am stuck, the query reads the database and creates a check box list of all treatments
                // i can tick all the treatments and i can type out a message and upload a file.
                $result = mysqli_query($link,"SELECT TreatmentName FROM treatment where Medaid = '".$MedAid."'");
                if ($result) {
                    while($row = mysqli_fetch_array($result)){
                        echo "<input type='checkbox' name='TreatmentName[]' value='".$row['TreatmentName']."'>"
                                        .$row['TreatmentName'] ;
                        echo "<h1></h1>";

                    }
?>

        <form name="myForm" action="SendMail.php" method="post"enctype="multipart/form-data">
                Message: <textarea name="message" placeholder="Your Message.."></textarea>
                <h1></h1>
                <label for='uploaded_file'>Select A File To Upload:</label>
                <input type="file" name="uploaded_file">
                <h1></h1>
                <input type="submit" name="submit" value="Submit" />

<?php   
                // after clicking on submit the items i ticked dont get posted.
                // my file and message goes across though.                  
                }
            }
        else
        {
            echo "<h1>Error</h1>";
            echo "<p>Sorry, Your patient was not added. Please go back and try again.</p>";    

        }       
     }
}

   elseif 

   (empty($_SESSION['LoggedIn']) && empty($_SESSION['Username']))
   if(empty($_POST['Name']) && empty($_POST['SurName']))
        echo "<h1> no Success</h1>";
        // echo "<a href=\billing\index.php>click here to log in</a>";
        if(empty($_POST['Name']) && empty($_POST['SurName']))
            echo "<h1> no Success post</h1>";
?>

</div>
</body>
</html>

这是我提交表单,用于接收所选项目,附件和消息以及发送邮件:

<?php

 if($_POST && isset($_FILES['uploaded_file']))
{

    $from_email         = 'somebody@mycorp.com'; //from mail, it is mandatory with some hosts
    $recipient_email    = 'somebody@mycorp.com'; //recipient email (most cases it is your personal email)

    //Capture POST data from HTML form and Sanitize them,
    $sender_name    = filter_var($_POST["patientsurName"], FILTER_SANITIZE_STRING); //sender name
    $reply_to_email = filter_var($_POST["Doctor"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
    $subject        = filter_var($_POST["Doctor"], FILTER_SANITIZE_STRING); //get subject from HTML form
    $message        = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message
    $code = implode( "\r\n" .'  ', $_POST['TreatmentName'] );
    $FullMessage = 
                    'Hello
                    ' ."\r\n" .
                    'You have received a new form entry from ' . $_POST['doctor'] . '
                    ' ."\r\n" .
                                        'Patient Name :   ' . $_POST['patientName']. '
                    ' .
                                        'Patient Surname :   ' . $_POST['patientsurName']. '
                    ' .
                                        'Patient Medical Aid Number :   ' . $_POST['medicalAidNumber']. '
                    ' ."\r\n" .
                                       ' These are the codes used :   
                    ' ."\r\n" .
                                        '  ' . $code . '
                    ' ."\r\n" . 

                                        ' ' . $_POST['doctor']. ' has attached the following comment: 

                    ' .
                               $_POST['message'];



    //Get uploaded file data
    $file_tmp_name    = $_FILES['uploaded_file']['tmp_name'];
    $file_name        = $_FILES['uploaded_file']['name'];
    $file_size        = $_FILES['uploaded_file']['size'];
    $file_type        = $_FILES['uploaded_file']['type'];
    $file_error       = $_FILES['uploaded_file']['error'];

    if($file_error > 0)
    {
        die('Upload error or No files uploaded');
    }
    //read from the uploaded file & base64_encode content for the mail
    $handle = fopen($file_tmp_name, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode($content));

    $boundary = md5("sanwebe");
    //header
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From:".$from_email."\r\n";
    $headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= chunk_split(base64_encode($FullMessage));

    //attachment
    $body .= "--$boundary\r\n";
    $body .="Content-Type: $file_type; name=".$file_name."\r\n";
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
    $body .="Content-Transfer-Encoding: base64\r\n";
    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
    $body .= $encoded_content;

    $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {      
        die('Thank you for your email. <a href=\billing\patientDetails.php> click here to start billing a new patient</a>');
    }else{
        die('Could not send mail! Please check your PHP mail configuration.');  

    }
  echo('PAge offline.'); 
}
?>

请你协助我告诉我哪里出错了,如果我手动输入并创建了一个列表而不能在我的循环中工作,为什么会有效呢?

谢谢

1 个答案:

答案 0 :(得分:0)

这里的问题是我放入了

<form name="myForm" action="SendMail.php" method="post"enctype="multipart/form-data">

在错误的地方。

它需要在数组之上。我最初把它放在阵列下面。

这是代码看起来完成的内容。 :

<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

<title>-Ray</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))

if(!empty($_POST['Name']) && !empty($_POST['SurName']))
{
    $MedaidNumber = mysqli_real_escape_string($link, $_POST['MedaidNumber']);
    $Name = mysqli_real_escape_string($link, $_POST['Name']);
    $SurName = mysqli_real_escape_string($link, $_POST['SurName']);
    $MedAid  = mysqli_real_escape_string($link, $_POST['taskOption']);
    $PracticeNum = mysqli_real_escape_string($link, $_POST['PracticeNum']);
     $Date = mysqli_real_escape_string($link, date("Y-m-d"));


     $checkusername = mysqli_query($link,"SELECT * FROM users WHERE PracticeNum = '".$PracticeNum."'");

     if(mysqli_num_rows($checkusername) == 0)
     {
        echo "<h1>Error</h1>";
        echo "<p>Sorry, that your practice number is not valid. Please go back and try again.</p>";
     }
     else
     {
$registerquery = mysqli_query($link,"INSERT INTO patients (MedaidNumber, Name, SurName, MedAid, PracticeNum, Date) VALUES('".$MedaidNumber."', '".$Name."', '".$SurName."', '".$MedAid."', '".$PracticeNum."', '".$Date."')");


        if($registerquery)
        {
            echo "<h1>Success</h1>";
            echo "<p>Your $MedAid patient was successfully added. Please select the treament given.</p>";


            ?>

                                                                        <form name="myForm" action="SendMail.php" method="post"enctype="multipart/form-data">

    <div class="container">
<form>

            <?php   
            $result = mysqli_query($link,"SELECT TreatmentName FROM treatment where Medaid = '".$MedAid."'");
                                if ($result) {

                                while($row = mysqli_fetch_array($result)){
                                    echo "<input type='checkbox' name='codes[]' value='".$row['TreatmentName']."'>"
                                        .$row['TreatmentName'] ;
                                     echo "<h1></h1>";

                                }
                                    ?>


                                                    Message: <textarea name="message" placeholder="Your Message.."></textarea>
                                        <h1></h1>
                                        <label for='uploaded_file'>Select A File To Upload:</label>
                                        <input type="file" name="uploaded_file">
                                        <h1></h1>
                                        <input type="submit" name="submit" value="Submit" />
  <h1></h1> <a href=\billing\>click here to Return to the main Login Screen</a>
    </form>
                                          <?php             


                                }



if ($MedAid == 'GEMS'){




                              ?>

                                          <?php             




                                }

                                }



        else
        {
            echo "<h1>Error</h1>";
            echo "<p>Sorry, Your patient was not added. Please go back and try again.</p>";    

            echo " ".$MedaidNumber.", ".$Name.", ".$SurName.", ".$MedAid.", ".$PracticeNum." ,".$Date."";


        }       
     }
}
else
{
    ?>



    <?php
}
   elseif 

   (empty($_SESSION['LoggedIn']) && empty($_SESSION['Username']))
   if(empty($_POST['Name']) && empty($_POST['SurName']))
   echo "<h1> no Success</h1>";
    // echo "<a href=\billing\index.php>click here to log in</a>";
        if(empty($_POST['Name']) && empty($_POST['SurName']))
   echo "<h1> no Success post</h1>";


?>
</body>
</html>

这是一个愚蠢的错误,耗费了大量时间。