92-3kgt Posted January 20, 2017 Share Posted January 20, 2017 So i am no PHP guru and i have barely coded in it (mostly HTML changes inside a PHP script). I have a basic setup on the page i am working on. The page has 4 buttons, they are all sending an email report to our service desk email to report any printer issues in our print labs. I have the email sending working and coded. The issue is that each button has to submit a different coded email to the service desk. I have the four buttons as images (not a lot of people like the boring normal buttons) here is the code: <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> <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> So the buttons come out fine and look good and all that but when you click on any of the buttons it only executes the first PHP if code and ignores to check the others. Here is my PHP code (keep in mind i am not really a PHP person, i just know some general coding. <?php $url = 'testing/floor4/marketing'; // Setting the URL parameter for PHP if(isset($_POST['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($_POST['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>'; } ?> So the code isn't the best structured i am sure so if you can help me out that'd be awesome, like i said i am no PHP coder. It's just weird how i originally had the coding working fine before i changed the echo text and had it redirect the headers no it just ONLY executes the first email for toner. I am also very sorry this script is sloppy and potentially not structured correctly?I've been piecing together snippets from different sites Quote Link to comment https://forums.phpfreaks.com/topic/302989-php-image-button-if-statements/ Share on other sites More sharing options...
Solution requinix Posted January 20, 2017 Solution Share Posted January 20, 2017 Hidden inputs are all sent with the form when it's submitted. However submit buttons ( and ) and image buttons () will only send themselves when they are used to submit the form - and only one of those can happen at a time. So what you need to do is figure out what image button was clicked. 1. Remove the hidden inputs since they're not helping you. 2. Give names to the image buttons (eg, "toner"). 3. Look for each button to tell which was clicked. Note that there will not be a "toner" but instead "toner_x" and "toner_y" values. Quote Link to comment https://forums.phpfreaks.com/topic/302989-php-image-button-if-statements/#findComment-1541720 Share on other sites More sharing options...
Psycho Posted January 20, 2017 Share Posted January 20, 2017 requinex beat me to it, but I'll post the sample code I was working on: <form action="" method="post"> <br/><br/> <h2>Please select issue</h2><br/> <input type="image" src="testing/images/need_toner.png" name="toner"/> <input type="image" src="testing/images/paper_jam.png" name="paper-jam"/><br> <input type="image" src="testing/images/printer_fault.png" name="printer-fault"/> <input type="image" src="testing/images/other.png" name="other"/> </form> if(isset($_POST['toner_x'])) { echo "Perform the Toner action"; } if(isset($_POST['paper-jam_x'])) { echo "Perform the Paper jam action"; } if(isset($_POST['printer-fault_x'])) { echo "Perform the Printer Fault action"; } if(isset($_POST['other_x'])) { echo "Perform the Other action"; } Replace the echo statements with the relevant code for each option. Also, the two "fields" that are returned when clicking a button (fieldName_x 7 fieldName_y) will have values for the x & y positions that the mouse clicked on the relevant image. Put this on your page to "see" how it works: echo "<pre>" . print_r($_POST, 1) . "</pre>"; 1 Quote Link to comment https://forums.phpfreaks.com/topic/302989-php-image-button-if-statements/#findComment-1541722 Share on other sites More sharing options...
92-3kgt Posted January 20, 2017 Author Share Posted January 20, 2017 I'm not sure who to thank first haha but thank you guys both!!! I posted on Stack overflow but most of what they were doing/saying didn't quite make sense (and one of the snippets someone posted didn't work either). I owe you guys a beer or something! Quote Link to comment https://forums.phpfreaks.com/topic/302989-php-image-button-if-statements/#findComment-1541725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.