Contact Form Function

Hello I am making a contact Form and my PHP is not functioning not sure what I am missing or what it wrong:
I have the HTML and CSS HERE

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $from = 'From: TangledDemo'; 
    $to = 'csosa@iogproducts.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];
			
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
				
    if ($_POST['submit'] && $human == '4') {				 
        if (mail ($to, $subject, $body, $from)) { 
	    echo '<p>Your message has been sent!</p>';
	} else { 
	    echo '<p>Something went wrong, go back and try again!</p>'; 
	} 
    } else if ($_POST['submit'] && $human != '4') {
	echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
?>

What errors are you getting? What “is not functioning”?

Try something like this

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $from = 'From: TangledDemo'; 
    $to = 'csosa@iogproducts.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if (isset($_POST['submit']) && $human === '4') {				 
        if (mail ($to, $subject, $body, $from)) { 
	    echo '<p>Your message has been sent!</p>';
	} else { 
	    echo '<p>Something went wrong, go back and try again!</p>'; 
	} 
    } else if (isset($_POST['submit']) && $human !== '4') {
	echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
?>

Note that there is no sanitation here though so it’s highly vulnerable.

Im not receiving the email. Note that I am testing it in my local host desktop not sure if that is why it is not working…

Yes that’s likely the situation. Have you set up an e-mail server to do this?

I believe so yes, should I try putting the files in the my server to see if it functions?

You could. But if you truly wanted to run it locally, you should verify that in your php.ini you have the SMTP Server Settings setup correctly.

Alright so it is on my server, this is the LINK but now it gives me this error: You answered the anti-spam question incorrectly! when I fill out the form and submit.

None of your input fields have the name attribute and you don’t have a hidden field named “human”

How do you get the $human variable? There is no element with the name=“human” in your HTML. So it will

Edit-Dangit I was installing JDK and as soon as I get back, I get ninja’d…

1 Like

You mean these right that I am missing? : name="fullname", name="email" etc…

I took this off…

Yes.

Then you need to edit your PHP script.

Take

    if (isset($_POST['submit']) && $human === '4') {				 
        if (mail ($to, $subject, $body, $from)) { 
	    echo '<p>Your message has been sent!</p>';
	} else { 
	    echo '<p>Something went wrong, go back and try again!</p>'; 
	} 
    } else if (isset($_POST['submit']) && $human !== '4') {
	echo '<p>You answered the anti-spam question incorrectly!</p>';
    }

and change it to

    if (isset($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
	    echo '<p>Your message has been sent!</p>';
	} else { 
	    echo '<p>Something went wrong, go back and try again!</p>'; 
	} 
    }

weird now it says I have a parce error: Parse error: syntax error, unexpected ‘{’ in D:\Hosting\6870551\html\index_form.php on line 12

but I don’t seem to see it.

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $from = 'From: TangledDemo'; 
    $to = 'csosa@iogproducts.com'; 
    $subject = 'Hello';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

      if (isset($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
	    echo '<p>Your message has been sent!</p>';
	} else { 
	    echo '<p>Something went wrong, go back and try again!</p>'; 
	} 
    }
?>

Poor copy and paste on my part :smile:

Replace

if (isset($_POST['submit']) {

with (note the extra closing parenthesis needed)

if (isset($_POST['submit'])) {
1 Like

It said the message sent but I did not get the email…

I am having it sent to myself to to test it…

 $from = 'From: Demo'; 
 $to = 'myemail@business.com';  (not my actual email)

It has to do with my php.ini file right?
How do I link that to my HTML?

I need something like this right?:

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

This is what in that file right now:

register_globals = off
allow_url_fopen = on

expose_php = Off
max_input_time = 60
variables_order = "EGPCS"
extension_dir = ./
upload_tmp_dir = /tmp
precision = 12
SMTP = relay-hosting.secureserver.net
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset="
mail(to,subject,message,headers,parameters);

; Only uncomment zend optimizer lines if your application requires Zend Optimizer support

;[Zend]
;zend_optimizer.optimization_level=15
;zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.3
;zend_extension_manager.optimizer_ts=/usr/local/Zend/lib/Optimizer_TS-3.3.3
;zend_extension=/usr/local/Zend/lib/Optimizer-3.3.3/ZendExtensionManager.so
;zend_extension_ts=/usr/local/Zend/lib/Optimizer_TS-3.3.3/ZendExtensionManager_TS.so


; -- Be very careful to not to disable a function which might be needed!
; -- Uncomment the following lines to increase the security of your PHP site.

disable_functions = "highlight_file,ini_alter,ini_restore,openlog,passthru,
		      phpinfo, exec, system, dl, fsockopen, set_time_limit,
                     popen, proc_open, proc_nice,shell_exec,show_source,symlink"

No don’t change that on your actual server. Chances are your host maybe restrictive the user of mail ()

Who is your host?

Will it send using a from-address that isn’t a valid email address? Or did you put “demo” just to disguise it on the forum (a good idea)?

Go Daddy.

I put as a placeholder. What actually goes there though?

GoDaddy seems to have its own scripts it may want you to use (if you are on their Web/Classic hosting package)
https://support.godaddy.com/help/article/8376/using-our-php-form-mailers-on-web-classic-hosting

and
https://support.godaddy.com/help/article/508/specifying-an-email-address-for-the-php-form-mailer

If you have a cPanel/Plesk control panel, then you should be on the right direction using mail(), granted your From header is not quite right.

$from = 'From: TangledDemo';

Should be

$from = "From: TangledDemo <$email>";

And if that doesn’t work, it may require it to be from your email address

$to = 'youremail@yourdomain.com'; 
$from = "From: TangledDemo <$to>\r\n" .
    "Reply-To: $email";