I want to create a form which by pressing the submit button will insert the input to a database and at the same time when the as the site admin I want to get an email notification in my email like " new entry". I can do the first part but I don't have an idea how to notify the admin of the entry via email(Note I do not want the data sent to email, just something to make me aware that someone has submitted the form.
- When someone clicks the button, you can make it do what ever you want. Just add some email code after the database insert. There are many tutorials/guides about sending emails with PHP all over the internet if you do some quick research.M. Eriksson– M. Eriksson2021-05-28 22:14:01 +00:00Commented May 28, 2021 at 22:14
- 1Welcome to stackoverflow. First of all, your question would be easier to read if you check your spelling and insert paragraphs where appropriate. Regarding your question, the button should call a php script (assumed from your tags 'php' and 'html'), and the php script can easily insert a record in the database and inform the admin by mail of this action (without sending the content of the record).BogisW– BogisW2021-05-28 22:14:30 +00:00Commented May 28, 2021 at 22:14
- This is very confusing... When a button is clicked and a form is posted, PHP can do what ever you'd like.. If you're asking if one button can have two different actions independently, then no, two identical requests should not have different outcomes. Try two forms?CodeJunkie– CodeJunkie2021-05-28 22:15:33 +00:00Commented May 28, 2021 at 22:15
- @MagnusEriksson thanks, this is helpful. I'm new to PHP and I'm self-learning. I will definitely try this and see. Thanks a lot for the eye openerMcChris_ Made_It– McChris_ Made_It2021-05-28 22:16:32 +00:00Commented May 28, 2021 at 22:16
- @CodeJunkie Thanks for the info.McChris_ Made_It– McChris_ Made_It2021-05-28 22:18:50 +00:00Commented May 28, 2021 at 22:18
2 Answers
Yes of course, just define a function() and submit your form data to a function such as handleData(); and in this function, first run your query to submit the data into the database. afterwards, write a statement to send you an email notification.
function handleData(){ $sampleData= filter_var($_POST['data'], FILTER_SANITIZE_STRING); $query= "INSERT INTO ..."; //execute query //now send email notification here mail(); } 3 Comments
You can have two actions for one button using JavaScript by assigning a function to your button.
<button onclick="myFunction()" >Button text</button> <script> function myFunction { What your function should do. In your case, insert here your code to send the emial. } </script> As for the sending email automatically with a function there are a variety of ways you could achieve this as well, I recommend searching and looking for some articles on how you can send an email on a function. I would also recommend using server side languages if possible instead of using JavaScript to send emails because if the page source is viewable your credentials and email could be determined easily, try finding an answer using server side languages.