Saturday, December 22, 2018

php - One of my form's data doesn't seem to be setting properly when attempting to submit via javascript

I'm working on a website that will submit 2 forms, a hidden one that will send the data to a database and the visible one that will mail the form contents to a user. I looked up how to send multiple forms via javascript, and when I sent it the form that sent data to the database would work but the form that sent the data in an email wouldn't, the email would just be empty. I have hMailServer setup for local testing currently.



**index.php forms



























// 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
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 "

Message has been sent. Thank you!

";
echo "";
$_SESSION = [];

}
?>


// storeData.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

function sendMail(){
$name = $_POST["name"];
$email = $_POST["email"];
$subject = "website message: " . $_POST["subject"];
$message = $_POST["message"];

$to = "websiteownersemail";
$body = "

website Message










Name
$name

$message



";
$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();
?>


I expect both forms to submit, but clearly something isn't working correctly with form1. I originally just had isset($_POST) in my php files which would allow the database to be filled but the email sent to be empty. I put isset($_POST["name"]) as a test to see if form1 data was setting, which it wasn't.




Edit included sendmail and storedata php files

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...