2017-01-20 5 views
0

私はPHPの専門家ではなく、ほとんど(それはPHPスクリプト内のほとんどのHTMLの変更)をコード化しています。私は作業しているページに基本的なセットアップがあります。PHPの複数のボタンとif文

ページでは4つのボタンがあり、それらはすべて、当社のプリントラボで任意のプリンタに関する問題を報告するために当社のサービスデスクの電子メールに電子メールレポートを送信しています。電子メールを送信し、コード化しました。問題は、各ボタンが異なるコード化された電子メールをサービスデスクに提出しなければならないことです。

私はここの画像(退屈な通常のボタンのような人々のないたくさん)など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の人は、私はちょうどいくつかの一般的なコーディングを知っているわけではない覚えておいてください。

<?php 

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

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

    $to  = '[email protected]'; 
    $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: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\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  = '[email protected]'; 
    $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: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\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  = '[email protected]'; 
    $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: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\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  = '[email protected]'; 
    $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: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\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のコーダーがないと言ったように素晴らしいと思います。エコーテキストを変更して、ヘッダーをリダイレクトする前に元々コーディングが正常に機能していたのはちょっと変です。

答えて

0

$_REQUEST['toner']<input type="hidden" name="toner" id="toner" tabindex="1" />を参照しています。すべての入力が同じ形式になっているため、常にこのコードが送信されるため、コードの最初の部分が常に実行されます。

あなたの<input type="image" />要素にname属性を追加する必要があります(あなたが最初の画像をクリックした場合)clicking on an <input type="image" /> will send the x and y coordinates of the clickことを心に留めておく

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

、あなたは$_POST['img_need_toner_x']$_POST['img_need_toner_y']をチェックする必要があります。

+0

私はダムなど何も聞こえませんが、最初の画像ボタンだけがxとyを使用する必要がありますか? 私はこの前にこのようなことをしたことはありませんでしたので、私はちょうどこれについてより多くの情報を収集しようとしています。 – Paintballer4lfe

+0

''に 'name'属性を追加する限り、xとyをチェックする必要があります。たとえば、2番目のイメージをクリックすると、 '$ _POST ['img_paper_jam_x']'と '$ _POST ['img_paper_jam_x']'などを確認する必要があります。 – roberto06

+0

ちょっとちょっと意味が分かりますが、これはまったく違うものです。私は少しここでスクリプトを変更し、それがすべて良いことを確認します。 – Paintballer4lfe

2

すべての入力は同じフォームタグ内にあるので、すべての入力が設定として送信されます。あなたが$ _REQUESTを正当に使用している理由がわからない場合、あなたはできることがいくつかあります。

クイックフィックスと4つの別々のフォームにそれらを分離して、あなたの隠されたフィールド上のNAME属性と値を追加することですあなたに少しより良い理解を与えるための方法。エラー(NAME)の値(VALUE)が何であるかを$ _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を使用してフォームを送信することができる送信ボタンが必要ではありませんでした。それは多くの可能性の一つに過ぎません。

+0

私は新しいテストページであなたのコードを試して、メールコードとすべてをコピーして、これはすべての電子メールを送信し、それらのいずれかをクリックするとすべてのボタンをエコーするようです。うん、私のコードは、ちょうど私がハハを見つけたサイトからスニペットを一緒に投げ始めたのでかなり乱雑です。 – Paintballer4lfe