2013-06-20 69 views
1

我需要调整基本表单的代码。我不是一名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 = '[email protected]'; 
     $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 = '[email protected]'; 
     $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> 

谢谢!

+0

提交表单(如果未选中)我想从任何不必要的行剥离的代码,我只需要接受复选框,从我看到有是更多的东西,如名称,地址等。我应该在PHP中留下什么行? – user2486863

回答

2

编辑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 
} 
+0

JavaScript关闭的人仍然可以提交表单。 –

+0

@LightningDust我编辑了答案。 –

+0

感谢micro.pravi,工作很好,但我现在得到这个消息“谢谢你的请求,我们会尽快与你联系”两次,当表单被发送时。 – user2486863

2

您必须检查chekbox是否被检查。

变化

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

<?php 

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

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

我应该在哪里粘贴此代码? – user2486863

+0

更新了答案 – zkanoca

0

你将不得不增加一个条件到javascript函数evalid是

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

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

} 

虽然提交它会检查该复选框的形式检查或不。

0

您可以停止用户通过使用jquery

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