I am attempting to submit multiple values in separate HTML input fields using one button. These values will be entered into a MySQL table called answers with 3 columns: answerID, questionID and answerBody.
However, each input field is generated using a while loop in a separate PHP function, with the form id being set to 'questions'.
Is it possible to do this?
Here is my code:
survey.php:
<?php
require_once "config/config.php";
//Start the session to grab session details
session_start();
//Process data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
//Check if fields are empty
if (empty($_POST["answer"]))
{
//State error code
$answerError = 'Please answer this question.';
}
else
{
//Set answer
$answer = $_POST["answer"];
}
//Check if error variables have any values assigned
if (empty($answerError))
{
//Prepare database insert
$sql = "INSERT INTO answers (questionID, username, answerBody) VALUES (?,?,?)";
//Check if the statement has the connect and sql variables
if ($statement = mysqli_prepare($connect, $sql))
{
//Add variables to the statement
mysqli_stmt_bind_param($statement, "sss", $paramQuestion, $paramUsername, $paramAnswer);
//Set the parameter to the answer
$paramQuestion = $_POST["questionID"];
$paramUsername = $_SESSION["username"];
$paramAnswer = $answer;
//Execute statement with entered variable
if (mysqli_stmt_execute($statement))
{
//Redirect user to success page
header("location: thankyou.php");
}
else
{
echo "Something went wrong. Please try again later.";
}
//Close statement
mysqli_stmt_close($statement);
}
}
}
?>
<!DOCTYPE html>
<lang='en'>
<html>
<head>
<title>Survey</title>
<link rel="stylesheet" href="CSS/style.css">
</head>
<body>
<div class="wrapper">
<div class="header"><?php head(); ?>
<form align="right" name="form1" method="post" action="logout.php">
<p>Welcome, <?php echo $_SESSION["username"] ?>.</p>
<label class="logoutLblPos">
<input name="submit2" type="submit" id="submit2" value="Log Out">
</label>
</form>
</div>
<div class="wrapperContent">
<div class="content">
<?php getQuestions($connect); ?>
<button type="submit" form="questions">Submit</button>
</form>
</div>
</div>
</div>
</div>
<div class="footer"><p><?php if ($_SESSION["username"] == 'admin') { echo footer(); }; ?></p></div>
</body>
</html>
in function getQuestions:
function getQuestions($connect)
{
$query = "SELECT * FROM questions";
$result = mysqli_query($connect, $query);
if ($result)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$body = $row['questionBody'];
$questionID = $row['questionID'];
echo '<div class="entry">
<div class="questionTitle"><h3>' . $body . '</h3>
<form id="questions" action="survey.php" method="POST">
<input type="hidden" name="questionID" value="' . $questionID . '" />
<input type="text" name="answer" size="50" />
func
</form>
</div>
</div>';
}
}
}
You can't submit multiple forms via a submit form. You can either use XHR and send a request per form (which I don't recommend from a performance standpoint) or you can change your getQuestions
function so that it just adds new inputs to a single existing form. For looping through it easily in your backend, you can use the array format for your input names ie questionID[]
and answer[]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.