我是PHP新手并尝试使用简单的表单并能够通过电子邮件发送给管理员。我之前已经开始使用PHP表单了,但由于某种原因,我遇到了很多困难。基本上,当我按下提交按钮时,我得到了这个垃圾: 。我正在使用WAMP服务器来完成此任务,不确定iit是否与错误有关,但我不相信它。
这是我在主索引页面上的脚本,就在关闭html
之前 function validate(){
var name=document.getElementById('employee');
var name=document.getElementById('start-date');
var name=document.getElementById('end-date');
var name=document.getElementById('location');
var name=document.getElementById('company');
var name=document.getElementById('perdiem');
var name=document.getElementById('flight');
var name=document.getElementById('hotel');
var name=document.getElementById('rental');
var name=document.getElementById('depart');
var name=document.getElementById('city-venue');
var name=document.getElementById('submit');
if(employee.value == ''){
alert('Please enter employee name');
return false;
}
if(start-date.value == ''){
alert('Please enter start date');
return false;
}
if(end-date.value == ''){
alert('Please enter end date');
return false;
}
if(location.value == ''){
alert('Please enter location');
return false;
}
if(company.value == ''){
alert('Please enter employee company');
return false;
}
if(perdiem.value == ''){
alert('Please enter perdiem value');
return false;
}
if(depart.value == ''){
alert('Please enter departure details');
return false;
}
if(city-venue.value == ''){
alert('Please enter City-Venue');
return false;
}
}
以下是表格:
<div id="form">
<form method="get" name="contactform" action="handler.php" onSubmit="return validate()">
<h1>Employee to Request</h1>
<select id="employee">
<option value="emp1">sample1</option>
<option value="emp2">sample2</option>
</select><br>
<h1>Start Date:</h1> <input type="date" name="start-date"><br>
<h1>End Date:</h1> <input type="date" name="end-date"><br>
<h1>Location</h1>
<select id="location">
<option value="chicago">Chicago</option>
<option value="florida">Florida</option>
<option value="las-vegas">Las Vegas</option>
<option value="new-england">New England</option>
<option value="new-orleans">New Orleans</option>
<option value="new-york">New York</option>
<option value="northern-california">Northern California</option>
<option value="seattle">Seattle</option>
<option value="southern-california">Southern California</option>
<option value="washington-dc">Washington DC / Baltimore</option>
</select>
<h1>Company Name</h1>
<select id="company">
<option value="cmp1">cmp1</option>
<option value="cmp2">cmp2</option>
<option value="cmp3">cmp3</option>
</select>
<h1>Perdiem</h1>
<select id="perdiem">
<option value="35">$35</option>
<option value="45">$45</option>
<option value="50">$50</option>
</select>
<h1>Hotel</h1>
<select id="hotel">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<h1>Flight</h1>
<select id="flight">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<h1>Rental Car</h1>
<select id="rental">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<h1>Departing</h1>
<input type="text" name="depart" onClick="this.value='';" onFocus="this.select()" onBlur="this.value=!this.value?'Name: ':this.value;" value="Departing: " placeholder="Depart"><br>
<h1>City / Venue</h1>
<select id="city-venue">
<option value="city1">Las Vegas / Venue1</option>
<option value="city2">Orlando / Venue2</option>
<option value="city3">sample</option>
<option value="city4">sample2</option>
<option value="city5">sample3</option>
</select>
<br>
<h1>Other Information:</h1>
<textarea class="message" placeholder="Other Information:"type="text" name="message" onClick="this.value='';" onFocus="this.select()" onBlur="this.value=!this.value?'Message: ':this.value;" value="Message: "></textarea><br>
<input type="submit" value="Submit" id="submit">
</form>
</div> <!--close form-->
这里是handler.php文件
<?php
$employee = $_GET['employee'];
$employee = $_GET['start-date'];
$employee = $_GET['end-date'];
$employee = $_GET['location'];
$employee = $_GET['company'];
$employee = $_GET['perdiem'];
$employee = $_GET['flight'];
$employee = $_GET['hotel'];
$employee = $_GET['rental'];
$employee = $_GET['depart'];
$employee = $_GET['city-venue'];
echo "Welcome".$employee;
echo "</br>"
?>
提前致谢,我真的很感激
答案 0 :(得分:2)
这种情况正在发生,因为php从输入名称读取值而不是输入id,因此索引未定义,我会在输入中添加名称,例如
<select id="employee">
将是
<select id="employee" name="employee">
您必须添加所有输入字段
答案 1 :(得分:0)
使用isset()函数
试试这个
if(isset($_GET['employee']))
{
$employee = $_GET['employee'];
}
并且是将name属性放在所有输入中
答案 2 :(得分:0)
这与PHP错误无关,但JavaScript充满了问题
COPY PASTE ERROR
var name=document.getElementById('employee');
var name=document.getElementById('start-date');
var name=document.getElementById('end-date');
var name=document.getElementById('location');
var name=document.getElementById('company');
var name=document.getElementById('perdiem');
var name=document.getElementById('flight');
var name=document.getElementById('hotel');
var name=document.getElementById('rental');
var name=document.getElementById('depart');
var name=document.getElementById('city-venue');
var name=document.getElementById('submit');
无效的变量名称
city-venue.value
这就是说变量city
并减去venue.value
现在PHP错误 - 表单问题
您没有向serber提交姓名/价值对。表单的问题是元素只有id而没有名称。您的所有表单元素都需要名称。这就是提交给服务器的内容。