[SOLVED] Thanks everyone that replied.
I've got a PHP script that is run after a user submited a contact form.
This script shows a message in the top left corner saying if it worked properly or if it encountered a problem.
Now my question is, how can I put those messages into a popup box?
I know there is JS involved, but I barely have knowledge of that.
( Example: http://www.dylanvanheugten.nl/contact.php)
PHP
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Site Contact';
$to = 'info#dylanvanheugten.nl';
$subject = $_POST['subject'];
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $subject != '' && $message != '') {
if ($human == '12') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>';
} else {
echo '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>';
}
} else if ($_POST['submit'] && $human != '12') {
echo '<p>U heeft de spam preventie som foutief beantwoord.</p>';
}
} else {
echo '<p>Alstublieft alle velden invullen.</p>';
}
}
HTML
<form method="post" action="contact.php">
<label>Naam</label>
<input name="name" placeholder="Naam">
<label>Email</label>
<input name="email" type="email" placeholder="Email">
<label>Onderwerp</label>
<input name="subject" placeholder="Onderwerp">
<label>Bericht</label>
<textarea name="message" placeholder="Laat hier ook uw telefoonnummer achter als u telefonisch contact wilt."></textarea>
<label><strong>*Spam preventie*</strong><br/>Wat is 11+1?</label>
<input name="human" placeholder="11 + 1 =">
<input id="submit" name="submit" type="submit" value="Verzend">
<label><u>Alle velden zijn verplicht.</u></label>
</form>
Enclose your echo with <script> tags like this.
echo '<script>alert("Alstublieft alle velden invullen");<script>';
In the same way that you can print HTML from PHP, you can 'print' javascript. Here's an example with an alert box:
print("<script>window.alert('This is a javascript alert from PHP');</script>");
You are able to put your PHP variables inside of the statement, as well, if you need to.
You can simply alert msg like,
$msg='';
if ($_POST['submit']) {
if ($name != '' && $email != '' && $subject != '' && $message != '') {
if ($human == '12') {
if (mail ($to, $subject, $body, $from)) {
$msg='Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug';
} else {
$msg='Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.';
}
} else if ($_POST['submit'] && $human != '12') {
$msg='U heeft de spam preventie som foutief beantwoord.';
}
} else {
$msg='Alstublieft alle velden invullen.';
}
echo '<script>
alert("'.$msg.'");
</script>';
// you can replace the above javscript code to any plugin like jquery ui modal box
}
Add some javascript to the top of your html markup where you define a method to display a popup. Something like this:
<script type="text/javascript">
function displayPopup()
{
alert("Form submitted!");
}
</script>
Then within your PHP when the form has been submitted, just call the method displayPopup() method within a script tag.
You can certainly optimise the approach above, but it should give you somewhere to start.
Store the error message in a variable and simply echo them in the required div.
if ($_POST['submit'])
{
if ($name != '' && $email != '' && $subject != '' && $message != '')
{
if ($human == '12')
{
if (mail ($to, $subject, $body, $from))
{
$message = '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>';
} else
{
$message = '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>';
}
} else if ($_POST['submit'] && $human != '12')
{
$message = '<p>U heeft de spam preventie som foutief beantwoord.</p>';
}
}
else
{
$message = '<p>Alstublieft alle velden invullen.</p>';
}
}
And in your HTML, you can create a and display the error inside it:
<div id="errorMessage"> <?php echo $message; ?> </div>
Try with this code
if ($query) {
echo "<script type='text/javascript'>alert('data Updated successfully!'); window.location.href = 'home.php';</script>";
} else {
echo "<script>alert('ERROR! Something Went wrong. Try Again')</script>";
}
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I've tried to built a php form. However, it won't send.
html (index.html):
<div class="row">
<div id="Contact" class="container">
<h1>Meer weten?</h1>
<h2>Neem vrijblijvend contact op</h2>
<div id="ContactBlock">
<form class="contactform" method="post" action="submitform.php">
<div class="FormTop">
<div class="half-left-cf">
<input type="text" id="input-name" name="name" placeholder="Naam (verplicht)">
<input type="text" id="input-email" name="email" placeholder="Email address (verplicht)">
<input type="text" id="input-telnr" name="telnr" placeholder="Telefoonnummer">
</div>
<div class="half-right-cf">
<textarea name="message" type="text" id="input-message" name="message" placeholder="Uw bericht"></textarea>
</div>
</div>
<label>*Hoeveel is is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Antwoord hier">
<input type="submit" value="Verzenden" id="input-submit">
</form>
</div>
</div>
php (submitform.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$telnr = $_POST['telnr'];
$message = $_POST['message'];
$from = 'From: Contact Form';
$to = 'e-mail';
$subject = 'Hello';
$human = $_POST['human'];
$body = "Afkomstig van: $name\n E-Mail: $email\n Telnr:\n $telnr Bericht:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Uw bericht werd goed verzonden. Wij contacteren u zo snel mogelijk.</p>';
} else {
echo '<p>Er is iets misgegaan, probeer a.u.b. opnieuw!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>U heeft de anti-spam vraag niet correct ingevuld</p>';
}
} else {
echo '<p>U heeft niet alle verplichte velden ingevuld</p>';
}
}
?>
When I press submit the browser returns a blanc page with /.../submitform.php in the url-bar.
This happens both in an online environment as well as on localhost.
Am I doing something wrong? Thanks
If you not sure about your folder structure just use this simple trick.
Change post url to absolute one like http://www.example.com/xyz/submitform.php
In local
http://www.localhost/xyz/submitform.php
Instead of trying simple submitform.php
The form action is linked to this php script:
<?php
$email = $_POST['subscribefield'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Dit adres klopt niet";
die();
}
$to = "flash1996mph#hotmail.com";
$subject = "Abonee voor de nieuwsbrief";
$body = "$email \n Heeft zich aangemeld voor de nieuwsbrief";
mail($to, $subject, $body);
echo "U bent succesvol aangemeld voor de Vandenberg nieuwsbrief";
echo $_SERVER['REMOTE_ADDR'];
?>
At the end i added echo $_SERVER['REMOTE_ADDR']; then i checked my mail, but there was nothing. Did i use the code the wrong way?
EDIT:
<?php
$email = $_POST['subscribefield'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Dit adres klopt niet";
die();
}
$to = "flash1996mph#hotmail.com";
$subject = "Abonee voor de nieuwsbrief";
$body = "$email \n Heeft zich aangemeld voor de nieuwsbrief";
$body .= $_SERVER['REMOTE_ADDR'];
mail($to, $subject, $body);
echo "U bent succesvol aangemeld voor de Vandenberg nieuwsbrief";
?>
The above is an edit^
When mail is called the email is sent. In your script $body is the body of the email so append the IP address on to that variable before you call the mail function.
$to = "flash1996mph#hotmail.com";
$subject = "Abonee voor de nieuwsbrief";
$body = "$email \n Heeft zich aangemeld voor de nieuwsbrief";
$body .= $_SERVER['REMOTE_ADDR'];
mail($to, $subject, $body);
The echo currently just outputs the IP on the browser.
I have created a contact form, you can view it here. When I fill out the contact form and go to my inbox folder - the Norwegian letters æ, ø, å aren't shown in the message.
This is what I have currently added:
<meta http-equiv="content-type" content="text/html" charset="ISO-8859-1">
I have also tried <form accept-charset="ISO-8859-1">, but with no luck.
Here is a screenshot from the e-mail I receive after contact form has been submitted
as you can see there are no æ, ø, å letters.
Is there any way I can fix this?
PHP Code:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$number = $_POST['number'];
$from = 'Ny melding sendt fra kontaktskjema på Helsespesialisten.no';
$to = 'test#test';
$subject = 'Helsespesialisten | Du har motatt en ny melding';
$body = "Fra: $name\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Vennligst skriv inn ditt navn';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vennligst skriv inn din e-post';
}
//Check if message has been entered
if (!$_POST['number']) {
$errNumber = 'Vennligst skriv inn ditt telefonnummer';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Vennligst skriv en melding';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
if (mail ($to, $subject, $body, $from, $number)) {
$result='<div class="alert alert-success">Takk for din henvendelse! Vi tar kontakt i løpet av kort tid!</div>';
} else {
$result='<div class="alert alert-danger">Beklager, en feil skjedde! Kontakt oss på: +47 35 11 15 40</div>';
}
}
}
Try this:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$number = $_POST['number'];
$from = 'Ny melding sendt fra kontaktskjema på Helsespesialisten.no';
$to = 'test#test';
$subject = 'Helsespesialisten | Du har motatt en ny melding';
$headerFields = array(
"From: $from",
"MIME-Version: 1.0",
"Content-Type: text/html;charset=utf-8"
);
$body = "Fra: $name\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Vennligst skriv inn ditt navn';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vennligst skriv inn din e-post';
}
//Check if message has been entered
if (!$_POST['number']) {
$errNumber = 'Vennligst skriv inn ditt telefonnummer';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Vennligst skriv en melding';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
if (mail ($to, $subject, $body, implode("\r\n", $headerFields))) {
$result='<div class="alert alert-success">Takk for din henvendelse! Vi tar kontakt i løpet av kort tid!</div>';
} else {
$result='<div class="alert alert-danger">Beklager, en feil skjedde! Kontakt oss på: +47 35 11 15 40</div>';
}
}
}
I have the following contact form:
(HTML with PHP)
<form method="post" action="index.php">
<p>
<label>Namn</label>
<input name="name" placeholder="Skriv här">
<label>Epost adress</label>
<input name="email" type="email" placeholder="Skriv här">
<label>Meddelande</label>
<textarea name="message" placeholder="Skriv här"></textarea>
<label>Hur mycket är 2+2? (Anti-spam)</label>
<input name="human" placeholder="Skriv här">
</p>
<p>
<input id="submit" name="submit" type="submit" value="Submit">
</p>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Från: Forall.se';
$to = 'info#forall.se';
$subject = 'Ny meddelande';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Ditt meddelande har skickats!</p>';
} else {
echo '<p>Någonting gick fel. Var vänlig och försök igen!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>Du har gett fel svar på Anti-Spam frågan!</p>';
}
} else {
echo '<p>Du behöver att fylla alla fält i formuläret!</p>';
}
}
?>
</form>
Everything is woring as it should, except non English characters like ã, õ, ä, å, ö, ç and so on. This shouldn't be a problem if it wasn't a swedish website.
Here's a sample of the last e-mail I received:
"Jag hoppas du har förståelse för detta och att du hittar någon annan marknadsförare!
Jag återkommer när och om jag startar upp min firma, just nu är det lite osäkert hur det blir med dettas"
How do I solve this issue?
you need to add headers
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Från: Forall.se';
$to = 'info#forall.se';
$subject = 'Ny meddelande';
$human = $_POST['human'];
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "From: $from <$email> ". PHP_EOL;
$headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;
$name = str_replace( '[at]','#', $name);
$message = str_replace( '[at]','#', $message);
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
if ($human == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Ditt meddelande har skickats!</p>';
} else {
echo '<p>Någonting gick fel. Var vänlig och försök igen!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>Du har gett fel svar på Anti-Spam frågan!</p>';
}
} else {
echo '<p>Du behöver att fylla alla fält i formuläret!</p>';
}
}
?>
PHP mail function 4th parameter isn't exclusive to send a from value. Acording with PHP's manual:
-This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).
Including charset which will encode the text of the message. In your case, as the comments says, you'll have to use UTF-8.
So, your code would be like this:
$header = "From: somebody#example.com\r\nContent-Type: text/plain; charset=UTF-8";
$message = $_POST['message'];
$to = 'info#forall.se';
$subject = 'Ny meddelande';
mail ($to, $subject, $body, $header)
I'm building a php contact form that checks if some fields are filled it or not (no ajax).
The form is not working for 100%. I will try to explain. When I submit my form, The user navigates to http://mydomain.com/send.php
There is receive my error messages (if there are any). When I refresh the send.php page I receive the following errors: Notice: Undefined index: name in /Library/WebServer/Documents/~aledvertising/send.php
Here is my code
html form
<form method="post" action="send.php">
<div id="form-top">
</div>
<div id="form-left">
<label>Naam:<span class="star">*</span></label>
<input name="name" placeholder="Uw naam">
<label>Email:<span class="star">*</span></label>
<input name="email" type="email" placeholder="Uw email">
<label>Hoeveel is 2+2? (Anti-spam)<span class="star">*</span></label>
<input name="human" placeholder="Uw antwoord">
</div>
<div id="form-right">
<label>Uw bericht:<span class="star">*</span></label>
<textarea name="message" placeholder="Uw bericht"></textarea>
</div>
<input id="submit" name="submit" type="submit" value="Verzenden">
</form>
SEND.PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
?>
<?php
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
Can somebody please help me to get a full working form? Thank you
You should see isset method from php.
<?php
if(isset($_POST["name"]))
{
//code here
}
?>
The problem is that when you reload the page, the server side code looks for the index name and it does not find it because name does not have any value yet. The value is received once you submit the page. So that's why the error is there.
update
The error is that when you submit the form, there are some fields that may be send empty, so once that the post values goes to send.php there are $_POST that will be expecting values. if some of them do not recive does values, it will tell you: "Hey, i do not have values, so i im null". That why you have to check that the fields are all filled or make a validation on server side.
<?php
if(isset($_POST["name"],$_POST["last_name"],etc,etc))
{
//if everything ok, ill go on.
}
else{
//if there are empty field, go back
}
?>
UPDATE 2
if (isset($_POST['name'],$_POST['email'],$_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
setting the isset() function and then declaring the variables worked for me, tested it in my server, not giving any errors and form is submitted successfully...
please check it once...