Jump to content

Is my form correct? Why do I sometimes get this response?


Recommended Posts

Take a look at http://us.php.net/print_r

 

I'm not sure that you want to directly output the information in the array like that in your email. You may want to consider making a string with the message in it by processing the information in the array.

That code is rather basic. If that is all of the code, it is not checking that a form was even submitted and as a result it will send an email with an empty $_POST array any time someone or something (search engine, spam bot) simply visits the page that code is on.

 

At a minimum it needs to check that the $_POST variable that corresponds to the name of your submit button is set.

Sorry, I'm very new to all this stuff.  It's all in the file called process.php

 

Do I need to post anything else?

 

I apologize for being stupid on this one but I am very new at the php side of things

 

Thank you

Ryan

 

No,

 

The


tags are used when you post source in this forum.

 

Go into your editor, copy all the code, and paste the copied code into a reply.

 

Put the

 tag before your code and 

after it.

 

Ken

Thanks.  Code as follows:

 

<?php

$headers = "From: webmaster@MYWEBSITE.co.uk\r\n" .
"X-Mailer: PHP" . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";

// this emails you the results of the form
mail( "Webmaster@MYWEBSITE.co.uk", "New Contact Message", print_r( $_POST, true),"$headers");

// this will redriect the user somewhere away from this page
header("location: http://www.MYWEBSITE.co.uk/thanks.html");

?>

 

Thank you

Ryan

 

If there is any chance that this script can be invoked directly instead of being invoked from the form being submitted, then the $_POST array would be empty and that would cause your problem. I would test to make sure the process was invoked via the form. Try:

<?php
if (isset($_POST['submit'])) {
     $headers = "From: webmaster@MYWEBSITE.co.uk\r\n" .
                        "X-Mailer: PHP" . phpversion() . "\r\n" .
                        "MIME-Version: 1.0\r\n" .
                        "Content-Type: text/html; charset=utf-8\r\n" .
                        "Content-Transfer-Encoding: 8bit\r\n\r\n";

// this emails you the results of the form
      mail( "Webmaster@MYWEBSITE.co.uk", "New Contact Message", print_r( $_POST, true),$headers);

// this will redriect the user somewhere away from this page
       header("location: http://www.MYWEBSITE.co.uk/thanks.html"); }
else {
      echo "Script invoked incorrectly."
?>

 

Ken

Alright well your form was not mailing to anyone and you did not check to see if the form was mailed properly

 

I have structured the message section of the email to do what you want it to though i dont think that is a good idea

 

It would also help alot if you could show us the form

 

<?php
if(isset($_POST['submit'])) {
	$from = $_POST['EMAIL'];
	$subject = "New Contact Message";
	$message = "";
	foreach($_POST as $description => $post_option) {
		$message .= $description . ': ' . $post_option . "\r\n";
	}
	$to = "Webmaster@MYWEBSITE.co.uk";
	$headers = "From: webmaster@MYWEBSITE.co.uk" . "\r\n";
	$headers .= "X-Mailer: PHP" . phpversion() . "\r\n";
	$headers .= "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-Type: text/html; charset=utf-8" . "\r\n";
	$headers .= "Content-Transfer-Encoding: 8bit" . "\r\n";
	if(mail($to,$subject,$message,$from,$headers)) {
		header("location: http://www.MYWEBSITE.co.uk/thanks.html");
	}
	else {
		print 'Failed sending message.';
		if(error_get_last() != '') {
			print '<br />An error was retrieved from the system:<br />';
			print error_get_last();
		}
		else {
			print '<br />The system did not return an error';
		}
	}
}
else {
	// Put the form here
}
?>

The original mail() call did have a "to" field, it was just entered as a literal. You don't need a variable. Same goes for the body of the message. Using print_r() with the second parameter of "true" will put the results into the message, There's no need for the foreach loop you have.  Also, your foreach loop only puts in the value of the submitted fields, not the names of the fields, which is rather meaningless. To see the source of the form, go the URL the OP mentioned a few posts ago and click on "Show source" in your browser.

 

Ken

The original mail() call did have a "to" field, it was just entered as a literal. You don't need a variable. Same goes for the body of the message. Using print_r() with the second parameter of "true" will put the results into the message, There's no need for the foreach loop you have.  Also, your foreach loop only puts in the value of the submitted fields, not the names of the fields, which is rather meaningless. To see the source of the form, go the URL the OP mentioned a few posts ago and click on "Show source" in your browser.

 

Ken

 

1). I am sorry when i said to i was looking at something else, i meant from,

2). You dont need a variable it is just easier to read

3). using print_r is not the best way to do it as print_r can not be formatted

4). my foreach loop shows the description of the posted field as well

5). I do not see a link unless MYWEBSITE.co.uk is a link which i doubt

1). I am sorry when i said to i was looking at something else, i meant from,

2). You dont need a variable it is just easier to read

3). using print_r is not the best way to do it as print_r can not be formatted

4). my foreach loop shows the description of the posted field as well

5). I do not see a link unless MYWEBSITE.co.uk is a link which i doubt

1) The OP did include a "from" line

2) Depends on who's reading it.

3) The OP wants to email the contents of the form to himself. IMHO, print_r is the easiest way to do that.  I use it all the time. The output looks fine in a text based email message.

4) I believe you were editing your response when I posted my response

5) Sorry, I was thinking of a different question.

 

Ken

1) The OP did include a "from" line

2) Depends on who's reading it.

3) The OP wants to email the contents of the form to himself. IMHO, print_r is the easiest way to do that.  I use it all the time. The output looks fine in a text based email message.

4) I believe you were editing your response when I posted my response

5) Sorry, I was thinking of a different question.

 

Ken

 

1). i do not see a from in this string "mail( "Webmaster@MYWEBSITE.co.uk", "New Contact Message", print_r( $_POST, true),$headers);"

2). Well im sorry but i find it easier when i know exactly what each field is

3). It may be the easiest but it is not the best, so i guess your point is to be lazy

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.