Reputation: 57
[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)
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Site Contact';
$to = '[email protected]';
$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>';
}
}
<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>
Upvotes: 4
Views: 27217
Reputation: 1
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>";
}
Upvotes: 0
Reputation:
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>
Upvotes: 0
Reputation: 525
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.
Upvotes: 0
Reputation: 68556
Enclose your echo
with <script>
tags like this.
echo '<script>alert("Alstublieft alle velden invullen");<script>';
Upvotes: 3
Reputation: 40639
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
}
Upvotes: 0
Reputation: 81
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.
Upvotes: 2