簡體   English   中英

除非選中復選框,否則禁用表單提交

[英]Disable form submit unless checkbox is ticked

我需要調整基本形式的代碼。 我不是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>

在此先感謝您的幫助!

編輯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
}

您必須檢查復選框是否已選中。

更改

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

<?php

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

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

您將不得不在javascript函數evalid中添加另一個條件,即

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

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

}

提交表單時,它將檢查該復選框是否已選中。

您可以使用jquery停止用戶提交表單(如果未選中)

if($('#your_checkbox_id').attr('checked')) {
       return true;
    }else{
    return false;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM