Sahibjot Singh
Sahibjot Singh

Reputation: 1391

My javascript contact form doesn't work

I need to use javascript to disbale fields when an option is select from list box. But when the fields are disabled, form does not work.

I am making a contact form with this PHP 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.';
        }
    }

}

?>

This is the javascript that I am using to disable and enable the fields.

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

Now if don't use javascript this form works. But when i use this javascript form does not work.

Here is the HTML code of the form:

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

When I select any option other than "Automotive Service" from the list box, the fields "Car-Make", "Car Model" and "Model Year" gets disabled. But when I run the form, it does not work.

  1. When I choose "Automotive Service" from list box, it does work.
  2. But when I choose other options, fields are disabled but form does not work.

Upvotes: 0

Views: 353

Answers (2)

Tieson T.
Tieson T.

Reputation: 21246

I'm not sure exactly what you're trying to do, but the first PHP part of your question can be greatly simplified:

<?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 treats empty or null variables as "false" so you don't have to check the $_POST variables again, after you assign them to your local variables. I don't see where you create $to or $body, though, so that's a problem.

Upvotes: 0

Cerbrus
Cerbrus

Reputation: 72967

The problem is in your if:

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

If the fields are disabled, the data will not be sent, so the if will not pass.
(Since $_POST['phone'] is not set, if the related field is disabled)

You'll have to check for the $_POST values individually.


As fas as I can see, you can leave these be:

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

But these values can be "not set":

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

So, remove those 3 $_POST variables out of your if, like the example above, then replace:

$maker = $_POST['maker'];

$model = $_POST['model'];

$year = $_POST['year'];

With:

$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'];
}

(Technically, those if's aren't necessary, but they're nice if you want a default "Not set" value in the variables)

Upvotes: 2

Related Questions