需要帮助完成联系表格 [英] Need Help in completing the contact form

查看:111
本文介绍了需要帮助完成联系表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建联系表单

 <?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; ($ _ POST ['fname'])&& isset($ _ POST ['phone'])&& isset($ _ POST ['e_mail'])& ; $ _ 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(空($ phone)){
echo'请填写所有字段。';
}
else {
if(mail($ to,$ subject,$ body,$ headers)){
header('Location:thankyou.html');

}
else {
echo'发送邮件时发生错误。';
}
}

}

?>

它可以很好地工作,但我希望在用户选择另一个选项时禁用文本字段。这是代码

 < script> 
函数findselected(){
var state = document.getElementById('#type');
var notus = document.getElementById('#model');
var year = document.getElementById('#year');
var maker = document.getElementById('#maker');
(state.value ==汽车服务)? notus.disabled = false:notus.disabled = true;
(state.value ==汽车服务)? year.disabled = false:year.disabled = true;
(state.value ==汽车服务)? maker.disabled = false:maker.disabled = true;
}
< / script>

这里是html代码

 < select name =serviceid =typeonchange =findselected()> 
< option value =Not Selected>选择服务类型< / option>
< option value =住宅服务>住宅服务< / option>
< option value =商业服务>商业服务< / option>
< option value =工业服务>工业服务< / option>
< option value =24/7或紧急服务> 24/7或紧急服务< / option>
< option value =汽车服务>汽车服务< / option>
< / select>

以下是完整的代码。我希望当我选择任何其他选项,然后在标签中使用Automotive Service时,应禁用名称为year,model和名称为maker的列表框,并将空白值发送至电子邮件。 / p>

这适用于我,但是当我运行它时,但表单不起作用。当我选择汽车服务时,表单起作用,文本字段年和模型启用并形成工作。但是,当我从字段中选择任何其他选项并禁用文本字段时,表单不起作用,我该如何解决这个问题,请帮助。



请让我知道您是否有任何问题。 解决方案

你的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 ==汽车服务)?假:真;
year.disabled =(state.value ==汽车服务)?假:真;
maker.disabled =(state.value ==汽车服务)?假:真;
}

演示: http://jsfiddle.net/F2PLF/1/



更新:
如果 $ _ POST ['maker'] php代码中的条件不得不重写 $ _ POST ['model'] $ _ POST ['year'] 将被发送到服务器。所以既可以从中删除​​那些if 条件,就像这样


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



或者您可能需要重新编写> 条件基于从下拉列表中选择的值< select>


I am making a contact form with the following code

<?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.';
        }
    }

}

?>

It works perfectly, but i want text field to be disabled when user selects another option. I am using javascript for that.This is the code

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

Here is the html code

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

Here is the complete code. I want when i select Any other option then "Automotive Service" in tag, the fields with name "year", "model" and list box with name "maker" should be disabled and blank values should be sent to e-mail.

This works for me but when I run it, but then form does not work. When I choose Automotive Service the form works and text fields 'year' and 'model gets enabled and form works. But when i choose any other option from the field and the text fields gets disabled, then form does not work, how can I fix it, please help.

Please let me know if you have any questions.

解决方案

Your JS was not correct. You cannot use # in getElementById('#type') on native Javascript (I think you might be confused with jQuery)

Here is the correct code,

  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;
  }

Demo: http://jsfiddle.net/F2PLF/1/

Update: You may have to rewrite the if condition in your php code for $_POST['maker'], $_POST['model'] and $_POST['year'].When they are disabled, no value will be sent to server. so can either remove those from the if condition, like this

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

Or you may have to re-write your if condition based on the selected values from the drop-down <select>

这篇关于需要帮助完成联系表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆