0

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.

6
  • 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. Commented May 28, 2021 at 22:14
  • 1
    Welcome 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). Commented 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? Commented 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 opener Commented May 28, 2021 at 22:16
  • @CodeJunkie Thanks for the info. Commented May 28, 2021 at 22:18

2 Answers 2

3

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(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

That's basically the right approach, but is very insecure, as it doesn't filter input parameters (use php.net/manual/en/function.filter-input.php or something similar), and is open for SQL injection attacs, which are very common and easy to perform.
sure.. sanitizing the input is obvious.. it is just the idea to solve the issue..
Firstly, it's the author's first posting, and he says, he is a beginner in PHP. More importantly, we never post insecure solutions and assume that everybody just knows about that. Please update your answer so that the code is actually secure.
-2

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.

13 Comments

Warning! If you use the code in the first link to send emails (through JavaScript), you've basically given away your email credentials to all visitors, which would be extremely bad. Don't... do.... that! All JavaScript is downloaded to the client (browser), which means that anyone can just read the code and get the credentials in plain text.
Yeah I just realized that, next time I'll make sure to include in the answer to use some sort of external script so the code source isn't viewable.
I highly recommend not sending emails from javascript. Emails should only be sent from server side.
Edit your question and remove that suggestion.
I know, I just wanted to show an easier way to send the emails to give an idea of how you could accomplish something like that. Its a bit easier using javascript than server side languages.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.