Вот моя лучшая попытка разобраться в приведенном выше коде.Есть несколько вещей, которые я не понимаю, в частности, где именно в потоке документов находится этот скрипт - вы уже вывели какой-то HTML на этом этапе?Кроме того, есть строка, которая читает просто $url;
, и я не могу понять, что он должен делать.То, что он определенно делает, - ничто.
<?php
//--------------------------Set these parameters--------------------------
// Subject of email sent to you.
$subject = 'Add this Email Address to Mailing List';
// Your email address. This is where the form information will be sent.
$emailadd = 'test398ty32@gmail.com';
// Where to redirect after form is processed.
$url = 'http://10.0.1.1/~macpro';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '1';
// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
foreach ($_POST as $key => $value) {
if ($req == '1' && trim($value) == '') { // This is not a great validation check - what if the user enters some nonsense value?
// Is every field an email address? Which field are we dealing with here?
echo '<script type="text/javascript"> alert("Please enter a valid email address"); </script>';
// I don't get what this is supposed to do
$url;
}
// This sort of makes sense, but if you are using a meta refresh it implies we are
// still in the document head, so shouldn't you end it before outputing text?
if (strlen($key) >= 20) die("Name of form element $key cannot be longer than 20 characters");
// All of that spacing nonsense can be compressed to this one line
$text .= str_pad("$key:", 22, ' ', STR_PAD_RIGHT)."$value\n";
}
// Sends the email - this works for now although you should consider using a library
// like phpmailer or PEAR:Mail
if (mail($emailadd, $subject, $text, "From: $emailadd")) {
// The email was sent successfully
echo '<script type="text/javascript"> alert("Thankyou for subscribing to the newsletter"); </script>';
} else {
// Sending the email failed
echo '<script type="text/javascript"> alert("Failed to send email"); </script>';
}
// Refreshes the page - META refreshes are bad practice, you should really try to
// restructure all this code to allow you to do a header redirect (see below)
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
// Work towards being able to do this instead
// header('HTTP/1.1 303 See Other');
// header("Location: $url");
?>