0

Ive made a Contact form for my Club. here And now i got stuck with the issue that the form won't deliver the Information from the form which had been inputted to the email address. I don't know if there is a Problem with the html Code or PHP so ill post both. HTML:

<form action="#" method="post" style="max-width: 75%;">
  <table width="100%" border="0" cellpadding="3" cellspacing="1">
    <tr>
      <td align="right">*Anrede:</td>
      <td align="left">
        <input type="radio" name=Anrede value="Hr.">Herr
        <input type="radio" name=Anrede value="Fr.">Frau</td>
    </tr>
    <tr>
      <td width="140" align="right">*Nachname:</td>
      <td align="left">
        <input type="text" name="nachname" value="" style="width:100%;" />
      </td>
    </tr>
    <tr>
      <td align="right">*Vorname:</td>
      <td align="left">
        <input type="text" name="vorname" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">Firma:</td>
      <td align="left">
        <input type="text" name="firma" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*Postanschrift:</td>
      <td align="left">
        <input type="text" name="strasse" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*Telefon:</td>
      <td align="left">
        <input type="text" name="telefon" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*E-Mail:</td>
      <td align="left">
        <input type="text" name="email" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*Buchungstag:</td>
      <td align="left">
        <input type="text" id="datepicker" />
      </td>
    </tr>
    <tr>
      <td align="right">*Erwachsene:</td>
      <td align="left">
        <select name="Erwachsene" style="width: 145px;">
          <option selected="selected" disabled="disabled">Bitte ausw&auml;hlen</option>
          <?php for ($i=0 ; $i <=3 0; $i++) : ?>
          <option value="<?php echo $i; ?>">
            <?php echo $i; ?>
          </option>
          <?php endfor; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td align="right">*Jugend:</td>
      <td align="left">
        <select name="Jugend" style="width: 145px;">
          <option selected="selected" disabled="disabled">Bitte ausw&auml;hlen</option>
          <?php for ($i=0 ; $i <=2 5; $i++) : ?>
          <option value="<?php echo $i; ?>">
            <?php echo $i; ?>
          </option>
          <?php endfor; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td align="right">*Event:</td>
      <td align="left">
        <input type="radio" name=myradio value="extern" checked>extern
        <input type="radio" name=myradio value="intern">intern</td>
    </tr>
    <tr>
      <td colspan="2">
        <textarea rows="12" style="width:100%" name="text" placeholder="zus&auml;tzliche Informationen ..."></textarea>
      </td>
      <td></td>
    </tr>
    <tr>
      <td colspan="2" align="left" nowrap>
        <br />
        <input type="reset" value="Formular l&ouml;schen" style="width:49%" />
        <input type="submit" name="submit" value="Formular absenden" style="width:49%" />
      </td>
    </tr>
  </table>
</form>
<p>*Pflichtfelder</p>
<br />

PHP:

<?php 
if(isset($_POST['submit'])){


$anrede = $_POST['Anrede'];
$last_name = $_POST['Nachname'];//required
$first_name = $_POST['Vorname'];//required
$Firma = $_POST['Firma'];
$Anschrift = $_POST['Postanschrift'];//required
$tel = $_POST['telefon'];//required
$from = $_POST['email'];//required
$tag = $_POST['buchungstag'];//required
$erwach = $_POST['Erwachsene'];//required
$jugend = $_POST['Jugend'];//required
$zusatz = $_POST['text'];

$to= "kontakt@acherner-bogenclub.de , $from";
$subject = "Anfrage auf belegung des Bogenplatzes";

$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$from)) {

$error_message .= 'bitte einen gultige email eingelben.<br />';

}

$string_exp = "/^[A-Za-z .']+$/";

if(!preg_match($string_exp,$F_name)) {

$error_message .= 'Dein vorname ist nicht gültig.<br />';

}

$string_exp = "/^[A-Za-z ']+$/";

if(!preg_match($string_exp,$Anschrift)) {

$error_message .= 'bitte eine gültige addresse eingeben.<br />';

} 

if(!preg_match($string_exp,$L_name)) {

$error_message .= 'Dein nachname ist nicht gültig<br />';

}


if(isset($_REQUEST['Erwachsene']) && $_REQUEST['Erwachsene'] == '0') { 
echo 'Bitte anzahl der Erwachsene auswählen.<br /> '; 

} 
if(isset($_REQUEST['Jugend']) && $_REQUEST['Jugend'] == '0') { 
echo 'Bitte anzahl der Jugendlichen auswählen.<br /> '; 

} 
  //datepicker validation????

if(strlen($error_message) > 0) {

die($error_message);

}

$email_message = "Kontaktformular details siehe unten.\n\n";



function clean_string($string) {

$bad = array("content-type","bcc:","to:","cc:","href");

return str_replace($bad,"",$string);

}

$email_message .= "Anrede: ".clean_string($anrede)."\n";

$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Firma: ".clean_string($Firma)."\n";

$email_message .= "Postanschrift: ".clean_string($Anschrift)."\n";

$email_message .= "Telefonnummer: ".clean_string($tel)."\n";

$email_message .= "Email: ".clean_string($from)."\n";

$email_message .= "Buchungstag: ".clean_string($tag)."\n";

$email_message .= "Anzahl der Erwachsenen: ".clean_string($erwach)."\n";

$email_message .= "Anzahl der Jugendlichen: ".clean_string($jugend)."\n";

$email_message .= "Zusätzliche Informationen: ".clean_string($zusatz)."\n";



// create email headers

$headers = 'From: '.$from."\r\n".

@mail($to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->


Vielen dank. Wir werden sobald wie möglich mit ihnen in verbindung treten. 


<?php

}

?>

I hope that somebody could help me with that and also the Validation does not work for some reason and I've tried so many different ways but never worked.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mark
  • 103
  • 2
  • 11
  • what's the error that you are getting? – Danyal Sandeelo Feb 22 '16 at 11:14
  • 1
    Remove the `@` in front of your mail command, it suppresses error messages that might be helpful. Check what variables contain. Check if mail sending basically works by using `mail()` directly with strings, no variables. – Gerald Schneider Feb 22 '16 at 11:21
  • 2
    Note about your checks: They will fail on names and addresses containing umlauts. – Gerald Schneider Feb 22 '16 at 11:24
  • Is your PHP code in the same file as your HTML / or Using AJAX ? I see that your action attribute is empty, if you have your PHP script in a separate file specify the correct path in action. – Vishnu Nair Feb 22 '16 at 11:28
  • @Vincent the php is in the same file as the html – Mark Feb 22 '16 at 11:33
  • @GeraldSchneider could u maybe do an example then for Umlauts pls? – Mark Feb 22 '16 at 11:34
  • 1
    The `@` before the `mail` will suppress **any error** that may happen during the send process. Moreover, I personally suggest you to use a mailing library like phpmailer: https://github.com/PHPMailer/PHPMailer . It will surely solve most of your problems and will tell you exactly what is happening if a mail encountered any issue when sent. That said, please check if all the POST variables are being received. (also, the encoding at the beginning of the php file is a must in my opinion, do not forget about it) – briosheje Feb 22 '16 at 11:35
  • Maybe not related to your issue, but also your e-mail validation regex is Just Plain Wrong. The good people at `info@dot.swiss` would not appreciate the assumption that a TLD can only have 4 letters, for example. See also: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Piskvor left the building Feb 22 '16 at 11:39
  • ive delited the @ infront of the mail but i didnt Change anything it only stays at the contakt form with out posting al of the variables :/ @briosheje – Mark Feb 22 '16 at 11:40
  • 1
    what is `print_r($_POST);` printing? if it's empty, then the if below is not even being joined. (also, keep in mind that the exit is just for debugging purposes, everything **after** the exit won't be executed, the purpose of it, right now, is to check whether any POST data is being received) – briosheje Feb 22 '16 at 11:42
  • it prints: Array ( [nachname] => sdf [vorname] => [firma] => [strasse] => [telefon] => fdg [email] => [myradio] => extern [text] => [submit] => Formular absenden ) @briosheje – Mark Feb 22 '16 at 11:44
  • 1
    As for validation, László Müller (his mother is Hungarian ;)) is not going to appreciate you telling him that his name is not g`ü`ltig. (`ü` won't match `[A-Za-z ]`, etc.) And God forbid someone actually types in "Etwasstraße" instead of "Etwasstrasse". – Piskvor left the building Feb 22 '16 at 11:44
  • Well, as long as the "email" field is empty (try to set action="#") it will never ever work, try to submit some data and check if it's all okay. if it is, remove the exit; and see what happens, if the mail is not sent there probably are some issues in the mail send configuration (in the php.ini, usually). That's why I'm personally recommending you to take a look at the library suggested above (phpmailer) in order to configure the SMTP and so on. Also, keep in mind that Piskvor's point is extremely important, validating in the correct way is a must. – briosheje Feb 22 '16 at 11:47
  • @Piskvor how would u then solve that issue then so that nobody gets excluded :D – Mark Feb 22 '16 at 11:51
  • Array ( [Anrede] => Hr. [nachname] => sdfsd [vorname] => dfdsf [firma] => dfd [strasse] => dfdrfgtv [telefon] => dfdf [email] => ******@gmial.com[Erwachsene] => 2 [Jugend] => 1 [myradio] => extern [text] => sdf [submit] => Formular absenden ) @briosheje ive entered some data but it still showed this and ive also did Action="#" – Mark Feb 22 '16 at 11:54
  • @Mark: As for e-mail, see the linked question. As for the other fields, just check that they're filled out with *something* - `if(trim($someField) === '') { // empty, show error }`. (Building a list of "these are the only possible characters in a name" is futile and *will* bite you: letters only? Then hope that Mike O'Hare never tries to send a form.) http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Piskvor left the building Feb 22 '16 at 11:56
  • If you're sending to `gmIAL.com`, I have an idea why it's not getting delivered to `gmAIL.com` ;) – Piskvor left the building Feb 22 '16 at 11:57
  • @Piskvor is sending it to kontakt@acherner-bogenclub.de but it doesnt work... – Mark Feb 22 '16 at 12:00
  • @Piskvor when i Input ure advice from bevor then mike O'Hare can sent a form? :D – Mark Feb 22 '16 at 12:00
  • First off, just because the e-mail leaves your server is *not* a guarantee that it will be delivered. That said, see http://blog.codinghorror.com/so-youd-like-to-send-some-email-through-code/ – Piskvor left the building Feb 22 '16 at 12:01
  • 1
    re validation: Try and see. I'm not going to write your code for you, I don't think you could afford that ;) – Piskvor left the building Feb 22 '16 at 12:02
  • 1
    @Mark : My final suggestion is to ask you to please rewrite your code using PHPMailer: read about SMTP authentication (and gmail authentication), as Piskvor said: the fact that the mail seems to be sent does NOT guarantee that it will be delivered. Use PHPMailer for such tasks, it's just beatiful, easier to read and works perfectly (and, most important, tells you if something is not working). About filtering: it's FULL of topics in stackoverflow but Piskvor, again, gave you all you need to accomplish everything, just click every link above and you will solve your problems ;) – briosheje Feb 22 '16 at 12:08

3 Answers3

2

Because your <form action='' tab is empty, the form is not submitting data when sending and reloading the page. By simply setting action='#' I have made your page work in as much as the print_r($_POST); now correctly displays the form content.

P.S> PHPMailer is absolutely the way to go, regarding PHP emails.

Further Notes:


 if(strlen($error_message) > 0) {
   died($error_message);
 }

The Die funtion should not be in the past tense.


$email_subject as seen in the mail(); function is an undefined variable.


$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Anzahl der Erwachsenen: ".clean_string($erwach)."\n";

These three variables ($first_name,$ast_name,$erwach) are also undefined.


PLEASE read and check your own code before coming to Stack Overflow. We love to help but we can't be expected to be your typo- and error- checkers.


Debug:

if (mail($to, $email_subject, $email_message, $headers)){
    die("Message was sent ok!!");
}

Your clean_string function looks pretty pointless and should be expanded into a full string cleaning function rather than an ad-hoc removal system.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • thanks but how come that this then doesnt sent the emails to the email addres given? – Mark Feb 22 '16 at 11:55
  • @Mark I have added some further notes for you. – Martin Feb 22 '16 at 12:10
  • thankls for the hint for my spelling mistakes UPS :O now atlest im getting redirectet to the thank you page. but the email still wont come through – Mark Feb 22 '16 at 13:06
0

Try adding

print_r($_POST);
exit; 

on the line before if(isset($_POST['submit'])){

This will then show you if the form is even posting to the PHP form and also show what it is posting.

Yan Foto
  • 10,850
  • 6
  • 57
  • 88
MarkB
  • 123
  • 1
  • 7
  • This should be a comment, not an answer. You can comment on any post when you reached 50 reputation points, until then please don't post comments as answers. – Gerald Schneider Feb 22 '16 at 11:22
  • http://www.acherner-bogenclub.de/contakt2.php so i just have added ure addvice but i have no clue what this is telling me maybe ull have a look.@user2958426 @Gerald schneider ive changed what u said but it will not sent the email and only stays at the form even tho when clicked submit :/ – Mark Feb 22 '16 at 11:32
0

Use PHP code above the HTML code if using on the same page.

And PHP code needs few correction as mention below:

<?php 
if(isset($_POST['submit'])){


$anrede = $_POST['Anrede'];
$L_name = $_POST['Nachname'];//required
$F_name = $_POST['Vorname'];//required
$Firma = $_POST['Firma'];
$Anschrift = $_POST['Postanschrift'];//required
$tel = $_POST['telefon'];//required
$from = $_POST['email'];//required
$tag = $_POST['buchungstag'];//required
$ewach = $_POST['Erwachsene'];//required
$jugend = $_POST['Jugend'];//required
$zusatz = $_POST['text'];

$to= "kontakt@acherner-bogenclub.de , $from";
$subject = "Anfrage auf belegung des Bogenplatzes";

$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$from)) {

$error_message .= 'bitte einen gultige email eingelben.<br />';

}

$string_exp = "/^[A-Za-z .']+$/";

if(!preg_match($string_exp,$F_name)) {

$error_message .= 'Dein vorname ist nicht gültig.<br />';

}

$string_exp = "/^[A-Za-z ']+$/";

if(!preg_match($string_exp,$Anschrift)) {

$error_message .= 'bitte eine gültige addresse eingeben.<br />';

} 

if(!preg_match($string_exp,$L_name)) {

$error_message .= 'Dein nachname ist nicht gültig<br />';

}


if(isset($_REQUEST['Erwachsene']) && $_REQUEST['Erwachsene'] == '0') { 
echo 'Bitte anzahl der Erwachsene auswählen.<br /> '; 

} 
if(isset($_REQUEST['Jugend']) && $_REQUEST['Jugend'] == '0') { 
echo 'Bitte anzahl der Jugendlichen auswählen.<br /> '; 

} 
  //datepicker validation????

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Kontaktformular details siehe unten.\n\n";



function clean_string($string) {

$bad = array("content-type","bcc:","to:","cc:","href");

return str_replace($bad,"",$string);

}

$email_message .= "Anrede: ".clean_string($anrede)."\n";

$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Firma: ".clean_string($Firma)."\n";

$email_message .= "Postanschrift: ".clean_string($Anschrift)."\n";

$email_message .= "Telefonnummer: ".clean_string($tel)."\n";

$email_message .= "Email: ".clean_string($from)."\n";

$email_message .= "Buchungstag: ".clean_string($tag)."\n";

$email_message .= "Anzahl der Erwachsenen: ".clean_string($erwach)."\n";

$email_message .= "Anzahl der Jugendlichen: ".clean_string($jugend)."\n";

$email_message .= "Zusätzliche Informationen: ".clean_string($zusatz)."\n";



// create email headers

$headers = 'From: '.$from."\r\n".

@mail($to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->


Vielen dank. Wir werden sobald wie möglich mit ihnen in verbindung treten. 


<?php

}

?>
Apoorv
  • 231
  • 1
  • 8