1

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>

1 Answer 1

0

So here is hot it would be your form part

<form name="com" id="com" action="leavehandle.php" method="post">

your leavehandle.php fixed

<!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');

//set vars to null 
$fname = $lname = $email = $comment = "";

//validade post 



if(!empty($_POST['fname'])&&!empty($_POST['lname'])&&!empty($_POST['email'])&&!empty($_POST['comment'])) {
   

$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);
 
} else {
	
	//redirect to error page is is null 
	header("Location: leave.php");
}

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>

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Otávio, I edited my original post with another question. I'm also not sure where to put the php code in my leavehandle.php: if(!empty($_POST['email'])
I added my leavehandle.php code. I also have to make sure that the form is validated and all fields are required. That's why I was using this in my original form action: if (($_POST['email'] != NULL) && ($_POST['comment'] != NULL) && ($_POST['fname'] != NULL) && ($_POST['lname'] != NULL))
Just wait I will fix it , it can be validated in the leavehandler.php I will fix it and send you
I changed yout code, test right now and see how it goes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.