我填写了表单上的所有值,我的 PHP 仍然返回未定义索引
Posted
技术标签:
【中文标题】我填写了表单上的所有值,我的 PHP 仍然返回未定义索引【英文标题】:I fill in all the values on my form and my PHP still returns Undefined Index 【发布时间】:2018-04-09 16:34:01 【问题描述】:我开始学习 php,并在 localhost 中打开我的网站(我正在使用 xampp)我收到每个输入标签的未定义索引错误,但是,在使用该网站时,我确保填写所有内容,以便有没有缺失值。为什么它仍然会产生未定义的索引错误?我查看了其他问题并尝试重复他们的代码,但它仍然不起作用。
<form id="myForm" action="test.php">
<div id="Level1_Form"> <!--Use divs to space-->
<div id="ContainInputDiv1-1">
<p class="level1InputP">First Name: <span class="formError" id="FNameErrorP"></span></p>
<input type="text" class="Level1InputTxt" id="fName"/>
</div>
<div id="ContainInputDiv1-2">
<p class="level1InputP">Last Name: <span class="formError" id="LNameErrorP"></span></p>
<input type="text" class="Level1InputTxt" id="lName"/>
</div>
<div id="ContainInputDiv1-3">
<p class="level1InputP">E-Mail: <span class="formError" id="ErrorEmailP"></span></p>
<input type="text" class="Level1InputTxt" id="eMail"/>
<p id="ErrorEMailP"></p>
</div>
<div id="ContainInputDiv1-4">
<p class="level1InputP">Phone Number: <span class="formError" id="ErrorPhoneNumP"></span></p>
<input type="text" class="Level1InputTxt" id="phoneNumber"/>
</div>
</div>
<div id="Level2_Form">
<div id="ContainInputDiv2-1">
<p class="level2InputP">Street Address: <span class="formError" id="AddressErrorP"></span></p>
<input type="text" class="Level2InputTxt" id="address"/>
</div>
<div id="ContainInputDiv2-2">
<p class="level2InputP">Town: <span class="formError" id="TownErrorP"></span></p>
<input type="text" class="Level2InputTxt" id="town"/>
</div>
<div id="ContainInputDiv2-3">
<p class="level2InputP">Zip Code:<span class="formError" id="ErrorZipP"></span> </p>
<input type="text" class="Level2InputTxt" id="zip"/>
</div>
<div id="ContainInputDiv2-4">
<p class="level2InputP">State: </p> <!--Rearrange to street address, town, state, zip-->
<select id="State_Select" class="Select">
<option value="NH">New Hampshire</option>
<option value="ME">Maine</option>
<option value="MA">Massachusetts</option>
</select>
</div>
</div>
<div id="Level3_Form">
<div id="ContainInputDiv3-1">
<p class="level3InputP">Date: </p>
<div style="dispay: inline;">
<select id="Month_Select" class="Select">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select id="Day_Select" class="Select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select id="Year_Select" class="Select">
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
</select>
</div>
</div>
<div id="ContainInputDiv3-2">
<p class="level3InputP">Number of People: <span class="formError" id="ErrorNumOfPeopleP"></span></p>
<input type="text" id="NumOfPeople" value="Integer Value Only"/>
</div>
<div id="ContainInputDiv3-3">
<p class="level3InputP">Event Type: <span class="formError" id="EventTypeErrorP"></span></p>
<input type="text" id="eventType" value="Wedding, Family Reunion, etc..."/>
</div>
<div id="ContainInputDiv3-4">
<p class="level3InputP">Inside or Outside: </p>
<select id="InOrOut_Select" class="Select">
<option value="1">Inside</option>
<option value="2">Outside</option>
</select>
</div>
</div>
<div id="Level4_Form">
<p class="level4InputP">Requests/Specific Details: </p>
<center>
<textarea id="requests" rows="5" cols="100" style="resize: none;"/></textarea>
</center>
</div>
<center>
<div id="Level5_Form">
<script src="C:\Users\Connor\Desktop\Coding\Rick's Bar Service\Scripts\JS\Booking_Event.js"></script>
<input type="submit" id="Submit_Button" onclick="process()" value="Submit"/>
</div>
</center>
</form>
<?php
$path = "C:\\Users\\Connor\\Desktop\\Coding\\Rick's Bar Service\\JSON\\Request.json";
$myObj->FName = $_POST['fName'];
$myObj->LName = $_POST['lName'];
$myObj->EMail = $_POST['eMail'];
$myObj->PhoneNumber = $_POST['phoneNumber'];
$myObj->State = $_POST['State_Select'];
$myObj->Town = $_POST['town'];
$myObj->Zip = $_POST['zip'];
$myObj->Address = $_POST['address'];
$myObj->Month = $_POST['Month_Select'];
$myObj->Day = $_POST['Day_Select'];
$myObj->Year = $_POST['Year_Select'];
$myObj->NumOfPeople = $_POST['NumOfPeople'];
$myObj->EventType = $_POST['eventType'];
$myObj->InOrOut = $_POST['InOrOut_Select'];
$myObj->Requests = $_POST['requests'];
$myJSON = json_encode($myObj);
$myfile = fopen($path, "w");
fwrite($myfile, $myJSON);
fclose($myfile);
?>
错误:
Notice: Undefined index: fName in C:\xampp\htdocs\test.php on line 6
Notice: Undefined index: lName in C:\xampp\htdocs\test.php on line 7
Notice: Undefined index: eMail in C:\xampp\htdocs\test.php on line 8
Notice: Undefined index: phoneNumber in C:\xampp\htdocs\test.php on line 9
Notice: Undefined index: State_Select in C:\xampp\htdocs\test.php on line 10
Notice: Undefined index: town in C:\xampp\htdocs\test.php on line 11
Notice: Undefined index: zip in C:\xampp\htdocs\test.php on line 12
Notice: Undefined index: address in C:\xampp\htdocs\test.php on line 13
Notice: Undefined index: Month_Select in C:\xampp\htdocs\test.php on line 14
Notice: Undefined index: Day_Select in C:\xampp\htdocs\test.php on line 15
Notice: Undefined index: Year_Select in C:\xampp\htdocs\test.php on line 16
Notice: Undefined index: NumOfPeople in C:\xampp\htdocs\test.php on line 17
Notice: Undefined index: eventType in C:\xampp\htdocs\test.php on line 18
Notice: Undefined index: InOrOut_Select in C:\xampp\htdocs\test.php on line 19
Notice: Undefined index: requests in C:\xampp\htdocs\test.php on line 20
【问题讨论】:
将method="POST"
添加到您的<form>
元素中。默认方法是GET
。
我添加了 method="POST" 但仍然出现错误。我还尝试使用 GET 而不是 POST(我确保从表单中删除 method="POST" )并且仍然得到相同的答案。还有其他想法吗?
把 method="post" 放回去。您的所有输入都缺少 name 属性,即每个输入的 name="fName" 等... Name 属性是创建相应 $_POST[] 索引的原因。如果你执行了一个 var_dump($_POST) 来调试,你会发现它是空的......
【参考方案1】:
您应该首先检查是否确实发布了数据(表单提交与否),
// Check if there's ANY post data at all
// whether posted data
if (!empty($_POST))
// this is a way to get out of undefined index error
if( isset($_POST['fName']) )
$myObj->FName = $_POST['fName'];
else
// firstname doesn't exist
if( isset($_POST['lName']) )
$myObj->LName = $_POST['lName'];
// remaining goes here
形式上
<form id="myForm" action="test.php" method="POST">
【讨论】:
【参考方案2】:试试这个..
改变这一行
<form id="myForm" action="test.php">
到
<form id="myForm" method="POST" action="test.php">
然后从
开始<?php
$path = "C:\\Users\\Connor\\Desktop\\Coding\\Rick's Bar Service\\JSON\\Request.json";
$myObj->FName = $_POST['fName'];
$myObj->LName = $_POST['lName'];
$myObj->EMail = $_POST['eMail'];
$myObj->PhoneNumber = $_POST['phoneNumber'];
$myObj->State = $_POST['State_Select'];
$myObj->Town = $_POST['town'];
$myObj->Zip = $_POST['zip'];
$myObj->Address = $_POST['address'];
$myObj->Month = $_POST['Month_Select'];
$myObj->Day = $_POST['Day_Select'];
$myObj->Year = $_POST['Year_Select'];
$myObj->NumOfPeople = $_POST['NumOfPeople'];
$myObj->EventType = $_POST['eventType'];
$myObj->InOrOut = $_POST['InOrOut_Select'];
$myObj->Requests = $_POST['requests'];
$myJSON = json_encode($myObj);
$myfile = fopen($path, "w");
fwrite($myfile, $myJSON);
fclose($myfile);
到
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
$path = "C:\\Users\\Connor\\Desktop\\Coding\\Rick's Bar Service\\JSON\\Request.json";
$myObj->FName = $_POST['fName'];
$myObj->LName = $_POST['lName'];
$myObj->EMail = $_POST['eMail'];
$myObj->PhoneNumber = $_POST['phoneNumber'];
$myObj->State = $_POST['State_Select'];
$myObj->Town = $_POST['town'];
$myObj->Zip = $_POST['zip'];
$myObj->Address = $_POST['address'];
$myObj->Month = $_POST['Month_Select'];
$myObj->Day = $_POST['Day_Select'];
$myObj->Year = $_POST['Year_Select'];
$myObj->NumOfPeople = $_POST['NumOfPeople'];
$myObj->EventType = $_POST['eventType'];
$myObj->InOrOut = $_POST['InOrOut_Select'];
$myObj->Requests = $_POST['requests'];
$myJSON = json_encode($myObj);
$myfile = fopen($path, "w");
fwrite($myfile, $myJSON);
fclose($myfile);
谢谢你.. :)
【讨论】:
以上是关于我填写了表单上的所有值,我的 PHP 仍然返回未定义索引的主要内容,如果未能解决你的问题,请参考以下文章