尝试通过javascript提交时,我的表单数据之一似乎设置不正确

时间:2019-05-08 17:22:45

标签: javascript php forms post

我正在一个将提交2个表单的网站上工作,一个隐藏的表单会将数据发送到数据库,而一个可见的表单会将邮件内容发送给用户。我查找了如何通过javascript发送多个表格,当我发送表格时,将数据发送到数据库的表格可以使用,但是在电子邮件中发送数据的表格则无效,电子邮件将是空的。我目前有用于本地测试的hMailServer设置。

**index.php forms
<form id="form1" name="form1" method="POST" action="sendData.php">
                    <div class="col-sm-6">
                        <div class="form-controls left-sec">
                            <input id="name" name="name" type="text" placeholder="Your Name" pattern="[a-zA-Z0-9\s]+" required>
                            <input id="email" name="email" type="text" placeholder="Your Email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required>
                            <input id="subject" name="subject" type="text" placeholder="Subject" required>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-controls right-sec">
                            <textarea id="message" name="message" placeholder="Type Message" required></textarea>
                        </div>
                    </div>
                    <div class="col-sm-12">
                        <div class="form-controls">
                            <input id="submitButton" type="button" name="submit1" class="btn" value="Send Message">
                        </div>
                    </div>
                    </form>
<form id="form2" name="form2" style="display:none;" type="hidden" method="POST" action="sendData.php">
                        <input style="display:none;" type="hidden" id="dbname" name="dbname">
                        <input style="display:none;" type="hidden" id="dbemail" name="dbemail">
                        <input style="display:none;" type="hidden" id="dbsubject" name="dbsubject">
                        <input style="display:none;" type="hidden" id="dbmessage" name="dbmessage">
</form>
<script>
    "use strict";
    var btn = document.getElementById("submitButton");
    btn.onclick = submitForms; // calls function in forms.js, forms.js is included in index.php
</script>
// forms.js
"use strict";
function submitForms(){
    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;
    var subject = document.getElementById("subject").value;
    var message = document.getElementById("message").value;
    var dbname = document.getElementById("dbname");
    var dbemail = document.getElementById("dbemail");
    var dbsubject = document.getElementById("dbsubject");
    var dbmessage = document.getElementById("dbmessage");
    debugger;
    dbname.value = name;
    dbemail.value = email;
    dbsubject.value = subject;
    dbmessage.value = message;
    var f1 = document.getElementById("form1");
    var f2 = document.getElementById("form2");
    f1.submit();
    f2.submit();

}
// forms.js debugging, lines cut out for convenience, f1 and f1 elements dont seem to be registering correctly as inputs
"use strict";
function submitForms(){
    var name = document.getElementById("name").value; name = "testname"
    var dbname = document.getElementById("dbname"); dbname = input#dbname {accept: "", alt: "", autocomplete: "", autofocus: false, defaultChecked: false, …}
    var f1 = document.getElementById("form1"); f1 = form#form1
    var f2 = document.getElementById("form2"); f2 = form#form2 {0: input#dbname, 1: input#dbemail, 2: input#dbsubject, 3: input#dbmessage, acceptCharset: "", action: "http://localhost/test/sendData.php", autocomplete: "on", enctype: "application/x-www-form-urlencoded", encoding: "application/x-www-form-urlencoded", …}

    f1.submit();
    f2.submit();

}
// sendData.php
<?php
session_start();
include("../Include/storeData.php");
include("../Include/sendMail.php");
if(isset($_POST["name"])){ // if I use just isset($_POST), database info sends but mail is empty, I put in $_POST["name"] to test my theory that it wasn't setting correctly
    echo "<h1 style='font-weight: bolder; color: green; text-align: center;'>Message has been sent. Thank you!</h1>";
    echo "<script>setTimeout(\"location.href = 'index.php';\",1500);</script>";
    $_SESSION = [];
}
?>
// storeData.php
<?php
$host = "localhost";
$username = "root";
$password = "";
$link = mysqli_connect($host, $username, $password);
if(!$link){
    die("Could not connect: " . mysqli_error());
}

function checkExists($l){
    $db_selected = mysqli_select_db($l, "luberlaw");
    if(!$db_selected){
        $sql = "CREATE DATABASE db";
        if(mysqli_query($l, $sql)){
            mysqli_select_db($l, "db");
            $query = "CREATE TABLE msgData (ID int(11) AUTO_INCREMENT,
                      sName varchar(255) NOT NULL, sEmail varchar(255) NOT NULL, 
                      sSubject varchar(255) NOT NULL, sMessage varchar(3000) NOT NULL,
                      dateEntered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
                      PRIMARY KEY (ID))";
            $result = mysqli_query($l, $query);
        }else{
            echo "Error creating database: " . mysqli_error() . ".\n";
        }
    }
}

function storeData($l){
    $name = $_POST["dbname"];
    $email = $_POST["dbemail"];
    $subject = $_POST["dbsubject"];
    $message = $_POST["dbmessage"];
    $db_selected = mysqli_select_db($l, "luberlaw");
    $sql = "INSERT INTO msgData (sName, sEmail, sSubject, sMessage) VALUES (?, ?, ?, ?)";
    $stmt = mysqli_prepare($l, $sql);
    $stmt->bind_param("ssss", $name, $email, $subject, $message);
    $stmt->execute();
    $stmt->close();
    mysqli_close($l);
}

if(isset($_POST["name"])){
    checkExists($link);
    storeData($link);
}
//sendmail.php
<?php

function sendMail(){
    $name = $_POST["name"];
    $email = $_POST["email"];
    $subject = "website message: " . $_POST["subject"];
    $message = $_POST["message"];
    $to = "websiteownersemail";
    $body = "<html>
                <head>
                <title>website Message</title>
                </head>
                <body>
                <table>
                <tr>
                <th>Name</th>
                </tr>
                <tr>
                <td>$name</td>
                </tr>
                </table>
                <p>$message</p>
                </body>
                </html>";
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "Content-type: text/html; charset=iso-8859-1";
    $headers[] = "Bcc: myemail"; //testing purposes
    mail($to, $subject, $body, implode("\r\n", $headers));
}

if(isset($_POST["name"]))
    sendMail();
?>

我希望两种形式都可以提交,但是显然Form1无法正常工作。我最初只是在我的php文件中有isset($ _ POST),它将允许填充数据库,但发送的电子邮件为空。我放置了isset($ _ POST [“ name”])作为测试,以查看是否设置了form1数据。

编辑包含的sendmail和storageata php文件

1 个答案:

答案 0 :(得分:0)

您以错误的方式处理它。您在form1中具有“提交”按钮,该按钮仅将form1数据传递到PHP文件,您如何期望它也将发送form2

请将所有隐藏字段移至form1,并从所有隐藏字段中删除样式属性。