Configuring My PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hey I am trying to figure out my problem with my form and where my hangups are in the code. Anyone have any suggestions?
<form method="post" action="tourney.php">
<div class="row">
<div class="col-sm-4">
<input class="form-control" type="text" placeholder="Name" name="tname">
</div>
<div class="col-sm-4">
<input class="form-control" type="text" placeholder="Email" name="temail">
</div>
<div class="col-sm-4">
<input class="form-control" type="text" placeholder="School or Club" name="tclub">
</div>
<br>
<br>
<div class="col-sm-4">
<input class="form-control" type="text" placeholder="Address" name="taddress">
</div>
<div class="col-sm-3">
<input class="form-control" type="text" placeholder="City" name="tcity">
</div>
<div class="col-sm-2">
<input class="form-control" type="text" placeholder="State" name="tstate">
</div>
<div class="col-sm-2">
<input class="form-control" type="text" placeholder="Zip" name="tstate">
</div>
</div>
<br>
<div class="row">
<div class="col-sm-12">
<textarea placeholder="Type your message here..." class="form-control" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-6">
<select name="type" class="form-control">
<OPTION VALUE="None Selected" SELECTED>Please select one</OPTION>
<OPTION VALUE="Middle School">Middle School Tournamanet</OPTION>
<OPTION VALUE="Elementary">Elementary Tournament</OPTION>
</select>
</div>
<div class="col-sm-6 pull-right">
<input class="btn btn-danger" type="submit" value="Register">
</div>
</div>
</form>
and the PHP to send the email:
<?php
if(isset($_POST['submit'])){
$from = "minguswrestling.com";
$to = "puremeld#gmail.com";
$name = $_POST['tname'];
$email = $_POST['temail'];
$school = $_POST['tclub'];
$address = $_POST['taddress'];
$city = $_POST['tcity'];
$state = $_POST['tstate'];
$zip = $_POST['tzip'];
$type = $_POST['type'];
$message = $_POST['tmessage'];
$subject = "Tourney Signup!";
$body = "From: $from \n Name: $name \n Email: $email \n School: $school\n Address: $address\n City: $city State: $state Zip: $zip\n Event: $type\n Message: $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
'<h2>Thank You!</h2>';
header("Location: index.html");
} else {
echo '<p>Oops! An error occurred. Try sending your message again. </p>';
}
}
}
?>
Any help would be appreciated. I am using bootstrap for my build, thank you.

There are a few things wrong with your code.
Firstly, and as I mentioned in comments; everything inside the following conditional statement will never fire up:
if(isset($_POST['submit'])){...}
because there is no form element that is named "submit", being your submit button and error reporting would have thrown you an undefined index submit notice....
Therefore you need to modify it to read as:
<input name="submit" class="btn btn-danger" type="submit" value="Register">
However, you have a second if ($_POST['submit']) { that needs to be removed; it's not needed.
You also have no name attribute for:
<textarea placeholder="Type your message here..." class="form-control" rows="5"></textarea>
that needs to be modified to and adding name="tmessage" since $message = $_POST['tmessage']; is most likely to be used for the message:
<textarea name="tmessage" placeholder="Type your message here..." class="form-control" rows="5"></textarea>
Also, you've used the same name here for both "state" and "zip":
<input class="form-control" type="text" placeholder="Zip" name="tstate">
which should read as:
<input class="form-control" type="text" placeholder="Zip" name="tzip">
Then, you're outputting before header having the '<h2>Thank You!</h2>'; and header:
Sidenote: You may have meant to do echo '<h2>Thank You!</h2>';, yet that would still constitute as outputting before header, because PHP is actually throwing you a parse error, but your server is probably not set up to catch and display notices/errors etc.
if (mail ($to, $subject, $body, $from)) {
'<h2>Thank You!</h2>';
header("Location: index.html");
}
You need to choose from "one" of those and not both. Either modify '<h2>Thank You!</h2>'; to read as echo '<h2>Thank You!</h2>'; and remove the header, or remove the "Thank you" and use header.
Consult the following on Stack about outputting before header:
How to fix "Headers already sent" error in PHP
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Here's a rewrite and see the embedded comments:
<?php
if(isset($_POST['submit'])){
$from = "minguswrestling.com";
$to = "puremeld#gmail.com";
$name = $_POST['tname'];
$email = $_POST['temail'];
$school = $_POST['tclub'];
$address = $_POST['taddress'];
$city = $_POST['tcity'];
$state = $_POST['tstate'];
$zip = $_POST['tzip'];
$type = $_POST['type'];
$message = $_POST['tmessage'];
$subject = "Tourney Signup!";
$body = "From: $from \n Name: $name \n Email: $email \n School: $school\n Address: $address\n City: $city State: $state Zip: $zip\n Event: $type\n Message: $message";
if (mail ($to, $subject, $body, $from)) {
echo '<h2>Thank You!</h2>'; // either use echo
// header("Location: index.html"); // or header, not both. Comment one or the other out.
} else {
echo '<p>Oops! An error occurred. Try sending your message again. </p>';
}
}
else { echo "Submit is not set."; }
?>
Footnotes:
It would be best to also use !empty() against your POST arrays, since anyone could use your form and pass empty values.
Reference:
http://php.net/manual/en/function.empty.php
Plus, $from = "minguswrestling.com"; be careful with that. mail() expects that to be an E-mail address and not a domain and may end up in Spam.
Best to use a From: as stated in the manual.
Consult the manual:
http://php.net/manual/en/function.mail.php
Final notes.
It's unclear if you're using this from your own PC or a hosted service.
If it's from your own PC, then a Webserver needs to be installed and properly configured, including PHP and mail.
If from a hosted service, make sure that mail is made available to you.
Check your Spam also when testing.
If you're going to use header, add exit; for it, otherwise your code may want to continue executing:
header("Location: index.html");
exit;
Remember not to use the "echo" if you're going to use header.

The following:
'<h2>Thank You!</h2>';
header("Location: index.html");
Is wrong, you cannot output anything before a header. Remove:
'<h2>Thank You!</h2>';

Related

PHP mail function not working on Hostgator [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
this is my site:
http://feriapixel.cl...nway/index.html
I've been trying other stuff and now i have the php code and the form in the same index.php file.
The form now looks like this
<form action="index.php" method="post">
<div class="form-group">
<input type="text" class="form-control" id="nombre" placeholder="Nombre" name="nombre">
</div>
<div class="form-group">
<input type="text" class="form-control" id="apellido" placeholder="Apellido" name="apellido">
</div>
<div class="form-group">
<input type="text" class="form-control" id="telefono" placeholder="Número Móvil" name="telefono">
</div>
<div class="form-group">
<input type="mail" class="form-control" id="email" placeholder="Email" name="email">
</div>
<button type="submit" name="submit" id="submit" class="btn btn-ganarplata">QUIERO GANAR MÁS PLATA<br> VENDIENDO CLEAN WAY</button>
</form>
And i have the php at the beginning of the file like this:
<?php
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$telefono = $_POST['telefono'];
$mail = $_POST['mail'];
$from = 'From: Cleanway';
$to = 'alteizen#gmail.com';
$subject = 'Formulario de contacto Cleanway';
$body ="De: $nombre\n $apellido\n E-Mail: $mail\n Fono: $telefono\n ";
?>
<?
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Su mensaje ha sido enviado</p>';
} else {
echo '<p>No se pudo mandar su mensaje</p>';
}
}
?>
And now i have checked out and it is not a hosting issue, because if i take out the following if statement:
if ($_POST['submit']) {
The mail is automatically sent when i load the page. So the problem is with the if statement, something is wrong there.
Problem solved, this was missing:
QUIERO GANAR MÁS PLATA VENDIENDO CLEAN WAY
The button needed a "submit" value for sending the mail. That was all, thanks to me.
You need to add an email address to $headers so that when you mail, the from address can be seen. Actually, your web server needs an MX record. Use it like this.
NOTE: If you use an email that is not your website's email(like xxx#gmail.com) it won't gonna work.
$headers .= "From: Name <email#example.com>";
So I think this will gonna work.

PHP Mail Script hitting error [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have a PHP mail script set up and keep hitting the error "Something went wrong, go back and try again!" all the form fields have been checked and all the names match etc so I am wondering if there is something wrong with my script?
<form method="post" action="contact.php" id="contactForm">
<label for="name">Name</label>
<input type="text" id="name" name="name" class="name" />
<label for="email">Email</label>
<input type="text" id="email" name="email" class="email" />
<label for="phone">Phone</label>
<input type="text" id="phone" name="phone" class="phone" />
<label for="iam">I Am</label>
<select name="iam" class="iam" id="iam">
<option>a recruiter looking to recruit staff</option>
<option>a construction worker looking for work</option>
<option>requesting information</option>
</select>
<label for="message">Message</label>
<textarea name="message" id="message" class="message"></textarea>
<label for="captcha">What is 3+4?</label>
<input type="text" id="captcha" name="captcha" class="captcha" />
<input type="submit" name="submit" id="submit" class="submit" />
</form>
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$iam = $_POST['iam'];
$human = $_POST['captcha'];
$message = $_POST['message'];
$from = 'From: Test';
$to = 'sales#test.com';
$headers = "From: $email";
$subject = 'Tradeline Contact';
$body = "From: $name\n E-Mail: $email\n Phone Number:\n $phone I Am:\n $iam Message:\n $message";
if ($_POST['submit'] && $human == '7') {
if (mail($to, $subject, $body, $headers, "-f " . $from)) {
echo '<p>Your message has been sent!</p>';
header( 'Location: http://urlhere.com/thankyou.html' ) ;
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '7') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
Any help is appreciated.
You might want to change:
mail($to, $subject, $body, $headers, "-f " . $from)
to:
mail($to, $subject, $body, $headers."\r\n")
That way your mail headers will be in compliance.
Also, turn on error reporting. I happen to use error_reporting(7); right under the <?php line to turn on all common errors with the exception of catching undefined variables, and that will tell me if the mail function has problems.
Another thing you can do is check the mail server logs to see if mail is actually being sent.
I'm sure you already did this, but in case you haven't, make sure you use valid email addresses.

PHP error with contact form

I am currently creating a contact form and with the help of thw world wide web I made a php background, which I also understand.
Unfortunately it looks al great to me, but I do get aan error, saying:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\jquery\mail.php on line 16 [line 16 is the place where the if statement with the mail and the attributes appaeat, see below]
I have honestly no clue what the error means, and my code is below, does anybody now what it means and how I could fix it? Thanks in advance!
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$city = $_POST['city'];
$message = $_POST['message'];
$from = 'From: timohoogie';
$to = 'ownemail#gmail.com';
$subject = 'Contact zoeken met whoduniit';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n City: $city\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
<form method="post" action="mail.php">
<label for="Name">Naam:</label>
<input type="text" name="name" id="name" />
<label for="City">Stad:</label>
<input type="text" name="city" id="city" />
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
<label for="gevonden"> Hoe heeft u ons gevonden</label>
<input type="text" name="gevonden" id="gevonden" />
<br>
<br>
<label for="Message">Bericht:</label><br />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
You need to have a SMTP server running on the machine that hosts this code with your current settings. Or else you need to change the SMTP settings in your php.ini file.
Read more about how to set this up here: http://www.quackit.com/php/tutorial/php_mail_configuration.cfm
If you want to set up your own SMTP server, I dont have any good tips on top of my head, but there are several free server softwares out there. Although, you would probably be better off using a public one, or even you ISP SMTP server.
mail is a PHP function, yes. But it must be installed on the server that is doing the actual sending. SMTP is a slightly different protocol that seems to be getting triggered because the simple mail function is failing (for it not being installed.)
You need to check with your service provider to see what mail function you should be using.

Why isn't my php contact form sending?

I have a simple contact form appearing on a modal in bootstrap. For some reason my form will not send. I've been looking at it for the last few hours and could use a fresh pair of eyes. It seems so simple but I'm at a total loss.
I've put a non-displayed textfield in the form as a spam filter. Since most bots would fill the textfield I want the code to send only if the textfield is blank (which it should be if the user is human since it's not displayed).
index.php
<div class = "modal fade" id = "contact" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<a id = "btn-close" data-dismiss = "modal">X</a>
</div>
<form>
<form method="post" action="index.php">
<fieldset id= "contact-fieldset">
<input name="name" type="text" id="name" class="text-input" placeholder="First and last name" required>
<input name="email" type="email" placeholder="Email address" required>
<textarea name="message" placeholder="Your Message" required></textarea>
<input name="bot-catch" type="text" id="bot-catch">
<input id="submit" class="submit-btn" name="submit" type="submit" value="Submit">
</fieldset>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Me';
$to = 'me#email.com';
$subject = 'Hello';
$human = $_POST['bot-catch'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
</form>
<div class = "modal-footer">
</div>
</div>
</div>
</div>
EDIT: So I have now gotten my message to send, but only if I put the PHP code into a separate file and link to it by . Only problem is- I do not want the user to be directed off of my page once they send a message. Is there anything I can do? I still can't figure out why it won't send on my index page.
EDIT: Well I got it working!! Thank you for all your help!

PHP Form Submit Button Unclickable

SOLVED - permissions
I want to walk through my debug process so that it might help anyone else working through the same thing... 1) I wiped both pages and replaced with the code that I knew worked. 2) I then changed the form piece by piece until I got it how i wanted and continued testing 3) I then copied the current php file completely and redirected my form to it. 4) it failed... I changed the permissions to 655 and wallah it worked. Now I can go about hacking about the PHP code to get what I want. thanks for all of the suggestions, you definitely led me down the road to my solution
SOLVED
I have two separate intake forms on a site. Intake form 1 works perfectly. I takes, name, email and comment and sends it through a sendmail script.
I also wanted an intake form for lead capture to track those that want to access the demo videos so I modified the code from the form (for the new page) and then created an additional php file called videoform.php - which is basically just a modified version of my sendmail.php file.
When I fill out the form it does nothing when I click on submit. It validates, as it not let you enter a null value in any of the fields but I am not sure what I am missing. Is it something simple (I am by no means PHP reliable) or can I simply not do that?
Here is the form and the php:
<div class="message"></div>
<form action="./php/videoform.php" method="POST" id="contact-form">
<p class="column one-half">
<input name="name" type="text" placeholder="Your Name" required>
</p>
<p class="column one-half">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="column one-half">
<input name="phone" type="text" placeholder="Your Phone" required>
</p>
<p>
<input name="submit" type="submit" value="Submit">
</p>
</form>
</div>
This is the PHP
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$content = $_POST['content'];
$subject = "You've been contacted by $name";
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
header("Location: /video-form.html");
exit;
}
?>
I am comfortable with a number of coding formats but I am so weak when it comes to PHP. Any insight would be both appreciated and get me on the road to understanding PHP better.
Working scripts for comparison
Form
Send us a message
<p class="column one-half last">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="clear">
<textarea name="comment" placeholder="Your Message" cols="5" rows="3" required></textarea>
</p>
<p>
<input name="submit" type="submit" value="Comment">
</p>
</form>
</div>
PHP sendmail.php file
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$subject = "You've been contacted by $name";
$content = "$name sent you a message from your enquiry form:\r\n\n";
$content .= "Contact Reason: $comment \n\nEmail: $email \n\n";
if(#mail($to, $subject, $content, "Reply-To: $email \r\n")) {
echo "<h5 class='success'>Message Sent</h5>";
echo "<br/><p class='success'>Thank you <strong>$name</strong>, your message has been submitted and someone will contact you shortly.</p>";
}else{
echo "<h5 class='failure'>Sorry, Try again Later.</h5>";
}?>
From your php:
//...
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
//...
You never define $success. Since it doesn't have a value, if ($success) fails, and it always enters the else portion of the statement. It looks like you're missing a line that's something like $success = mail($to, $subject, $content);

Categories