This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I am trying to make a contact form and I believe that the only way is through PHP (so I get an e-mail when my client contacts me.) It is really a very straightforward form, but I am cannot get the form to send me the radio button checked status and have no clue how.
This is my HTML:
<form action="contact.php" method="post">
Your name<br>
<input type="text" name="cf_name" ><br>
<br />
Your e-mail<br>
<input type="text" name="cf_email"><br>
<br />
Do you want to join my mailing list?<br>
<input type="radio" name="mail-list" value="yes">Yes<br>
<input type="radio" name="mail-list" value="no">No<br>
<input type="radio" name="mail-list" value="I_am_already_in_it">I am already in it<br>
<br />
Message<br>
<textarea name="cf_message" rows="10" cols="100">
</textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
And this is my PHP:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$radio_yes = $_POST ['yes'];
$radio_no = $_POST ['no'];
$radio_I_am_already_in_it = $_POST ['I_am_already_in_it'];
$mail_to = 'my-email#my-site.com';
$subject = 'Hello, I am contacting you from your site. My name is '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$body_message .= 'True: '.$radio_yes;
$body_message .= 'True: '.$radio_no;
$body_message .= 'True: '.$radio_I_am_already_in_it;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you very much. I will do my best to reply to you within 24 hours.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('For some reason your message did not send. Please, do send me an e- mail directly to fernando#fernandoalbertmedium.com');
window.location = 'contact.html';
</script>
<?php
}
?>
The form is working fine, but the radio button status does not show up in the e-mail I receive. Also, for some reason, the cursor does not appear at the beginning of the textarea, but on the second line with some indent. Do you know why?
You are trying to get radio's value by value as $_POST's key, instead of this radio input's name, you need to get it like this:
$radio_value = $_POST['mail-list'];
$radio_checked = $_POST ['mail-list'];
their you get value which radio button is checked and $radio_checked value will become according to this as if you checked yes
Related
So I have published my website, but am making minor improvements to it as time progresses. Just a note: I am impatient and this is making me want to obliterate myself. Help!
So the question is the title. I have a contact form on my website which uses HTML and PHP.
Here is the form on HTML:
<form action="contact.php" method="post">
Name<br>
<input type="text" name="cf_name"><br>
E-Mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea></br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
And here is the PHP for it:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'amir#arshak.co.uk';
$subject = 'ALERT! Somebody has viewed your website!'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thanks. I have recieved your message. I may reply to the given e-mail.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, your message failed to send. Send an email to amir#arshak.co.uk');
window.location = 'contact.html';
</script>
<?php
}
?>
I can't figure out what is wrong with the code!
If the problem is that the email won't send...
You are allowing the from and reply-to addresses to be set to whatever is entered into the form.
A lot of mail servers will only allow an email to be sent where the from address matches the email address used to authenticate with the server.
This could possibly be causing your issue although it's hard to say without much more information.
I have tried looking in similar questions posted here, but cannot find an answer (got about 1900 questions popping up).
I have a contact form on a page at a site I am creating on the webpage hosting site
https://www.one.com/en/
Here is the form on the page contact/contact.shtml
<form action="contact/mail.php" method="post"><br>
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Here is the content of the contact/mail.php file
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'clashton#gmail.com';
$subject = ' Contact Form '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.shtml';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to clashton#gmail.com');
window.location = 'contact.shtml';
</script>
<?php
}
?>
When I try to send a message a window pops up containing the error message "Message failed. Please, send an email to clashton#gmail.com". In other words the "else" action is happening.
If it seems that this question has a trivial answer, please keep in mind that I am completely new at this.
Thanks for any help.
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
I'm completely new to HTML and PHP and am trying to build a simple contact form, so that users can send me a message on the contact page, which then gets directed to my email. Any ideas what I'm doing wrong here:
contact.html:
<section id="secondary">
<form action="contact.php" method="post" class="contactform">
Your Name<br>
<input type="text" maxlength="100" name="cf_name" style="width: 300px" class="contactboxes"><br>
Your E-mail<br>
<input type="text" maxlength="100" name="cf_email" style="width: 300px" class="contactboxes"><br>
The Reason for Contact<br>
<textarea name="cf_message" maxlength="500" style="width: 300px" class="contactboxes"></textarea><br>
<input type="submit" value="Send" class="sendclearbutton">
<input type="reset" value="Clear" class="sendclearbutton">
</form>
</section>
contact.php:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'sharan#hotmail.co.uk';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to sharan#hotmail.co.uk.');
window.location = 'contact.html';
</script>
<?php
}
?>
It looks fine to me, as Lance said, the only reason I can think that it wont be working is that you don't have PHP installed. Download Apache or whatever suits your computer best or use web hosting online. Do say if other code is working, though.
I am making a feedback form for my website using php, I thought that it was working, but then it took an arrow to the code.
Anyways, the form has 4 fields: "name", "email", "subject", and "message".
I get the "Message failed" alert if all four fields have content.
Seeing as that is the case, I tested with content in 3 of the 4 fields, which gives me 4 combinations:
combo 1 - "name", "email", "subject"
combo 2 - "name", "email", "message"
combo 3 - "name", "subject", "message"
combo 4 - "email", "subject", "message"
The results were as follows:
combo 1 - "Thank you for your message."
combo 2 - "Thank you for your message."
combo 3 - "Message failed."
combo 4 - "Thank you for your message."
Here is the html code I am using:
<form action="contact.php" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
Your Name: <input type="text" name="name" />
<br/>
Your Email: <input type="text" name="email" />
<br/>
Subject: <input type="text" name="subject" size="69" />
<br/>
Message:
<br/>
<textarea cols="63" rows="8" name="message"></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
And here is contact.php:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your message.');
window.location = './contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = './contact.html';
</script>
<?php
}
?>
Is there anything wrong with the code? I don't understand why it is doing this.
Great answer from Anton but I wanted to dig a little bit more into this and expand his answer.
If you want to make sure someone sent the email, require it hardcoded with php (and html5 would be cool too, but this can be avoid so you still need php). I also deleted the javascript bit as it felt irrelevant there. This is what I would try and since it's short I'd do it all in the same page. There are many subtle changes:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
//If all required fields are filled
if (!empty($field_name)&&!empty($field_email)&&!empty($field_message))
{
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor: '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
echo "Thank you for your message.";
}
//If not all required fields are filled display form again.
else
{ ?>
Please fill all the required fields.<br>
<form action="" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
<?php //The php bits are to retrieve the valid fields ?>
Your Name*: <input required type="text" value="<?php echo $field_name; ?>" name="name"/>
<br/>
Your Email*: <input required type="email" value="<?php echo $field_email; ?>" name="email" />
<br/>
Subject: <input type="text" name="subject" value="<?php echo $field_subject; ?>" size="69" />
<br/>
Message*:
<br/>
<textarea required cols="63" rows="8" name="message"><?php echo $field_message; ?></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
<?php
}
?>
Otherwise, if you don't care about who sends it, you could just put something like this
$field_name = $_POST['name'];
$field_email = $_POST['email'];
if (empty($field_email)) $field_email="anonymous#MYPAGE.com";
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
Last thing, I hope this is just a pseudo code as it doesn't comply almost any html rule. You need < html > tags, < body > etc...
Just tested and worked great (;
The problem is probably this:
Quote from http://php.net/manual/en/function.mail.php
When sending mail, the mail must contain a From header. This can be
set with the additional_headers parameter, or a default can be set in
php.ini.
Failing to do this will result in an error message similar to Warning:
mail(): "sendmail_from" not set in php.ini or custom "From:" header
missing. The From header sets also Return-Path under Windows.
In other words, if you do not have a header containing a proper "From: adress#email.com" an error will occur.
I've created a contact form for a website with multiple fields. The clear button works perfectly, but when I click the Submit button, I inadvertently download the .php file associated with the webpage, instead of sending an email with the order form to the specified address. I'm really new to php here, so any help would be greatly appreciated.
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_quantity = $_POST['cf_quantity'];
$field_which = $_POST['cf_which'];
$field_size = $_POST['cf_size'];
$field_school = $_POST['cf_school'];
$field_college = $_POST['cf_college'];
$field_message = $_POST['cf_message'];
$mail_to = 'xxx#gmail.com';
$subject = 'TShirt Order - xxx.Org '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail Address: '.$field_email."\n";
$body_message .= 'Quantity of Shirts: '.$field_quantity."\n";
$body_message .= 'Shirt Style(s): '.field_which."\n";
$body_message .= 'Size(s): '.field_size."\n";
$body_message .= 'School: '.field_school."\n";
$body_message .= 'College: '.field_college."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for placing an order with xxx.Org. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to xxx#gmail.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
the html code is:
<form action="contact.php" method="post">
Full Name:
<input type="text" name="cf_name">
<br />
<br />
Email Address:
<input type="text" name="cf_email">
<br />
<br />
Quantity of TShirts:
<input type="numeric" name="cf_quantity">
<br />
<br />
Which Style(s)?
<input type="text" name="cf_which">
<br />
<br />
Size(s):
<input type="text" name="cf_size">
<br />
<br />
Name of School:
<input type="text" name="cf_school">
<br />
<br />
Name of College:
<input type="text" name="cf_college">
<br />
<br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Are you completely sure the server accepts php? Seams to me the server doesn't understand it should use the php interpeter and instead of activating the php commands it just lets you download the file.
If you're not sure try and make a file with a simple in it, call that and see if it works.
best regards,
Mark