除非选中复选框,否则禁用表单提交

时间:2013-06-20 12:43:53

标签: php javascript html forms

我需要调整基本表单的代码。我不是PHP开发人员,我是平面设计师,我只掌握PHP的基础知识。我需要做的是禁止发送此表单,除非勾选复选框。我在网上找到了一些代码,我认为应该这样做,但它需要一些更改,我不确定调整哪部分代码。我查看了PHP,我知道有一大堆我不需要的代码,我看到还有一个JavaScript,我已经改变了HTML中的表单布局,只留下了所需要的东西。

以下是表单的外观:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php 
    session_start();
    if(isset($_POST['Submit'])) {
        $youremail = 'somemail@mail.com';
        $fromsubject = 'Request from form';
        $bedrijfsnaam = $_POST['bedrijfsnaam'];
        $naam = $_POST['naam'];
        $mail = $_POST['mail'];
        $adres = $_POST['adres']; 
        $plaatsnaam = $_POST['plaatsnaam']; 
        $postcode = $_POST['postcode']; 
        $branche = $_POST['branche']; 
        $telefoon = $_POST['telefoon']; 
        $message = $_POST['message']; 
    $to = $youremail; 
    $mailsubject = 'Bericht ontvangen van'.$fromsubject.' Actie Pagina';
    $body = $fromsubject.'

    Bedrijfsnaam:  '.$bedrijfsnaam.'
    Naam Contact Persoon:  '.$naam.'
    E-mail: '.$mail.'
    Adres: '.$adres.'
    Plaatsnaam: '.$plaatsnaam.'
    Postcode: '.$postcode.'
    Branche: '.$branche.'
    Telefoonnummer: '.$telefoon.'

    vraag of Wens: '.$message.'

    |---------End Message----------|'; 

        echo "thank you for your request we will contact you asap."; 
                            mail($to, $subject, $body);
    } 
    else
    { 
        echo "Error Please <a href='terms.php'>try again</a>"; 
    }

    /* if (isset($_POST['Submit'])) {
        $to = 'somemail@mail.com';
        $subject = 'Bericht ontvangen van'.$fromsubject.' Actie Pagina';
        $body = '';
        unset($_POST['Submit']);
        foreach($_POST as $key => $val) {
            $body .= ucfirst($key).": ".$val."\n";
        }
        if (mail($to, $subject, $body)) {
            echo "Mail sent to ".$to." successfully.";
        } else {
            echo "Mail could not be sent.";
        }
    }*/

?> 

</body>
</html>

这是HTML和Javascript:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>PHP form check box example</title>


<script type = "text/javascript">
//email form validation

function everif(str) {
    var at = "@"
    var punct = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var lpunct = str.indexOf(punct)

    if (str.indexOf(at) == -1)
    {
        alert("Valid email must be entered")
        return false
    }

    if (str.indexOf(at) == -1 || 
        str.indexOf(at) == 0  ||
        str.indexOf(at) == lstr) {
        alert("Valid email must be entered")
        return false
    }

    if (str.indexOf(punct) == -1 ||
        str.indexOf(punct) == 0  ||
        str.indexOf(punct) == lstr) {
        alert("Valid email must be entered")
        return false
    }

    if (str.indexOf(at,(lat+1)) != -1) {
        alert("Valid email must be entered")
        return false
    }

    if (str.substring(lat-1,lat) == punct ||
        str.substring(lat+1,lat+2) == punct) {
        alert("Valid email must be entered")
        return false
    }

    if (str.indexOf(punct,(lat+2)) == -1) {
        alert("Valid email must be entered")
        return false
    }

    if (str.indexOf(" ") != -1) {
        alert("Valid email must be entered")
        return false
    }
        return true         
}

function evalid() {
    var emailID = document.contact_form.mail

    if (everif(emailID.value) == false) {
        emailID.focus()
        return false
    }

    //empty field validation

var naam = document.contact_form.naam
    if ((naam.value == null) || (naam.value == "")) {
        alert("Fields marqued with * must be entered")
        naam.focus()
        return false
    }

var telefoon = document.contact_form.telefoon       
    if ((telefoon.value == null) || (telefoon.value == "")) {
        alert("Fields marqued with * must be entered")
        telefoon.focus()
        return false
    }

var branche = document.contact_form.branche
if ((branche.value == null) || (branche.value == "")) {
        alert("Fields marqued with * must be entered")
        branche.focus()
        return false
    }

    return true
}
</script>
<style type = "text/css">
    #content {
        width:100%;
        text-align:center;
        font-family:Arial, Helvetica, sans-serif;
        font-size:14px;      
}

    #button {
        width:100%;
    }       
</style>
</head>

<body>

<div id = "content">
   Some terms of trade goes here<br>
   Some terms of trade goes here<br>
   Some terms of trade goes here<br>
   Some terms of trade goes here<br>
   Some terms of trade goes here<br>
   Some terms of trade goes here<br>
   Some terms of trade goes here
</div>

<div id = "button">
    <form name = "contact_form" method = "post" 
      id = "contactform" action = "form2.php" onSubmit = "return evalid()">
    I accept the above terms of trade
        <input type = "checkbox" name = "emailmarketing"
          id = "emailmarketing" value = "emailmarketing" />
        <input type = "submit" name = "Submit" value = "Submit">
    </form>
</div>

</body>
</html>

提前感谢您的帮助!

4 个答案:

答案 0 :(得分:3)

编辑PHP的这一部分:

 vraag of Wens: 
 '.$message.'



|---------End Message----------|'; 

$check = $_POST['emailmarketing']; 
if ($check==false)
{
 echo "Error Please <a href='terms.php'>try again</a>"; 
}
else
{   
    echo "thank you for your request we will contact you asap."; 
    mail($to, $subject, $body);
}
?>

或者,如果您想通过javascript验证:

将此添加到evalid功能:

var checkBox=document.contact_form.emailmarketing

if (checkBox.value==false){
    alert("Please accept the terms");
    checkBox.focus()
    return false
}

答案 1 :(得分:2)

您必须检查复选框是否已选中。更改

<?php 
    session_start();
    if(isset($_POST['Submit'])) {
    ...

<?php

    if(!isset($_SESSION))session_start();

    if(isset($_POST['Submit']) && $_POST['emailmarketing'] == 'emailmarketing') {
    ...

答案 2 :(得分:0)

你必须在javascript函数evalid中再添加一个条件

if document.getElementById('emailmarketing').value==false {

    alert("you must accept terms of trade");
    return false;

}

提交表单时,会检查是否选中了该复选框。

答案 3 :(得分:0)

您可以使用jquery

停止用户提交表单(如果未选中)
if($('#your_checkbox_id').attr('checked')) {
       return true;
    }else{
    return false;
    }