2016-04-22 2 views
0

Ich habe ein Formular, mit dem ein Benutzer von einem Nicht-Benutzer angemeldet werden kann, nimmt das Skript den Inhalt des Formulars, speichert sie in meiner messages Tabelle und sendet dann schließlich den Benutzer den Inhalt an ihre E-Mail-Adresse.E-Mail senden und schreiben in Datenbank funktioniert nicht

Ich habe Probleme herauszufinden, warum mein Formular die Fehlermeldung, die ich eingestellt habe, nicht abschicken und zurückgeben kann, wenn sie nicht korrekt ausgeführt wurde. Es werden keine E-Mails versendet und es erfolgt kein Eintrag in der Datenbanktabelle.

Mein Formularcode;

<form class="signup-form" action="sendmail.php" id="email_submit" method="POST"> 
<fieldset> 
<input type="text" name="msg_touserid" id="msg_touserid" value="<?php echo htmlentities($_GET["uid"], ENT_QUOTES, 'UTF-8'); ?>" style="display: none;"> 
<input type="text" name="msg_tousername" id="msg_tousername" value="<?php echo htmlentities($_GET["username"], ENT_QUOTES, 'UTF-8'); ?>" style="display: none;"> 
<input type="text" placeholder="Your name..." name="msg_fromname" id="msg_fromname" value=""><br /> 
<input type="text" placeholder="Your mobile number..." name="msg_mobile" id="msg_mobile" value=""><br /> 
<input type="email" name="msg_toemail" id="msg_toemail" value="[email protected]" style="display: none;"> 

<input type="email" placeholder="Your email..." name="msg_fromemail" id="msg_fromemail" value=""><br /> 
<input type="text" placeholder="Message subject..." name="msg_subject" id="msg_subject" value=""><br /> 
<textarea name="msg_messagebody" id="msg_messagebody" style="height: 282px; background-image: none; background-position: 0px 50%; background-repeat: repeat;"></textarea> 
</fieldset> 
<input type="submit" class="btn-colored submit-send-email" value="Send email" /> 
</form> 

Mein sendmail.php Code;

$connection = mysql_connect('localhost', 'XXX', 'XXX'); 
    if (!$connection){ 
     die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db('DB'); 
    if (isset($_POST['email_submit'])){ 
     $msg_touserid = $_POST['msg_touserid']; 
     $msg_tousername = $_POST['msg_tousername']; 
     $msg_fromname = $_POST['msg_fromname']; 
     $msg_mobile = $_POST['msg_mobile']; 
     $msg_toemail = $_POST['msg_toemail']; 
     $msg_fromemail = $_POST['msg_fromemail']; 
     $msg_subject = $_POST['msg_subject']; 
     $msg_messagebody = $_POST['msg_messagebody']; 

     $sql = "INSERT INTO messages (msg_touserid, msg_tousername, msg_fromname, msg_mobile, msg_toemail, msg_fromemail, msg_subject, msg_messagebody) 
       VALUES ('$msg_touserid', $msg_tousername', '$msg_fromname', '$msg_mobile', '$msg_toemail', '$msg_fromemail', '$msg_subject', '$msg_messagebody')"; 
     if (!mysql_query($sql,$connection)){ 
      die('Error: ' . mysql_error()); 
     } 

     $emailID = "$msg_toemail"; 
     $subject = "Enquiry from. $msg_fromname . through our website"; 
$body = <<<EOD 

     <table cellspacing="0" cellpadding="1" border="1"> 
      <tbody> 
       <tr> 
        <td style="padding: 5px 10px;" width="150">Name: </td> 
        <td style="padding: 5px 10px;">$msg_fromname</td> 
       </tr> 
       <tr> 
        <td style="padding: 5px 10px;" width="150">Mobile: </td> 
        <td style="padding: 5px 10px;">$msg_mobile</td> 
       </tr> 
       <tr> 
        <td style="padding: 5px 10px;" width="150">Email: </td> 
        <td style="padding: 5px 10px;">$msg_fromemail</td> 
       </tr> 
       <tr> 
        <td style="padding: 5px 10px;" width="150">Message: </td> 
        <td style="padding: 5px 10px;">$msg_messagebody</td> 
       </tr> 
      </tbody> 
     </table> 

EOD; 

     $headers = "From: [email protected]\r\n"; 
     $headers .= "MIME-Version: 1.0\r\n"; 
     $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; 
     $headers .= "X-Priority: 1\r\n"; 
     $headers .= "X-MSMail-Priority: High\n"; 
     $headers .= "X-Mailer: PHP". phpversion() ."\r\n"; 

     mail($emailID, $subject, $body, $headers); 
     echo "<h4>Thank you for your message.</h4>"; 

    } else { 
     echo("Oops... Please check you have completed the form correctly."); 
    }; 

Meine Datenbankstruktur;

id      int(10) (AI) 
msg_touserid   int(10) 
msg_tousername   varchar(100) 
msg_fromname   varchar(100) 
msg_mobile    varchar(20) 
msg_toemail    varchar(100) 
msg_fromemail   varchar(100)  
msg_subject    varchar(200) 
msg_messagebody   varchar(1000) 
msg_sent    timestamp 
+0

Nichts in 'if (iset ($ _ POST ['email_submit'])) {...}' wird jemals ausgelöst, und http://php.net/manual/en/function.error-reporting.php hätte dir davon erzählt ;-) –

+1

[** SQL injection alert **] (http://stackoverflow.com/q/60174/) –

Antwort

0

Die Submit-Button nicht name Attribut hat.

<input type="submit" class="btn-colored submit-send-email" value="Send email" /> 

Also, die condtion

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

ist nicht befriedigend.

Lösung:

hinzufügen name Taste einzureichen.

Corrected Code:

<input type="submit" class="btn-colored submit-send-email" value="Send email" name="email_submit" /> 
+0

danke. Das scheint sich weiterentwickelt zu haben, aber dann wurde mir klar, dass ich die Auswahl einer Datenbank verpasste, also fügte ich sie hinzu, aber jetzt bekomme ich einen Syntaxfehler. Ich habe meinen sendmail.php-Code oben aktualisiert, um die Änderung widerzuspiegeln. –

0

Es war nicht Absenden/Senden einer E-Mail, weil es nie die Chance hatte.

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

wird nie so email_submit erfüllen ist in Ihrem Formular definiert. Sie haben es als die Formular-ID gesetzt, aber die IDs werden nicht an $_POST in der gleichen Weise wie name übergeben.

Geben Sie Ihre Absenden-Button einen Namen email_submit in Ihrer Form:

<input name="email_submit" type="submit" class="btn-colored submit-send-email" value="Send email"> 

Nun, da Anfangsbedingung erfüllen.

Verwandte Themen