我的javascript联系表单不起作用

时间:2013-01-08 08:17:38

标签: php javascript html

当从列表框中选择一个选项时,我需要使用javascript来取消字段。但是当字段被禁用时,表单不起作用。

我正在使用此PHP代码制作联系表单

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

现在如果不使用javascript这个表单有效。但是当我使用这个javascript表单时不起作用。

以下是表单的HTML代码:

<form action="form.php" method="post">
  Customer Name :
    <input type="text" name="fname" class="text-box" autocomplete="on" />

    Contact No. :
    <input type="number" name="phone" class="text-box" autocomplete="on"/><br /><br />
    E-mail:
    <input type="email" name="e_mail" class="text-box" autocomplete="on"/><br /><br />
    Service Type:
    <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>


    Car Make:
    <select name="maker" id="maker" 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>

    Car Model:
    <input type="text" name="model" id="model" class="text-box" autocomplete="on"  disabled="disabled" value=" "/>
    Model Year:
    <input type="text" name="year" id="year" value=" " disabled="disabled" class="text-box" autocomplete="on"/>

Service Required:
        <textarea name="message" class="text-area" autocomplete="on"></textarea>
        <br /><br />
        <input type="submit" value=" " class="submit"/>
      </p>
    </form>

当我从列表框中选择“汽车服务”以外的任何选项时,“Car-Make”,“Car Model”和“Model Year”字段将被禁用。但是当我运行表单时,它不起作用。

  1. 当我从列表框中选择“汽车服务”时,它确实有效。
  2. 但是当我选择其他选项时,字段被禁用但表单不起作用。

2 个答案:

答案 0 :(得分:2)

问题出在您的if

if(isset($_POST['fname']) && isset($_POST['phone']) && isset($_POST['e_mail'])...

如果字段被禁用,则不会发送数据,因此if将无法通过 (由于未设置$_POST['phone'],因此禁用相关字段)

您必须单独检查$_POST值。


我可以看到,你可以留下这些:

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

但这些值可以“未设置”:

$_POST['model']
$_POST['year']
$_POST['maker']

因此,请从$_POST中移除这3个if变量,如上例所示,然后替换:

$maker = $_POST['maker'];

$model = $_POST['model'];

$year = $_POST['year'];

使用:

$maker = $model = $year = 'Not Set'; // Or just use a empty string: '';
if(isset($_POST['maker'])){
    $maker = $_POST['maker'];
}
if(isset($_POST['model'])){
    $model = $_POST['model'];
}
if(isset($_POST['year'])){
    $year = $_POST['year'];
}

(从技术上讲,那些不是必需的,但如果你想在变量中使用默认的“Not set”值,它们会很好)

答案 1 :(得分:0)

我不确定你要做什么,但你的问题的第一个PHP部分可以大大简化:

<?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($fname && $phone && $e_mail && $service && $maker && $model && $year && $message){

    if(mail($to,$subject,$body,$headers)){
    header('Location: thankyou.html');

    }
    else{
        echo 'There was an error sending emails.';
    }

}

?>

PHP将空变量或空变量视为“false”,因此在将它们分配给局部变量后,不必再次检查$_POST变量。我不知道你在哪里创建$to$body,所以这是一个问题。