-1

我正在使用以下代码制作联系表格

<?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"/>

这是完整的代码。我希望当我在标签中选择任何其他选项然后“汽车服务”时,名称为“年份”、“型号”的字段和名称为“制造商”的列表框应该被禁用,并且应该将空白值发送到电子邮件。

这对我有用,但是当我运行它时,表单不起作用。当我选择汽车服务时,表单工作和文本字段“年份”和“模型”被启用并且表单工作。但是当我从该字段中选择任何其他选项并且文本字段被禁用时,表单不起作用,我该如何解决它,请帮忙。

请让我知道,如果你有任何问题。

4

3 回答 3

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在您的 php 代码中为 和 重写条件$_POST['maker']$_POST['model']$_POST['year']它们被禁用时,不会向服务器发送任何值。所以可以从if条件中删除那些,像这样

if(isset($_POST['fname']) && isset($_POST['phone']) && isset($_POST['e_mail']) && isset($_POST['service']) && isset($_POST['message'])){

if或者您可能需要根据下拉列表中的选定值重新编写您的条件<select>

于 2013-01-08T06:51:52.400 回答
0

要获取 $_POST[] 数组中的空值,这篇文章可以提供帮助

尝试在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;
    }
}
于 2013-01-08T07:05:09.003 回答
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、删除disabledon submit 或更改您的if(isset())...

见 - http://www.w3.org/TR/html401/interact/forms.html#h-17.12

于 2013-01-08T06:52:20.900 回答