There are quite a few examples out there, but I can’t seem to get them to work and they seem overly complex for the simple thing I need to do because the code presented in those examples is far more complex as well. Basically, I have a form that, when submitted, it processes the data in a presentable format. On the bottom of the processed page, I want two buttons. One to e-mail the information (which is already stored in variables) and one to print the page. I have the following two buttons:
<button id="emailButton" name="emailButton" class="btn btn-success hidden-print">Email Record</button> <button id="printButton" name="printButton" class="btn btn-primary hidden-print" onClick="window.print()">Print Record</button>
The print button works great. However, for the e-mail button I just want to run the PHP function: sendEmail($email, $subject, $body);
This e-mail function works fine if I just call it on the page (it’s part of the SendInBlue service). I’ve tried wrapping the button in a form, but then when I click it all the information on the page except the buttons vanish. I’ve also tried several javascript things I could think of like adding an event listener to the button, but I’m either not doing it correctly or that’s not the correct route to go. I’ve also tried using it in the onClick method with the around it. I believe I might be overthinking it. Is there an easy way to make that button execute that one PHP command? How is this type of thing normally done?
Advertisement
Answer
As PHP is a server-side language – a button can’t directly trigger a PHP function.
Either make a simple form that submits back to the server and then triggers the sendEmail()
function, or alternatively you’ll need to look at using ajax to allow JavaScript to send a message to a PHP script and do the necessary processing.
Perhaps post the form you’ve already tried that causes content to vanish for us to help more accurately.