如果表单字段留空,如何从HTML电子邮件中删除表行

时间:2021-09-26 09:47:55

I'm currently working on a materials order form for the place I work in order to save money. So far, I have about 90% of the different parts working how I'd like them to work. I am new to coding in every aspect, so I apologize if this is some super basic concept. I've been searching the web for days trying to find a solution to this, but every time I come across something helpful like "Use this JS script" I'll come across something like, "Don't ever use JS script for this".

我正在为我工​​作的地方的材料订单表单工作以节省资金。到目前为止,我有大约90%的不同部分工作,我希望他们如何工作。我是各方面编码的新手,所以如果这是一些超级基本的概念我会道歉。我已经在网上搜索了几天试图找到解决方案,但每次我遇到一些有用的东西,比如“使用这个JS脚本”,我会遇到类似的事情,“不要使用JS脚本”。

So, here's the question: how do I remove the corresponding table row in the html email of a form field that has been left blank?

所以,这是一个问题:如何删除已留空的表单字段的html电子邮件中的相应表行?

I came across something similar in another question: PHP: Do not email form fields that are left blank , but it didn't help much as my email is a table and I don't want an empty table row left in place of an empty field.

我在另一个问题中遇到了类似的东西:PHP:不要通过电子邮件发送空白的表单字段,但它没有多大帮助,因为我的电子邮件是一个表,我不想要一个空的表行代替一个空领域。

This is the html I have for the form:

这是我对表单的html:

<form action ="testemail.php" method="post">
<label>Tube</label>
    <table>
        <tr>
            <th>Type</th>
            <th>Qty</th>
        </tr>
                <td>
                    <select name="tube">
                        <option value=""></option>
                        <option value="Steel">Steel</option>
                        <option value="Aluminum">Aluminum</option>
                    </select>
                </td>
                <td>
                    <input type="number" name ="tubeqty">
                </td>
    </table>
<br>
<br>
<label>Brace</label>
    <table>
        <tr>
            <th>Type</th>
            <th>Qty</th>
        </tr>
                <td>
                    <select name="brace">
                        <option value=""></option>
                        <option value="Steel">Steel</option>
                        <option value="Aluminum">Aluminum</option>
                    </select>
                </td>
                <td>
                    <input type="number" name ="braceqty">
                </td>
    </table>
<br>
<br>
<hr>
<h2>Submit Form</h2>
        <br>
        <h3>If you are sure that everything is filled out correctly, then hit "Submit".</h3>
            <input type="submit" value="Submit">    

The PHP looks like this:

PHP看起来像这样:

<?php
$to = "example@test.com";

$tube = $_POST['tube'];
$tubeqty = $_POST['tubeqty'];
$brace = $_POST['brace'];
$braceqty = $_POST['braceqty'];

$message = <<<EMAIL

<html>
<body>
<table width="600" cellpadding="0" cellspacing="0" align="left">
<tr>
<td width="143">
<h2>Item</h2>
</td>
<td width="10">&nbsp;</td>
<td width="143">
<h2>Type</h2>
</td>
<td width="10">&nbsp;</td>
<td width="143">
<h2>Qty</h2>
</td>
</tr>
</table>
<br>
<table rules="all" cellpadding="0" cellspacing="0" align="left" style= "border: solid black 1px;" "border-collapse:collapse;">
<tr><td  width="143"Tube</td><td width="143">$tube</td><td width="143"></td><td width="143">$tubeqty</td></tr>
<tr><td  width="143">Brace</td><td width="143">$brace</td><td width="143"></td><td width="143">$braceqty</td></tr>
</table>
</body>
</html>

EMAIL;

$subject = "New Order Form";

$headers = "MIME-Version: 1.0\r\nContent-Type: text/html; charset=ISO-8859-l\r\n";

mail($to, $subject, $message, $headers);
?>
<!DOCTYPE html>
<body>
<h1>Thanks for submitting.  We will get on that order ASAP!</h1>
</body>
</html>

Is this something I can do with a PHP function? Do I perhaps need to change the HTML a bit to add class types or better name=""s ? Any help would be extremely appreciated, even just terms that I should Google search.

这是我可以用PHP函数做的事吗?我是否可能需要更改HTML以添加类类型或更好的name =“”s?任何帮助都会非常感激,即使是我应该谷歌搜索的条款。

Thanks in advance.

提前致谢。

1 个解决方案

#1


0  

It looks like you just want to check if each of the submitted form variables are empty() and if not, append the table row to the email message string. You could do with a simple change to your code like this:

看起来您只想检查每个提交的表单变量是否为空(),如果不是,请将表行附加到电子邮件消息字符串。您可以对代码进行简单的更改,如下所示:

$message = <<<EMAIL
<html>
<body>
<table width="600" cellpadding="0" cellspacing="0" align="left">
<tr>
<td width="143">
<h2>Item</h2>
</td>
<td width="10">&nbsp;</td>
<td width="143">
<h2>Type</h2>
</td>
<td width="10">&nbsp;</td>
<td width="143">
<h2>Qty</h2>
</td>
</tr>
</table>
<br>
<table rules="all" cellpadding="0" cellspacing="0" align="left" style= "border: solid black 1px;" "border-collapse:collapse;">
EMAIL;

if (!empty($tube)) {
  $message .= "<tr><td width='143'>Tube</td><td width='143'>$tube</td><td width='143'></td><td width='143'>$tubeqty</td></tr>";
}
if (!empty($brace)) {
  $message .= "<tr><td width='143'>Brace</td><td width='143'>$brace</td><td width='143'></td><td width='143'>$braceqty</td></tr>";
}

$message .= "</table>
</body>
</html>";

You can add more if (!empty($var)) { } checks for additional form fields.

您可以添加更多if(!empty($ var)){}检查其他表单字段。

#1


0  

It looks like you just want to check if each of the submitted form variables are empty() and if not, append the table row to the email message string. You could do with a simple change to your code like this:

看起来您只想检查每个提交的表单变量是否为空(),如果不是,请将表行附加到电子邮件消息字符串。您可以对代码进行简单的更改,如下所示:

$message = <<<EMAIL
<html>
<body>
<table width="600" cellpadding="0" cellspacing="0" align="left">
<tr>
<td width="143">
<h2>Item</h2>
</td>
<td width="10">&nbsp;</td>
<td width="143">
<h2>Type</h2>
</td>
<td width="10">&nbsp;</td>
<td width="143">
<h2>Qty</h2>
</td>
</tr>
</table>
<br>
<table rules="all" cellpadding="0" cellspacing="0" align="left" style= "border: solid black 1px;" "border-collapse:collapse;">
EMAIL;

if (!empty($tube)) {
  $message .= "<tr><td width='143'>Tube</td><td width='143'>$tube</td><td width='143'></td><td width='143'>$tubeqty</td></tr>";
}
if (!empty($brace)) {
  $message .= "<tr><td width='143'>Brace</td><td width='143'>$brace</td><td width='143'></td><td width='143'>$braceqty</td></tr>";
}

$message .= "</table>
</body>
</html>";

You can add more if (!empty($var)) { } checks for additional form fields.

您可以添加更多if(!empty($ var)){}检查其他表单字段。