Har problem med ett formulär och är total nybörjare på php.
Formulär-sidan är "include-länkad" i en av huvudsidorna (index3.php). När man skickar mailet skall ett tackmeddelande skrivas ut. Har man inte fyllt i fälten så skall en varning visas.
Problemet är att formuläret funkar inte när det är inkluderat i huvudsidan. Istället för tackmeddelande hoppar kommer man till startsidan. Fungerar dock om man surfar direkt till formuläret...
http://joelsundkvist.com/index3.php?page=contact (hela sidan)
http://joelsundkvist.com/contact.php (formuläret)
Postar lite kod:
<?php
// Your e-mail adress:
$mailto = "mail@domain.com";
# Maximum size of attachment in bytes:
$max_attach_size = 500000;
?>
<html>
<head>
<title>Formmailer</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.caution{ color: red; font-weight: bold; }
.specialp {
font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Lucida, Helvetica, Sans-Serif;
font-size: 11px;
margin: 0px 0px 5px 10px;
line-height: 14px;
color: #7E7F7A;
}
.tsp {background-color:transparent;border:1px solid #ccc;}
-->
</style>
</head>
<body style="background-image:url(gfx/tablebg_1.jpg); background-repeat:no-repeat;background-position:bottom center;">
<?php
/*if (empty($_POST['form_submitted']))
{
?><p>Please fill out the form:</p><?php
}
*/
if (isset($_POST["form_submitted"]))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$text = $_POST['text'];
unset($errors);
if ($email != "" and !preg_match("/^[^@]+@.+\.\D{2,5}$/", $email)) $errors[] = "e-mail address seems to be invalid";
if ($text == "") $errors[] = "no message entered";
if ($_FILES['probe']['size'] > $max_attach_size) $errors[] = "attachment too large (".number_format($_FILES['probe']['size']/1000,0,",","")." KB) - maximum size: ".number_format($max_attach_size/1000,0,",","")." KB";
if (empty($errors))
{
$text = stripslashes($text);
$subject = stripslashes($subject);
if ($name != "") $mail_name=$name; else $mail_name="Unknown";
if ($subject != "") $mail_subject = $subject; else $mail_subject = "No subject";
if ($email != "") $mail_email = $email; else $mail_email = "email@unknown.xyz";
$ip = $_SERVER["REMOTE_ADDR"];
// if attachment, MIME-Mail:
if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "")
{
// read and encode file:
$datei_content = fread(fopen($_FILES['probe']['tmp_name'],"r"),filesize($_FILES['probe']['tmp_name']));
$datei_content = chunk_split(base64_encode($datei_content),76,"\n");
// Boundary:
$boundary = md5(uniqid(rand()));
// Mail-Header:
$mail_header = "From: ".$mail_name." <".$mail_email.">\n";
$mail_header .= "X-Sender-IP: ".$ip."\n";
$mail_header .= "MIME-Version: 1.0\n";
$mail_header .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\n";
$mail_header .= "This is a multi-part message in MIME format.\n";
// Mail-Text:
$mail_header .= "--".$boundary;
$mail_header .= "\nContent-Type: text/plain";
$mail_header .= "\nContent-Transfer-Encoding: 8bit";
$mail_header .= "\n\n".$text;
// Attachment:
$mail_header .= "\n--".$boundary;
$mail_header .= "\nContent-Type: ".$_FILES['probe']['type']."; name=\"".$_FILES['probe']['name']."\"";
$mail_header .= "\nContent-Transfer-Encoding: base64";
$mail_header .= "\nContent-Disposition: attachment; filename=\"".$_FILES['probe']['name']."\"";
$mail_header .= "\n\n".$datei_content;
// End:
$mail_header .= "\n--".$boundary."--";
// Sende E-Mail und gebe Fehler bzw. Bestaetigung aus
if (@mail($mailto,$mail_subject,"",$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
}
// no attachment, normal E-mail:
else
{
$mail_header = "From: ".$mail_name." <".$mail_email.">\n";
$mail_header .= "X-Sender-IP: $ip\n";
$mail_header .= "Content-Type: text/plain";
if (@mail($mailto,$mail_subject,$text,$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
}
// copy to sender:
if (isset($sent) && isset($email) && $email != "" && isset($_POST['copy']))
{
if (isset($_FILES['probe']['name']) && trim($_FILES['probe']['name']) != "") $copy_mail_text = "Copy of the e-mail:\n\n".$text."\n\nAttachment: ".$_FILES['probe']['name']; else $copy_mail_text = "Copy of the e-mail:\n\n".$text;
$header= "From: ".$mailto."\n";
$header .= "X-Sender-IP: ".$ip."\n";
$header .= "Content-Type: text/plain";
@mail($email, $mail_subject, $copy_mail_text, $header);
}
}
}
if (empty($sent))
{
if(isset($errors))
{
?><p class="caution">Error:</p><ul><?php foreach($errors as $f) { ?><li><?php echo $f; ?></li><?php } ?></ul><br /><?php
}
?>
<form method="post" action="<?php echo basename($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
<p class="rub">Contact</p>
<br>
<table width="700" cellspacing="0" cellpadding="0">
<tr>
<td width="407" style="border-bottom:1px solid #ccc;vertical-align:middle;border-top:1px solid #ccc;height:20px;background-color:whitesmoke">
<p class="rub3" style="display:inline;">E-mail</p>
</td>
<td width="293" style="border-bottom:1px solid #ccc;border-top:1px solid #ccc;vertical-align:middle;border-left:1px solid #ccc;height:20px;background-color:whitesmoke">
<p class="rub3" style="display:inline;">contact info</p></td>
</tr>
<tr>
<td><br>
<p class="specialp">Name:<br>
<input type="text" name="name" value="<?php if (isset($name)) echo htmlentities(stripslashes($name)); else echo ""; ?>" size="35" class="tsp" /></p>
<td rowspan="7" valign="top" style="border-left:1px solid #ccc;">
<p class="rub2">Adress</p>
<p>bla bla bla bla bla bla bla </p>
</td>
</tr>
<td><p class="specialp">E-mail:<br>
<input type="text" name="email" value="<?php if (isset($email)) echo htmlentities(stripslashes($email)); else echo ""; ?>" size="35" class="tsp" /></p>
</td>
</tr>
<tr>
<td>
<p class="specialp">Subject:<br>
<input type="text" name="subject" value="<?php if (isset($subject)) echo htmlentities(stripslashes($subject)); else echo ""; ?>" size="35" class="tsp" /></p>
</td>
</tr>
<tr>
<td><p class="specialp">
Message:<br>
<textarea name="text" cols="55" rows="6" class="tsp"><?php if (isset($text)) echo htmlentities(stripslashes($text)); else echo ""; ?></textarea>
</p>
</td>
</tr>
<tr>
<td>
<p class="specialp">Attachment:<br>
<input type="file" name="probe" value="<?php if (isset($_POST['probe'])) echo htmlentities(stripslashes($_POST['probe'])); else echo ""; ?>" size="20" /></p>
</td>
</tr>
<tr>
<td>
<p class="specialp"><input type="submit" name="form_submitted" value="Send!" />
<input type="checkbox" name="copy" value="true" class="tsp" /> Copy to sender</p>
</td>
</tr>
</table>
</form><?php
}
else
{
if (empty($email)) { ?><p> </p><p><b>Thank you!</b><br>The message has been sent successfully but you didn't specify your e-mail address wich makes it hard to reply.</p><?php }
else { ?><p><b>Thank you!</b><br>The message has been sent successfully.<br>Joel will get back to you as soon as possible.</p><?php }
}
?>
</body>
</html>
Kan det vara felaktiga permissions som spökar?
Finns det någon grundregel för hur man ska ställa in dessa?
Tack på förhand! :)