My form submit button has to be clicked twice before the form will submit. My professor gave me some Java Script code, which I've implemented below, that will click the submit button twice automatically, but it is not working.
My form action is shown below:
<form name="com" id="com" action="<?php if (($_POST['email'] != NULL) && ($_POST['comment'] != NULL) && ($_POST['fname'] != NULL) && ($_POST['lname'] != NULL)) {
echo "leavehandle.php";
$submit == "true";
} else {
echo "leave.php";
} ?>" method="post">
I also have this PHP script outside of my closing form tag:
<?php
if ($submit == "true") {
echo"<script>document.getElementById('com').submit();</script> ";
} ?>
My form submit button still needs to be clicked twice before the form will submit. Any help would be much appreciated - I don't have much experience with Java Script!
Thank you!
Edit:
I adjusted my code in the form action to the following:
<form name="com" id="com" action="<?php echo "leavehandle.php";
if ($submit == "true"){
} else {
echo "leave.php";
} ?>" method="post">
Do I need the PHP opening tag in the first part of the form action? Also, I'm not sure where to place the other code for validating the form in my leavehandle.php.
Leavehandle.php:
<!DOCTYPE html>
<html> <!--Opening HTML tag-->
<div align="center"> <!--Aligns content center-->
<head>
<?php //pulls in my header from another PHP page using Inlcude()
include("header.php");
?>
</head>
<style>
html {
font-family:arial;
background-color:#ffcc99;
}
</style>
<body>
<?php
include('mysqli_connect.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$query = "INSERT INTO guestbook (id, email, fname, lname, date, comment) VALUES (NULL, '$email', '$fname', '$lname', NOW(), '$comment');";
$result = mysqli_query($dbc, $query);
if ($result) {
echo "Thank you for submitting a comment $fname!";
}
else {
echo "ERROR";
}
?>
</body>
<?php //pulls in my footer from another PHP page using Inlcude()
include("footer.php");
?>
</div>
</html>