gpt4 book ai didi

javascript - 未定义索引 : formSubmit

转载 作者:行者123 更新时间:2023-11-28 18:56:24 27 4
gpt4 key购买 nike

我正在构建一个用于将数据输入数据库的表单。我已经构建了基本的表单和验证(我认为)。当我尝试在我的服务器/域上测试它时,我不断收到错误未定义索引:表单提交。我确信这是一件小事,但我找不到我的错误。如果有更多人关注这一点,我们将不胜感激。

<!DOCTYPE HTML>
<html lang = "en">
<head>
<link rel="stylesheet" type="text/css" href="mystylesheet.css">
<title>Tech Order Department.html</title>
<meta charset = "UTF-8" />

</head>

<body>

<!--Designing my form-->

<h2>New Projects</h2>

<br>

<?php
if($_POST['formSubmit'] == "Submit")
{
$varMovie = $_POST['formProject'];
$varMovie = $_POST['formClient'];
$varMovie = $_POST['formLastName'];
$varMovie = $_POST['formDateReceived'];
}
?>


<form action="myform.php" method="post">

Project:<br>
<input type="text" name="Project">
<br>
Client:<br>
<input type="text" name="Client">

<br>
Last Name:<br>
<input type="text" name="LastName">

<br>
Date Received:<br>
<input type="text" name="DateReceived">


<br><br>

<input type="submit" name="formSubmit" value="Submit">

</form>

<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";

if(empty($_POST['formProject']))
{
$errorMessage .= "<li>You forgot to enter a project name!</li>";
}
if(empty($_POST['formClient']))
{
$errorMessage .= "<li>You forgot to enter a client name!</li>";
}
if(empty($_POST['formLastName']))
{
$errorMessage .= "<li>You forgot to enter the tech writer name!</li>";
}
if(empty($_POST['formDateReceived']))
{
$errorMessage .= "<li>You forgot to enter the date received!</li>";
}


$varMovie = $_POST['formProject'];
$varName = $_POST['formClient'];
$varMovie = $_POST['formLastName'];
$varMovie = $_POST['formDateReceived'];

if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}

}

?>


</body>

</html>

最佳答案

你应该改变:

  if($_POST['formSubmit'] == "Submit") 

致:

  if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit") 

当您使用表单时,您应该始终至少在发布后验证您的字段是否已设置。

例如:

我们有这个快速表格:

<form action="action_page.php" method="POST">
First name:<br>
<input type="text" name="firstname" value="">
<br>
Last name:<br>
<input type="text" name="lastname" value="">
<br><br>
<input type="submit" value="Submit">
</form>

首先请注意,我添加了两个属性:

  • action:表单将发送到的位置
  • 方法:表单的发送方式

有两个输入“firstname”、“lastname”和一个提交按钮。

假设您要在“action_page.php”上显示:“您好,名字姓氏”。

您可以执行以下操作:

<?php 
echo 'Hello, '.$_POST['firstname'].' '.$_POST['lastname'];
?>

这将打印出你想要的内容。但如果这些字段为空,则会创建“未定义索引”...

因此,为了确保您不会使用不存在的变量,您应该执行以下操作:

<?php 

//First we create an array that will errors if they occurs
$error = array();

//Then we test if firstname exist
if(isset($_POST['firstname'])) {
//if it does we can assign him.
$firstname = $_POST['firstname'];
}else{
//if it doesn't that's an error.
$error[] = 'Please enter your firstname';
}

//Then we test if lastname exist
if (isset($_POST['lastname'])) {
//if it does we can assign him.
$lastname = $_POST['lastname'];
}else{
//if it doesn't that's an error.
$error[] = 'Please enter your lastname';
}

//In the end we check if we have any error stocked:
if(empty($error)) {
//if it's empty we have all our required data to display our echo
echo 'Hello, '.$firstname.' '.$lastname;
}else{
//else let's show the error
print_r($error);
}
?>

通过这种方式,我们可以在任何可能的“未定义索引”发生之前对其进行处理,并且我们确信我们的数据将返回某些内容或至少有一个相关的错误。

关于javascript - 未定义索引 : formSubmit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33567519/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com