Jump to content

Recommended Posts

Hi guys, im updating all my old php/MySQL to newer coding standards (or trying to at least). So heres what I have so far. The form makes you enter all the required fields but instead of it sending the message, it just appends it all to the address bar after refreshing the page. Im not sure exactly whats wrong with it but this is kinda new to me so any help would be greatly appreciated :)

 

EDIT: Im getting this error now, im sure its something simple but im just not seeing it. i dont see where or how to load this dynamic library.

 

[21-Nov-2016 13:36:28] PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/php/54/usr/lib64/php/modules/imagick.so' - /usr/php/54/usr/lib64/php/modules/imagick.so: undefined symbol: zend_new_interned_string in Unknown on line 0

<script>
function _(id){ return document.getElementById(id); }
function submitForm(){
	_("mybtn").disabled = true;
	_("status").innerHTML = 'Please Wait...';
	var formdata = new FormData();
	formdata.append( "n", _("n").value );
	formdata.append( "e", _("e").value );
	formdata.append( "p", _("p").value );
	formdata.append( "m", _("m").value );
		var ajax = new XMLHttpRequest();
	ajax.open( "POST", "parser.php" );
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			if(ajax.responseText == "success"){
				_("my_form").innerHTML = '<h2>Thanks '+_("n").value+', your message has been sent.</h2>';
			} else {
				_("status").innerHTML = ajax.responseText;
				_("mybtn").disabled = false;
			}
		}
	}
	ajax.send( formdata );
}
</script>
			<div class="contact-grid agileits w3layouts">
				<form id="my_form" onsubmit="submitForm(); returm false;">
					<input type="text" id="n" class="text agileits w3layouts" name="Name" placeholder="YOUR NAME" required="">
					<input type="email" id="e" class="text agileits w3layouts email" name="Email" placeholder="YOUR EMAIL" required="">
					<input type="text" id="p" class="text agileits w3layouts" name="Phone" placeholder="YOUR PHONE" required="">
					<textarea name="Message" id="m" placeholder="MESSAGE" required=""></textarea>
					<input type="submit" id="mybtn" class="more_btn agileits w3layouts" value="SEND MESSAGE"><span id="status"></span>
				</form>
			</div>

And here is the parser.

<?php
if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['m']) ){
	$n = $_POST['n']; 
	$e = $_POST['e'];
	$m = nl2br($_POST['m']);
	$to = "brunsoncomputerservice@gmail.com";	
	$from = $e;
	$subject = 'Online Contact Form';
	$message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <br><b>Phone:</b> '.$p.'<p>'.$m.'</p>';
	$headers = "From: $from\n";
	$headers .= "MIME-Version: 1.0\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1\n";
	if( mail($to, $subject, $message, $headers) ){
		echo "success";
	} else {
		echo "The server failed to send the message. Please try again later.";
	}
}
?>
Edited by willscarlet
Link to comment
https://forums.phpfreaks.com/topic/302582-updated-email-not-working/
Share on other sites

this error has probably been occurring for some time, but because your code/site isn't using the imagick extension, and you haven't had the php error_reporting/display_errors setting set to report all errors, it hasn't been shown.

 

this error, while it should be fixed, isn't relevant to the immediate problem.

 

if the form field names and values are showing up in the browser address bar, it's because the JavaScript isn't submitting the form and the browser is (the <form tag doesn't have a method attribute, so the default get method is being used.)

 

if the code worked before, what exactly have you changed in it?

Oh im sorry, I guess i was misleading before, I had built an old CMS years ago using PHP/MySQL now im trying to get back into it all and im finding that MySQL is no longer suggested so im xfering everything over to PDO and updating my general coding standards. So alllll that being said :) this is new code that I kinda pieced together from tutorials, ive never used this code before but it looked like it was fairly straight forward and good. Just looking for a simple bot safe contact form really.

this code is not good and it's not safe.

 

afaik, the XMLHttpRequest() object is not universal between browsers. if you are going to use ajax, you must either take into account all the likely browsers or you need to use a library like jquery to do this for you.

 

before you can ajax, you must be able to html. your form and your form processing code must work properly before you can add ajax to it. your page should also work of someone has javascript disabled. once you get your form and form processing code to work, adding ajax is as simple as adding an event listener tied to an id or class in the form tag, prevent the default form action, serialize the form data (which takes a single statement to operate on all the successful form fields), submit the data, and handle the response.

 

next, because anyone or anything can submit data to your form processing code, it must enforce security. your form processing code needs to detect that a post method form has been submitted, validate the input data, safely produce the message body, and any email address you put into a mail header must be validated to insure it contains only one properly formatted email address to prevent mail header injection.

 

lastly, emails being sent from a form submission on a web site are NOT being sent from the email address that someone entered in the form. the emails are being sent from the web hosting mail server or a third-party mail server. the From: email address must either be hosted at the sending mail server or there must be an SPF record at the domain in the From: email address that says the sending mail server is authorized to send email for that domain.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.