I'm having this problem. Look at this: http://santz.net/index.contacto.html
Try sending whatever and see what happens (it's mine, I recieve it, send whatever no problem...). (It leaves the page, shows a dialog that says thanks for contacting us... and it redirects you to the same page). I HATE THIS!!!
I'm looking fore some AJAX and jQuery code that after the message is sent, it clears the form and opens a dialog (the common one... like the typical loggin boxes) and that fades the page and that show some "x" content...
The thing is that I don't know how to do any of this things and I'm driving crazy! If you could give me the code and tell me where to put it or just give a tutorial for noobs or something like that it would be great...
I leave here the PHP code I'm using:
<?php
$field_name = $_POST['php_name'];
$field_email = $_POST['php_email'];
$field_phone = $_POST['php_phone'];
$field_message = $_POST['php_message'];
$field_sender = 'alpha#hotmail.com';
$mail_to = 'gama#hotmail.com.ar';
$subject = 'Mensaje via Santz.net de '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_sender."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Gracias por contactarse, en breve, me pondre en contacto.\n\nSantz Design | www.santz.net');
window.location = 'index.contacto.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('El envio fallo. Por favor, envie un mail directamente a info#santz.net');
window.location = 'index.contacto.html';
</script>
<?php
}
?>
Try change this:
window.location.assign('http://www.santz.net/');
instead of:
window.location = 'index.contacto.html';
Showing error message on the same page, follow this example:
Add validation message in fieldset instead of js popup
And for a modal windows, use a plugin like this one:
http://www.mywebdeveloperblog.com/my-jquery-plugins/modalpoplite
Demo:
http://www.mywebdeveloperblog.com/projects/modalpoplite/demo/
saludos ;)
Why dont you use jQuery
$('input[type="submit"]').click(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: contact.php,
data: $('#contact-form"').serialize();,
success: function() {
//modal
}
});
});
something along those lines would be the ajax way of submitting your form and then pop up a window to the user saying thanks please view my form to get an idea just without popup
Related
I have a form on my website, that is used to reset a users password. When the button is pressed, it calls this:
if($getreq == 'true') {
//Begynder: Sender mail til brugeren
$headers = 'Content-type: text/html; charset=iso-8859-1';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = 'FROM: <BLANK>';
$recipient = $getusermail;
$subject = "Password reset";
$message = "Password has been reset";
mail($recipient, utf8_decode($subject), utf8_decode($message), $headers);
}
Everything works fine, but I would like some sort of message like "Sending..." or "Wait while sending mail" - just to notify the user that the browser is working (see image; https://i.stack.imgur.com/nd37W.png). Is this possible? I have tried google it, but havn't been able to find it (maybe I search the wrong words). I thought that I could just 'echo' before mail(), but the mail action always comes first.
<a class="button_link_small" href="edit.php?change=user&step=edit&postid=<?php echo $getPostID?>&ResetUserPass&req=true">Reset password</a>
if($getreq == 'true') {
echo "Sending mail"
//Begynder: Sender mail til brugeren
Can someone help me with a solution, and have in mind, that I need it guided out. :-) If more code shown is needed, just say.
Dude to 'Don't Panic answer I came up with this little work around:
if($getAction == 'ResetUserPass'){?>
<script type="text/javascript">
var count = 5;
var redirect = "edit.php?change=user&step=edit&postid=<?php echo $getPostID?>&req=true";
function ResetCount(){
var timer = document.getElementById("ResetCount");
if(count > 0){
count--;
timer.innerHTML = "("+count+")";
setTimeout("ResetCount()", 1000);
}else{
window.location.href = redirect;
}
}
</script>
<?php redtext("System is working, await green infobox...<span id=\"ResetCount\"><script type=\"text/javascript\">ResetCount();</script></span>");
}
if($getreq == 'true') {
doing stuff... and..
sending mail here...
greentext("Bla bla bla, system is done.");
}
Prob not the best solution, but it works for me. :-)
I cant understand why the following is not working, I am trying to call a php script to send an email, but i dont think my ajax call is calling the php script at all, I just get an error returned.
My ajax call:
var name_field = $('#cf_name').val();
$.ajax({
type: 'post',
url: 'contact.php',
data: {'name_field' : name_field },
dataType: 'json',
success: function(data) {
if(data.status == 'success')
alert("Thank you for subscribing!");
else if(data.status == 'error')
alert("Error on query!");
},
error: function(err) {
alert(err.responseText);
}
});
My php script:
$field_name = isset($_POST['name_field']);
$field_email = 'name#email.com';
$field_phone = '12345';
$field_message = 'Hello World!';
$mail_to = 'emailaddress#email.com';
$subject = 'Message from a website visitor '.$field_name;
$body_message = 'This message has been sent via website:'."\n\n";
$body_message .= 'From: '.$field_name."\n\n";
$body_message .= 'E-mail: '.$field_email."\n\n";
$body_message .= 'Phone Number: '.$field_phone."\n\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {
$response_array['status'] = 'success';
}
else {
$response_array['status'] = 'error';
}
header('Content-type: application/json');
echo json_encode($response_array);
If i navigate to the url directly the php works as intended, but i can not get the ajax post to call it.
$field_name is not an actual string, it's a boolean since you used $field_name=isset($_POST[name_field']). Remove the isset().
See this: jQuery Ajax post cancelled i'm not a js expert but I agree with your problem being the async execution of your ajax call, since it takes a while to send an email, maybe isn't completed in time for your js
See this nice explanation about the "async" propertie in the ajax call:
What does "async: false" do in jQuery.ajax()?
And see this question/answer:
How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
You should check url: contact.php as you did not specify the full URL, so your post is going to a relative path. The root path will be same current script path.
I've been looking around for ages and haven't found an easy way of editing this code so that when the user hasn't typed anything into a required field (all of the fields are required) an alert is shown asking the user to please enter something.
Can anyone help?
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'you#yourdomain.com';
$subject = 'Site Mail';
$body_message = 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thanks! Your email has been sent.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, something went wrong.');
window.location = 'index.html';
</script>
<?php
}
?>
You can use required attribute if you want to take advantage of html5 features.
Try this LINK. It shows a simple demo of how to use the required attribute.
This one too
PHP
<?php
$fields = array([0]=>$field_name,[1]=>$field_email,[2]=>$field_message);
foreach($fields as $field){
if !$field{
echo "Please enter a value.";
}
}
?>
Alternatively, you could use javascript validation, that would look something like this:
<script>
function formValid(){
var valid = true;
var fields = getElementByTag(input);
for (var i;i>=fields.length;i++){
if (!fields[i]){
getElementById('warning').style.display='inline'; // assuming the error message element is called 'warning'
getElementById('valid').value=1; // assuming we have an input that will indicate to the php code whether the form is validated
}
}
}
</script>
for this script we would then add to the beginning of the form-action php block:
if(isset($_POST['submit'])&&$_POST['valid']!=1){
// form action code goes here
}
use the code like below:-
$('.required').each(function(){
if($(this).val()=="")
{
alert("error");
}
});
The most common way to check for required fields is both on the client side (to inform the user) and the server side (to check the data is legit before we send it).
On the client side, at the point at which the form is submitted we should check to see if the fields are correct and ask the user to fix mistakes. There are a bunch of great helper libraries available to do this, a couple of the more common libraries are http://parsleyjs.org/ and http://jqueryvalidation.org/ both use jQuery and have easy examples to get started.
On the server side, as some commenters have noted, your current script is vulnerable to header injection and other nasty stuff. You might want to switch to using a library such as swiftmailer which will prevent a lot of bad stuff happening. Here is a really simple example of how to send email http://swiftmailer.org/docs/sending.html
To add server side validation to you example, as simple approach would be to check the values of $_POST['name']; before calling $mailer->send($message);
I have been trying to implement server validation to prevent blank emails in my contact us page, but I am not sure on how to do it in PHP, here is my code:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_tel = $_POST['cf_tel'];
$field_message = $_POST['cf_message'];
$mail_to = 'test#test.com, test#test.com, test#test.com';
$subject = 'Just iStuff Mobile Contact Us: '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Telephone Number: '.$field_tel."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your email, we have received your message and will reply within the next few days.');
window.location = 'contactus.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed, please try again or email test#test.com');
window.location = 'contactus.html';
</script>
<?php
}
?>
Can anyone help me to do this, the tutorials online do not cover this way of doing it...
Thanks
Before your $mail_to..
You can validate the _POST/_GET first on server side.
<?php
if (empty($field_name) && empty($field_email) && empty($field_tel) && empty($field_message)) {
echo 'Please correct the fields';
return false;
}
?>
Alternatively, you can validate first on the client-side. It will save you time and resources.
just test the variable for "emptiness" and exit early. Something like this:
if(empty($field_email)) {
// maybe show the user a reason why this was rejected...
return;
}
You probably want to do this for just about all the input fields.
In addition, you can use JavaScript (jQuery has some nice plugins) to prevent the user from submitting invalid data in the first place. This won't remove the need to do it server side (since they can just disable JS, or someone malicious might intentionally bypass this measure), but it can make it a more user friendly experience.
You can use filter for this; since you're using the passed email address as part of the mail() operation, it's best to also validate:
$fields = filter_input_array(INPUT_POST, array(
'name' => FILTER_UNSAFE_RAW,
'email' => FILTER_VALIDATE_EMAIL,
'tel' => FILTER_UNSAFE_RAW,
'message' => FILTER_UNSAFE_RAW,
));
// check for missing fields
if (null === $fields || in_array(null, $fields, true)) {
// some or all fields missing
} elseif (in_array(false, $fields, true)) {
// some or all fields failed validation
} else {
// all fields passed validation
// use $fields['email'] as the email address
}
I've used FILTER_UNSAFE_RAW for all fields except email, but perhaps there are better filters that apply.
Try to put a submit input <input type="submit" name="sub" value="Submited"> inside your form
when it's clicked.
<?php
if (isset($_POST['sub']) {
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_tel = $_POST['cf_tel'];
$field_message = $_POST['cf_message'];
if (empty($field_name) && ....)
{
exit('Field name is empty');
}
.....
I have an html form within a flash site. The form is php processed. Help me fix the php file so that after processing the form, the php returns the user to the flash page and reloads the html form. I tried a window.location in the php to send to the page but that did not work. It results in a white rectangle within the html code area (maybe frame but I'm not sure). How can I get just that html form reloaded? I do not want to load the entire page using a URL because that is too slow and I cannot replicate the flash state. BTW, below is a stripped down version of the code to simplify.
URL:
http://www.wix.com/efficertain/efficertainaccounting/accounting
Form Code:
Name
Phone
Best time to call
PHP Code:
$mail_to = 'info#efficertain.ca';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_phone."\n";
$body_message .= 'Message: '.$field_when;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'http://www.wix.com/efficertain/efficertainaccounting/accounting';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info#efficertain.ca');
window.location = 'http://www.wix.com/efficertain/efficertainaccounting/accounting';
</script>
<?php
}
?>
You should not use window.location here as it will reload the whole browser window. Try to use document.location and you will need to pass it the URL of your form instead of the URL for the whole page as you only want to reload the iframe content. So your URL would be http://www.flashquix.com/embeds/fb4c83282b9342aa83952e74ad33dfdd?wrap=no&gzip=true&bg=transparent according to the example above.
document.location = 'http://www.flashquix.com/embeds/fb4c83282b9342aa83952e74ad33dfdd?wrap=no&gzip=true&bg=transparent';
You are already putting new content on the page.. just follow the logic and make that content your form.
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info#efficertain.ca');
</script>
// INSERT YOUR FORM HTML HERE AND YOU'RE GOOD TO GO