我正在使用以下代码制作联系表格
<?php
$action = $_SERVER['PHP_SELF'];
$fname = $_POST['fname'];
$phone = $_POST['phone'];
$e_mail = $_POST['e_mail'];
$service = $_POST['service'];
$maker = $_POST['maker'];
$model = $_POST['model'];
$year = $_POST['year'];
$message = $_POST['message'];
$headers = 'From: me <reply@locallockman.com>';
$thankyou = "thankyou.html"; // thank you page
if(isset($_POST['fname']) && isset($_POST['phone']) && isset($_POST['e_mail']) && isset($_POST['service']) && isset($_POST['maker']) && isset($_POST['model']) && isset($_POST['year']) && isset($_POST['message'])){
$fname = $_POST['fname'];
$phone = $_POST['phone'];
$e_mail = $_POST['e_mail'];
$type = $_POST['service'];
$maker = $_POST['maker'];
$model = $_POST['model'];
$year = $_POST['year'];
$message = $_POST['message'];
if(empty($phone)){
echo 'Please fill all fields.';
}
else{
if(mail($to,$subject,$body,$headers)){
header('Location: thankyou.html');
}
else{
echo 'There was an error sending email/s.';
}
}
}
?>
它完美无缺,但我希望在用户选择其他选项时禁用文本字段。我正在使用javascript。这是代码
<script>
function findselected(){
var state = document.getElementById('#type');
var notus = document.getElementById('#model');
var year = document.getElementById('#year');
var maker = document.getElementById('#maker');
(state.value == "Automotive Service")? notus.disabled = false : notus.disabled = true;
(state.value == "Automotive Service")? year.disabled = false : year.disabled = true;
(state.value == "Automotive Service")? maker.disabled = false : maker.disabled = true;
}
</script>
这是html代码
<select name="service" id="type" onchange="findselected()">
<option value="Not Selected">Select Service Type</option>
<option value="Residential Service">Residential Service</option>
<option value="Commercial Service">Commercial Service</option>
<option value="Industrial Service">Industrial Service</option>
<option value="24/7 or Emergency Service">24/7 or Emergency Service</option>
<option value="Automotive Service">Automotive Service</option>
</select>
<select name="maker" id="maker" disabled="disabled" style="margin-left:36px;" autocomplete="on">
<option value="Not Selected" selected="selected" ><strong>Select Car Make</strong></option>
<option value="Acura" >Acura</option>
<option value="Audi">Audi</option>
<option value="BMW">BMW</option>
<option value="Buick">Buick</option>
<option value="Cadillac">Cadillac</option>
<option value="Chevrolet">Chevrolet</option>
<option value="Chrysler">Chrysler</option>
<option value="Daewoo">Daewoo</option>
<option value="Dodge">Dodge</option>
<option value="Fiat">Fiat</option>
<option value="Ford">Ford</option>
<option value="General Motors">General Motors</option>
<option value="Honda">Honda</option>
<option value="Hummer">Hummer</option>
<option value="Hyundai">Hyundai</option>
<option value="Isuzu">Isuzu</option>
<option value="Infinity">Infinity</option>
<option value="Jaguar">Jaguar</option>
<option value="Jeep">Jeep</option>
<option value="Kia">Kia</option>
<option value="Lexus">Lexus</option>
<option value="Lincoln">Lincoln</option>
<option value="Mercedes">Mercedes Benz</option>
<option value="Mazda">Mazda</option>
<option value="Mercury">Mercury</option>
<option value="Mini">Mini</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="NISSAN">NISSAN</option>
<option value="Plymouth">Plymouth</option>
<option value="Pontiac">Pontiac</option>
<option value="Porche">Porche</option>
<option value="Saab">Saab</option>
<option value="Saturn">Saturn</option>
<option value="Subaru">Subaru</option>
<option value="Suzuki">Suzuki</option>
<option value="Toyota">Toyota</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Volvo">Volvo</option>
</select>
<input type="text" name="model" id="model" style="margin-left:83px;" class="text-box" autocomplete="on" disabled="disabled" value=" "/><br /><br />
Model Year:
<input type="text" name="year" id="year" value=" " disabled="disabled" style="margin-left:73px;" class="text-box" autocomplete="on"/>
这是完整的代码。我希望当我选择任何其他选项,然后在标签中选择“汽车服务”时,应禁用名称为“年”,“模型”的字段和名称为“制造商”的列表框,并将空白值发送到电子邮件。 / p>
这适合我,但是当我运行它时,然后表单不起作用。当我选择汽车服务时,表单工作,文本字段'年'和'模型启用,表单工作。但是当我从字段中选择任何其他选项并且文本字段被禁用时,表单不起作用,我该如何修复它,请帮忙。
如果您有任何疑问,请与我们联系。
答案 0 :(得分:1)
你的JS不正确。你不能在#
中使用本地Javascript中的getElementById('#type')
(我想你可能会对jQuery感到困惑)
这是正确的代码,
function findselected(){
var state = document.getElementById('type');
var notus = document.getElementById('model');
var year = document.getElementById('year');
var maker = document.getElementById('maker');
notus.disabled = (state.value == "Automotive Service") ? false : true;
year.disabled = (state.value == "Automotive Service") ? false : true;
maker.disabled = (state.value == "Automotive Service") ? false : true;
}
演示:http://jsfiddle.net/F2PLF/1/
<强>更新强>
您可能必须在if
,$_POST['maker']
和$_POST['model']
的PHP代码中重写$_POST['year']
条件。当它们被禁用时,不会向服务器发送任何值。所以可以从if
条件中删除那些,如此
if(isset($_POST['fname']) && isset($_POST['phone']) && isset($_POST['e_mail']) && isset($_POST['service']) && isset($_POST['message'])){
或者您可能需要根据下拉if
<select>
条件
答案 1 :(得分:0)
当表单元素为disabled
时,它们将不会被发送,因此它们将无法在$_POST
中使用。所以这一行 -
if(isset($_POST['fname']) && isset($_POST['phone']) && isset($_POST['e_mail']) && isset($_POST['service']) && isset($_POST['maker']) && isset($_POST['model']) && isset($_POST['year']) && isset($_POST['message']))
会失败。
您可以使用read-only
,删除提交时的disabled
,也可以更改if(isset())...
请参阅 - http://www.w3.org/TR/html401/interact/forms.html#h-17.12
答案 2 :(得分:0)
获取$ _POST []数组this post can help
中的空值尝试在 js
中使用简单的if循环像:
function findselected(){
if(document.getElementById("type").value=="Automated Service")
{
notus.disabled = (state.value == "Automotive Service") ? false : true;
year.disabled = (state.value == "Automotive Service") ? false : true;
maker.disabled = (state.value == "Automotive Service") ? false : true;
}
}