简体   繁体   中英

Sending MySQL Table via PHP using HTML form

Can someone help me fixing my code?

I want to retrieve some data on my database based on HTML Form and send it via email (PHP).

HTML FORM: (this form is working fine, I don't see any problems here)

<form method="post" action="valid_tasks.php">

    <div class="form-group">
        <label for="mailTo">To:</label>
        <select class="form-control" id="mailTo" name="mailTo">
            <?php echo showUsers(); ?>
        </select>
    </div>

    <div class="form-group">
        <label for="statusTo">Task Status:</label>
        <select class="form-control" id="statusTo" name="statusTo">
            <?php echo showStatus(); ?>
        </select>
    </div>

    <input type="submit" name="submitMail" id="submitMail" class="btn btn-info" value="Send" style="margin-bottom: 20px;">

</form>

PHP: (I'm using this code as a function on another page, and it seems to work fine too, it's a little bit different, but it works)

<?php 

require_once('db.class.php');

$objDb = new db();
$link = $objDb->conecta_mysql();

if(isset($_POST['submitMail']))
    {

    $status = $_POST['statusTo'];
    $userMail = $_POST['mailTo'];

    $id = $_SESSION['id'];
    $username = $_SESSION['username'];

    $query = "SELECT T.setor, T.taskWhat, T.taskWho, DATE_FORMAT(T.deadLine,'%d/%m/%Y') AS deadLine,";
    $query .= "T.taskStatus, U.username, U.email, S.descricao, S.abDescri";
    $query .= "FROM tarefas AS T LEFT JOIN status AS S ON T.taskStatus = S.abDescri ";
    $query .= "LEFT JOIN users AS U ON U.username = T.taskWho ";
    $query .= "WHERE T.taskWho = '$userMail' AND S.abDescri = '$status'";

    $result = mysqli_query($link, $query);

while($row = mysqli_fetch_assoc($result)){

    $setor = $row['setor'];
    $taskWhat = $row['taskWhat'];
    $taskWho = $row['taskWho'];
    $deadLine = $row['deadLine'];
    $taskStatus = $row['taskStatus'];
    $userAcao = $row['username'];
    $emailAcao = $row['email'];
    $statusDescri = $row['descricao'];
    $statusAb = $row['statusAb'];

    $setor = mysqli_escape_string($link, $setor);
    $taskWhat = mysqli_escape_string($link, $taskWhat);
    $taskWho = mysqli_escape_string($link, $taskWho);
    $deadLine = mysqli_escape_string($link, $deadLine);
    $taskStatus = mysqli_escape_string($link, $taskStatus);
    $userAcao = mysqli_escape_string($link, $userAcao);
    $emailAcao = mysqli_escape_string($link, $emailAcao);
    $statusDescri = mysqli_escape_string($link, $statusDescri);
    $statusAb = mysqli_escape_string($link, $statusAb);

    echo 
    '<tr>
        <td>'.$setor.'</td>
        <td>'.$taskWhat.'</td>
        <td>'.$deadLine.'</td>
        <td>'.$taskWho.'</td>
        <td>'.$statusAb.'</td>
    </tr>';

    }
}

Email: (I've seen a lot of examples of how to use it, but none of them were about getting info from a HTML Form and putting it into PHP Mail)

$to = $email;
$subject = "Tarefas com status ".$status;

$message = "
<html>
<head>
<title>HTML email</title>
<link rel='stylesheet' type='text/css' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>
</head>
<body>
<div class='container'>
<center><h1>Hello, ".$username."!</h1></center>
</div>
       // I NEED TO PUT THIS INFO HERE
</div>
</body>
</html>

Thank you all!

  1. Define a variable at the top of your script

$table = '';


  1. Inside your loop, next to the echo, assign the string to a variable:

echo 
'<tr>
    <td>'.$setor.'</td>
    <td>'.$taskWhat.'</td>
    <td>'.$deadLine.'</td>
    <td>'.$taskWho.'</td>
    <td>'.$statusAb.'</td>
</tr>';

$table .= ""
    . "<tr>"
        . "<td>$setor</td>"
        . "<td>$taskWhat</td"
        . "<td>...</td>"
    . "</tr>"
;

  1. Once you are done, add it to the $message variable:

 $message = "
<html>
<head>
<title>HTML email</title>
<link rel='stylesheet' type='text/css' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>
</head>
<body>
<div class='container'>
<center><h1>Hello, $username!</h1></center>
</div>
    $table
</div>
</body>
</html>";

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