Javascript PHP多个按钮和if语句

Javascript PHP多个按钮和if语句,javascript,php,html,email,if-statement,Javascript,Php,Html,Email,If Statement,因此,我不是PHP大师,也几乎没有编写过代码(主要是PHP脚本中的HTML更改)。在我正在处理的页面上有一个基本设置 该页面有4个按钮,它们都向我们的服务台发送电子邮件报告,以报告我们打印实验室的任何打印机问题。我有电子邮件发送工作和编码。问题是每个按钮都必须向服务台提交不同的编码电子邮件 我有四个按钮作为图像(不是很多人喜欢无聊的普通按钮),下面是代码: <form action="" method="post"> <br/> <br/>

因此,我不是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 if代码,而忽略检查其他的。 这是我的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程序员。奇怪的是,在我更改回显文本并使其重定向标题之前,我最初的编码工作得很好,没有,它只是执行第一封关于toner的电子邮件。

$\u请求['toner']
指的是
。由于所有输入都是相同的形式,因此始终发送此输入,因此始终执行代码的第一部分

您应该在
元素中添加
名称
属性:

<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"  />



请记住,您需要检查
$\u POST['img\u need\u toner\u x']
$\u POST['img\u need\u toner\u y']
(如果您单击第一张图像)。

$\u请求['toner']
指的是
。由于所有输入都是相同的形式,因此始终发送此输入,因此始终执行代码的第一部分

您应该在
元素中添加
名称
属性:

<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"  />



请记住,您需要检查
$\u POST['img\u need\u toner\u x']
$\u POST['img\u need\u toner\u y']
(如果您单击第一张图像)。

所有输入都在同一个表单标签内,因此所有输入都会按设置发送。您可以做一些事情,但不确定为什么要使用$\u REQUEST以示诚实

一个快速修复方法是将它们分成4个单独的表单,并在隐藏字段中添加名称属性和值。然后检查$\u POST数组中错误(名称)的值(值)。请参见下面的示例

<?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提交表单。这只是许多可能性之一

您所有的输入都在同一个表单标记内,因此所有的输入都是按设置发送的。您可以做一些事情,但不确定为什么要使用$\u REQUEST

一个快速修复方法是将它们分成4个单独的表单,并在隐藏字段中添加名称属性和值。然后检查$\u POST数组中错误(名称)的值(值)。请参见下面的示例

<?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提交表单。这只是许多可能性之一

我不想让自己听起来很傻,但只有第一个图像按钮需要使用x和y?我以前从来没有做过这样的事情,所以我只是想收集更多关于这方面的信息,以便我能更好地理解并使它工作。只要你在
上添加
名称
属性,你就需要检查其x和y。例如,如果单击第二幅图像,则需要检查
$\u POST['img\u paper\u jam\u x']
$\u POST['img\u paper\u jam\u x']
,等等。好吧,那就有点道理了,这对我来说完全不同。我会稍微修改一下这里的脚本,确保一切正常。Sooo…不确定为什么,但页面只是一个白色页面。所有指向图像的链接都很好,但页面只是白色,没有任何内容。我看到的唯一错误是php无法oad mcrypt,没有别的。if是这样的:if(设置($\u POST['img\u need\u toner\u x']和$\u POST['img\u need\u toner\u y']))别担心,我已经弄明白了!非常感谢你,伙计。@Paul我很抱歉,我不能给你答案我第一次尝试了roberto06的解决方案,他也第一次成功了,谢谢你们!我不是想装傻或做任何事,但只有第一个图像按钮需要使用x和y?我以前从未真正做过这样的事情我只是想收集更多关于这方面的信息,以便我能更好地理解它并使它工作。只要你在
上添加
名称
属性,你就需要检查它的x和y。例如,如果你点击第二张图片,你就需要检查
$\u POST['img\u paper\u jam\u x']
$\u POST['img\u paper\u jam\u x']
等等。好吧,这有点道理,这对我来说完全不同。我会在这里稍微修改一下脚本,确保一切正常。所以…不确定为什么,但页面只是一个白色页面。所有指向图像的链接都很好,但页面只是白色,没有任何内容。唯一的错误是