successful/fail message pop up box after submit?
You are echoing outside the body tag of your HTML.
Put your echos there, and you should be fine.
Also, remove the onclick="alert()"
from your submit. This is the cause for your first undefined
message.
<?php
$posted = false;
if( $_POST ) {
$posted = true;
// Database stuff here...
// $result = mysql_query( ... )
$result = $_POST['name'] == "danny"; // Dummy result
}
?>
<html>
<head></head>
<body>
<?php
if( $posted ) {
if( $result )
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
else
echo "<script type='text/javascript'>alert('failed!')</script>";
}
?>
<form action="" method="post">
Name:<input type="text" id="name" name="name"/>
<input type="submit" value="submit" name="submit"/>
</form>
</body>
</html>
How to get a popup box after submitting a form?
Enclose your echo
with <script>
tags like this.
echo '<script>alert("Alstublieft alle velden invullen");<script>';
Alert message after submitting form in PHP
After long researching, I found solution to delay the alert message with jQuery delay()
function, the delay allowing the HTML page load and then execute the alert.
Thanks to all who helped me to get to this result.
<script>
$(document).ready(function(){
setTimeout(function() {
<?php
if( $_GET['status'] == 'success') {
echo 'alert("welldone");';
}
else{
echo 'alert("no good");';
}
?>
}, 500);
});
</script>
Pop-up message after submitting a form with php
To pass values using ajax. Form:
<form id="form">
<input type="text" value="test" name="akcija">
</form>
All inputs fields values in your form will be passed.
Ajax:
jQuery(function(){
$('#form').on('submit', function (e) { //on submit function
e.preventDefault();
$.ajax({
type: 'post', //method POST
url: 'yoururl.php', //URL of page where u place query and passing values
data: $('#form').serialize(), //seriallize is passing all inputs values of form
success: function(){ //on success function
$("#input").attr("disabled", true); //example
$("#input").removeClass('btn-primary').addClass('btn-success');//example
},
});
}
});
And on the ajax page you can get values by
$akcija = $_POST['akcija']
Getting the alert message after submit the form using ajax
<script>
$(function () {
$("#myForm").submit(function (evenr) {
event.preventDefault(e);
var form_data = $(this).serialize();
$.ajax({
url:'mail.php',
method:'POST',
data:{data},
success:function(data){
alert(data);
if($.trim(data))
{
alert("Mail was sent !")
$("#success_message").attr("style", "display: none");
}else{ alert("Mail was not sent !")}
}
});
});
});
</script>
Ajax File changes:
if(isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['service'])) {
$to = 'test@gmail.com';
$subject = "{$_POST['name']} " . ' Requested the Pest Control Services';
$message = '
<html>
<head>
<title>Pest Control Services</title>
</head>
<body>
<p><b>Name:</b> ' . $_POST['name'] . '</p>
<p><b>Phone:</b> ' . $_POST['phone'] . '</p>
<p><b>Email:</b> ' . $_POST['email'] . '</p>
<p><b>Services Type:</b> ' . $_POST['service'] . '</p>
</body>
</html>';
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From:{$_POST['name']} {$_POST['email']}\r\n";
if(mail($to, $subject, $message, $headers)){
echo "1"; exit;
else{
echo "0"; exit;
}
}
Related Topics
How to Save Binary Buffer to Png File in Nodejs
How to Output a Pdf Buffer to Browser Using Nodejs
How to Kill a Running Process on Windows from Nodejs
How to Clear Input After Onclick With Reactjs
How to Find Previous or Next Element from Array in JavaScript
How to Add a Dynamic Action That Will Redirect to Another Page by Executing JavaScript Code
Html Input File Selection Event Not Firing Upon Selecting the Same File
Why Does My Function Return Undefined Whilst I Do Give It an Argument
How to Add Active Class/State for Li Element in Reactjs
Handling Multiple Http Responses One by One in Angular 6
Javascript: Location.Href to Open in New Window/Tab
Changing the Value of Json Object's Key, Changes Other Values Also
How to Create Expand/Collapse Function for Each Row of Table - Angular6
How to Execute a Function After Another One
How to Pass the Checked and Unchecked Value of Checkboxes in Component.Ts File in Angular
How to Redirect Drop Down Menu When Option Is Selected
Javascript Regex - No White Space At Beginning + Allow Space in the Middle
Display HTML Form Values in Same Page After Submit Using Ajax