Reputation: 678
How does one link to the submit button, I am thinking of embedding a or just using href or actually have the entire php emailer code embedded somehow inside the submit section?
I can submit the information supplied by a user to a MySQL database and log it there; however, I was just wondering if there is a more elegant way to send email using the information saved in my database by the user after clicking on submit, or would I have to just script some php emailer and use the code below to link to it, so that when the user clicks submit, I have a copy on my database and the other copy is sent to my email in box?
More code can be supplied as requested, thank you for not flaming the newb.
<label for="submit">Submit</label>
<input id="submit" type="submit" value="Send Email!" /><br />
</p>
</form>
Upvotes: 2
Views: 2890
Reputation: 224
How does one link to the submit button
On a webpage, an input element with 'submit' as its 'type' attribute inside a form element will submit the form to the URL specified in the form's 'action' attribute and the response will be loaded in the window supplied in the form's 'target' attribute.
<form action='process.php' target='_blank' id='emailForm'>
<input type='text' name='email' />
<input type='submit' value='Send Email!' />
</form>
The code above will submit what the user enters (upon clicking the 'Send Email!' button) in the 'email' text field to 'process.php' and the page it returns will be shown in a new window.
I am thinking of embedding a or just using href
If you don't want to use a submit button like the example above, the code below will produce the same results as the submit button.
<a href='javascript:document.getElementById("emailForm").submit();'>
<img src='images/submit.jpg' />
</a>
have the entire php emailer code embedded somehow inside the submit section
Unless you're talking about AJAX, there's a clear distinction between the capability of code executed on the client side (JavaScript) and server side (PHP).
JavaScript code is executed from the client side and lets you check for potential errors (and stop a form being submitted) prior to submitting a form, like a space in an email address or letters where the entry is supposed to be numeric.
PHP code is executed from the server side and saves the email address (or whatever else needs saving) in a database. It is also capable of sending emails. For security reasons, you should not depend solely on JavaScript for error-checking and the same checks, like spaces in email addresses and letters where the entry's supposed to be numeric, should be carried out in the PHP too.
I can submit the information supplied by a user to a MySQL database and log it there; however, I was just wondering if there is a more elegant way to send email using the information saved in my database
If I understand you correctly, you now have a form that submit to a PHP file which in turn records the contents of that form (an email address) to a MySQL database which you check from time to time. Now your objective is to have the email address sent to your inbox in addition to being recorded in the MySQL database, is that right? All you need to do is to append this code to your existing PHP file that currently writes to your MySQL database.
$to = "[email protected]";
$subject = "New email address!";
$body = "Hi,\n\nYou have a new email address:\n" . $_REQUEST['email'];
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Different hosting service providers may have different rules governing sending of emails like requiring a from field, or the from field needs to contain an email address matching the domain the form is hosted on, etc. Check with your service provider if you have problems sending mail.
Upvotes: 1
Reputation:
You don't link to a button
element. By clicking a button you trigger an action that is executed in the client (if Javascript is involved) and for the most part on the server. Everthing written in PHP is executed on the server, not in the client.
So, yes, you'd write PHP code to save data to a database, send an email, redirect the browser to a success page, and everything else necessary to handle the request triggered by clicking the button.
Upvotes: 0
Reputation: 17750
Isn't that the whole point of having an action
attribute in your form
?
Use this way:
<form method="post" action="your_page_where_you_send_a_mail"> ... </form>
Upvotes: 0