PHP多个按钮和if语句

时间:2017-01-20 14:29:01

标签: javascript php html email if-statement

所以我不是PHP大师,我几乎没有在其中编码(主要是PHP脚本中的HTML更改)。我正在处理的页面上有一个基本的设置。

该页面有4个按钮,它们都向我们的服务台电子邮件发送电子邮件报告,以报告我们的打印实验室中的任何打印机问题。我有电子邮件发送工作和编码。问题是每个按钮都必须向服务台提交不同的编码电子邮件。

我有四个按钮作为图像(不是很多人喜欢无聊的普通按钮)这里是代码:

<form action="" method="post">

    <br/>
    <br/>
    <h2>Please select issue</h2>
    <br/>


    <input type="image" src="testing/images/need_toner.png"/>
    <input type="hidden" name="toner" id="toner" tabindex="1" />
    <input type="image" src="testing/images/paper_jam.png" /><br>&nbsp;<input type="hidden" name="paper-jam" id="paper-jam" tabindex="2" /><input type="image" src="testing/images/printer_fault.png" />
    <input type="hidden" name="printer-fault" id="printer-fault" tabindex="3" />
    <input type="image" src="testing/images/other.png" />
    <input type="hidden" name="other" id="other" tabindex="4"  />

</form>

所以按钮出来很好,看起来不错,但是当你点击任何按钮时它只执行第一个PHP代码而忽略检查其他按钮。 这是我的PHP代码(请记住,我不是一个真正的PHP人员,我只知道一些通用编码。

<?php

    $url = 'testing/floor4/marketing'; // Setting the URL parameter for PHP

if(isset($_REQUEST['toner']))
{   

    $to      = 'XXX@XXX.com';
    $subject = 'TONER NEEDED - AKRON_32 Marketing Printer';
    $message = 'A request for toner has been reported for printer AKRON_32 on the 4th floor in the marketing department.';
    $headers = 'From: XXX@XXX.com' . "\r\n" .
        'Reply-To: XXX@XXX.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);


    header('Refresh: 6;URL=testing/floor4/marketing' );
    echo '<center><h2>Report Sent! - Page will refresh after a few seconds.</h2></center>';  

}

if(isset($_REQUEST['printer-fault']))
{
    $to      = 'XXX@XXX.com';
    $subject = 'PRINTER FAULT - AKRON_32 Marketing Printer';
    $message = 'A printer fault has been reported for printer AKRON_32 on the 4th floor in the marketing department.';
    $headers = 'From: XXX@XXX.com' . "\r\n" .
        'Reply-To: XXX@XXX.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    header('Refresh: 6;URL=testing/floor4/marketing' );
    echo '<center><h2>Report Sent! - Page will refresh after a few seconds.</h2></center>';
}


if(isset($_POST['paper-jam']))
{
    $to      = 'XXX@XXX.com';
    $subject = 'PAPER JAM - AKRON_32 Marketing Printer';
    $message = 'A paper jam has been reported for printer AKRON_32 on the 4th floor in the marketing department.';
    $headers = 'From: XXX@XXX.com' . "\r\n" .
        'Reply-To: XXX@XXX.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    header('Refresh: 6;URL=testing/floor4/marketing' );
    echo '<center><h2>Report Sent! - Page will refresh after a few seconds.</h2></center>';
}


if(isset($_POST['other']))
{
    $to      = 'XXX@XXX.com';
    $subject = 'OTHER  - AKRON_32 Marketing Printer';
    $message = 'An issue marked "other" has been reported for printer AKRON_32 on the 4th floor in the marketing department.';
    $headers = 'From: XXX@XXX.com' . "\r\n" .
        'Reply-To: XXX@XXX.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    header('Refresh: 6;URL=testing/floor4/marketing' );
    echo '<center><h2>Report Sent! - Page will refresh after a few seconds.</h2></center>';
}

?>

所以代码不是最好的结构我确定如果你可以帮助我那么棒,就像我说我不是PHP编码器。奇怪的是,在我更改回显文本并重新定向标题之前,我最初的编码工作正常,并且它只是执行第一封碳粉电子邮件。

2 个答案:

答案 0 :(得分:2)

所有输入都在同一个表单标记内,因此所有输入都按设置发送。您可以做一些事情,不确定为什么使用$ _REQUEST来说实话。

快速修复和一种稍微更好理解的方法是将它们分成4个单独的表单,并在隐藏字段上添加NAME属性和VALUE。然后检查$ _POST数组以查找错误(NAME)的值(VALUE)。请参阅下面的示例。

<?php
$url = 'testing/floor4/marketing'; // Setting the URL parameter for PHP

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

    $error = $_POST['error'];

    if($error === 'toner' ){echo 'TONER ERROR';}
    if($error === 'paper-jam' ){echo 'PAPER JAM';}
    if($error === 'printer-fault' ){echo 'PRINTER FAULT';}
    if($error === 'other' ){echo 'OTHER';}
}
?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>


<h2>Please select issue</h2>

<form action="" method="post">
    <input type="image" src="testing/images/need_toner.png"/>
    <input type="hidden" name="error" value="toner" id="toner" tabindex="1" />
</form>

<form action="" method="post">
    <input type="image" src="testing/images/paper_jam.png" />
    <input type="hidden" name="error" value="paper-jam" id="paper-jam" tabindex="2" />
</form>

<form action="" method="post">
    <input type="image" src="testing/images/printer_fault.png" />
    <input type="hidden" name="error" value="printer-fault" id="printer-fault" tabindex="3" />
</form>

<form action="" method="post">
    <input type="image" src="testing/images/other.png" />
    <input type="hidden" name="error" value="other" id="other" tabindex="4"  />
</form>

</body>
</html>

你可以做的更多整理更干净,并且有数百种方法可以用PHP做事情,例如你可以为每个错误使用单选按钮,并将它们全部放在一个表格上,如果你我不想要一个提交按钮,你可以使用JavaScript在按下单选按钮时提交表单。这只是众多可能性中的一种。

答案 1 :(得分:0)

$_REQUEST['toner']是指<input type="hidden" name="toner" id="toner" tabindex="1" />。由于所有输入都采用相同的形式,因此始终会发送此输入,因此始终会执行代码的第一部分。

您应该为name元素添加<input type="image" />属性:

<input type="image" name="img_need_toner" src="testing/images/need_toner.png"/>
<input type="hidden" name="toner" id="toner" tabindex="1" />
<input type="image" name="img_paper_jam" src="testing/images/paper_jam.png" /><br>
<input type="hidden" name="paper-jam" id="paper-jam" tabindex="2" />
<input type="image" name="img_printer_fault" src="testing/images/printer_fault.png" />
<input type="hidden" name="printer-fault" id="printer-fault" tabindex="3" />
<input type="image" name="img_other" src="testing/images/other.png" />
<input type="hidden" name="other" id="other" tabindex="4"  />

请注意clicking on an <input type="image" /> will send the x and y coordinates of the click,您需要检查$_POST['img_need_toner_x']$_POST['img_need_toner_y'](如果您点击第一张图片)。