PDA

View Full Version : Contact Form using .php


jacob1986
05-05-2015, 03:31 PM
I have written some php code for my contact form, most of the message does works. The problem I have is this - when I receive the message I see the recipient's email address but no name, moreover; when I changed the code I could see the recipient's name but no email address?

I have three text boxes (name, email and message) in my html webpage, but cannot seem to get the php working.

<?php

$from="reply.info@websiteinfotest.ninja";
$email="info@websiteinfotest.ninja";
$to ="info@websiteinfotest.ninja";
$subject=$_POST['Subject'];
$from=$_REQUEST["from"];
$message=$_POST['Message'];


mail ( $email, $subject, $message, "From:".$from);


Print "Your Message has been sent";



?>

edbr
05-06-2015, 01:54 AM
I have three text boxes (name, email and message)

so these will be $message=$_POST['Message'];,$message=$_POST['from];, and possiibly $message=$_POST[name'];

you have define $from twice so that is wrong. You could compile the message do this
$from="reply.info@websiteinfotest.ninja";
$email="info@websiteinfotest.ninja";
$to ="info@websiteinfotest.ninja";
$subject=$_POST['Subject'];

$message=$_POST['Message']. $_POST['from].$_POST[name'];


mail ( $email, $subject, $message, "From:".$from);


Print "Your Message has been sent";

edbr
05-06-2015, 07:25 AM
you will also need yo validate this to prevent spamming

edbr
05-06-2015, 07:27 AM
an example copy pasted
<?php

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

if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}

if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}

if ($_POST['homepage'] != "") {
$homepage = filter_var($_POST['homepage'], FILTER_SANITIZE_URL);
if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
$errors .= "$homepage is <strong>NOT</strong> a valid URL.<br/><br/>";
}
} else {
$errors .= 'Please enter your home page.<br/>';
}

if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}

if (!$errors) {
$mail_to = 'me@somewhere.com';
$subject = 'New Mail from Form Submission';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= 'Homepage: ' . $_POST['homepage'] . "\n";
$message .= "Message:\n" . $_POST['message'] . "\n\n";
mail($to, $subject, $message);

echo "Thank you for your email!<br/><br/>";
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
?>

<form name="form1" method="post" action="form-email.php">
Name: <br/>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="50" /><br/><br/>
Email Address: <br/>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><br/>
Home Page: <br/>
<input type="text" name="homepage" value="<?php echo $_POST['homepage']; ?>" size="50" /> <br/><br/>
Message: <br/>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" name="Submit" />
</form

jacob1986
05-06-2015, 03:46 PM
I tried to use the 'example copy' but no messages were sent to my email client? I will explain my procedure(s) for working in Dreamwever (php files) later for you.

All I really want to know is the correct was in which I can see all the information from my 'contact form' in my email client. When I receive the message from my contact form - I can see the recipients email address (where subject should be) and I can see 'To' which is my email address and the message itself - but no actual name of the correspondents or a 'from tag' i.e. the person's email address?

Moreover; when I receive a 'normal' email to my email client I can see subject, as well as the 'from tag' - the recipients email address and the 'To' which is my email address and below all this is the actual message.

jacob1986
05-06-2015, 03:59 PM
New document >> php >> delete html code >> code view: paste ‘example copy’ >> edit line 42 - $mail_to = 'me@somewhere.com'; >> save as mail script >> new html document >> insert form code in between the body tags >> save as contact >> upload mail script and then copy the location >> insert location of mail script into ‘action’ and save >> upload contact.


But as I said nothing was happening, I'm probably doing something wrong?

edbr
05-07-2015, 01:52 AM
ah, sorry i was a bit busy, i should have explained better. i will re do for you later.

edbr
05-07-2015, 02:42 AM
ok just tested it , i missed that there was an action to another page.
This is a one page with action set to itself, i also removed the url field with its validation so to make it what you wanted. you can of course separate the php and html and change the action.
<?php

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

if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}

if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}


if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}

if (!$errors) {
$mail_to = 'me@somewhere.com';
$subject = 'New Mail from Form Submission';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= "Message:\n" . $_POST['message'] . "\n\n";
mail($to, $subject, $message);

echo "Thank you for your email!<br/><br/>";
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
?>

<form name="form1" method="post" action= "">
Name: <br/>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="50" /><br/><br/>
Email Address: <br/>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><br/>

Message: <br/>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" name="Submit" />
</form

jacob1986
05-07-2015, 09:34 PM
Foremost: thank-you for helping me.

I am going to start again from scratch (building a form and php in Dreamweaver) then I will write the php code again from (as I said) scratch... I will ask for help in the next days to fathom the code operations (argument and so forth).

Once again thank-you.

edbr
05-08-2015, 02:07 AM
no problem. try not to rely on dreamweaver php code though, not always best in my experience

jacob1986
05-09-2015, 11:36 AM
I've created a contact form (the actual form code is at the bottom of the page). I just need to know how to see the email address of the person who submitted the form, the name of the person who submitted the form, the message in the submitted form and a Cc header.

Would it be feasible for you or anybody to write a small succinct php code so I can add this to the 'action tag' in dreamweaver, also do I need to write any other code elsewhere in dreamweaver for the form to work?

The problem I'm having is when I receive the email to my inbox and go to reply, it doesn't reply to the email address of the person who submitted the form - in the reply field it appears to have the default server email address. Moreover; the email address of the person who submitted the form is in the subject line?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="http://www.artworkpastels.com/websiteinfotest.ninja/mailscript.php">
<table width="216" border="0">
<tr>
<td>Name:</td>
</tr>
<tr>
<td><input name="Name" type="text" id="Name" size="35"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>Email</td>
</tr>
<tr>
<td><input name="Subject" type="text" id="Subject" size="35"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>Message</td>
</tr>
<tr>
<td><textarea name="Message" cols="33" id="Message"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

edbr
05-11-2015, 02:47 AM
i gave you code . what have to you done so far with php? just the form doesn't show your error

edbr
05-11-2015, 05:22 AM
ok i just posted a 'sticky' that should give you all the information you need to do this

jacob1986
05-14-2015, 02:45 PM
Sorry for the epic delay in answering this thread... but thank-you - the 'sticky' worked great - I fervently appreciate the help you have given me :)

If I may ask a cheeky question though... how do you use an image instead of the submit button using the form [sticky]? Moreover; I have used the following code but no messages are ever sent?

<INPUT TYPE="image" SRC="images/submit.gif" HEIGHT="30" WIDTH="173" BORDER="0" ALT="Submit Form">

edbr
05-15-2015, 08:56 AM
i believe that possibly depends on browser support for the way you quoted.
it can be done with javascript or using a class

.submit {
width: auto;
padding: 9px 15px;
background: #617798;
color: #FFFFFF;
} this uses a color you could use an image

<button name="Submit" type="submit" class="submit" >
should work on all browsers AFIK

jacob1986
05-18-2015, 08:05 PM
I had to change the code somewhat to make the imagefield work... but is my code good enough to deter and admonish spammers?

<?php

if($_POST['robots'] != '') {
echo 'No spammers here!';
} else {
// Process the the form
}

$to = 'general_enquiries@xxxxxx.com';
$subject = 'info help ';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email from: ' . $_POST['email'] . "\n";
$message .= "Message:\n" . $_POST['Message'] . "\n\n";
$headers .= 'Cc: admin@xxxxx.com';
mail($to, $subject, $message, $headers);


mail ($email, $subject, $message, "from:".$from);

header('Location: xxxxxxxx.com/thank-you.html');
exit();

?>

edbr
05-19-2015, 04:07 AM
no guarantees but has been effective to use a 'honeypot" , make sure no display for the trap form field.
you could also call it something that you think robots might look for
other than robots