我有一个表单,一旦从主下拉列表中进行选择,它就会分成两个不同的下拉列表。问题是,当我提交表单时,我需要从每个子下拉列表中进行选择才能提交表单。如果未使用该变量,我该如何让 PHP 代码忽略该变量?例如,我希望能够选择“击剑”>“colorbond”。但我还需要从内部民事>任何中进行选择。为了能够提交表单。<?php if(isset($_POST['submit'])){ $firstName = $_POST['first-name']; $lastName = $_POST['last-name']; $mailFrom = $_POST['email']; $tel = $_POST['tel']; $streetAddress = $_POST['street-address']; $suburb = $_POST['suburb']; $jobType = $_POST['job-type']; $subJobTypeCivil = $_POST['internal-civil']; $subJobTypeFencing = $_POST['fencing-job']; $subject = "$jobType $firstName $lastName $suburb"; echo ''.$jobType; $approximateMeterage = $_POST['approx-meterage']; $message = $_POST['message'];// workout how to files to upload//////////////// $mailTo = "name@gmail.com"; $headers = ("WEBQ $jobType $firstName $lastName $suburb"); $txt = "New ".$jobType." enquiry. \n\n"."Customer Name: ".$firstName." ".$lastName."\n"."Email: ".$mailFrom."\n"."Phone: ".$tel."\n"."Address: ".$streetAddress.", ".$suburb."\n"."Job Type: ".$jobType."\n". "Civil Job Type: ". $subJobTypeCivil."\n"."Fencing JobType:" . $subJobTypeFencing . "\n"."Approximate Meterage: ".$approximateMeterage."\n\n"."Message: ".$message; mail($mailTo, $subject, $txt, $headers); header("Location: index.html"); }?>function navHamburger() { var x = document.getElementById("nav-links"); if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; }}// CONTACT US FORM ////////////////////////////////////////////////////////////////////$("#job-type").change(function(){ if ($(this).val() == "internals-civil") { $('#telecom-internal-civil').show(); $('#internal-civil').attr('required', ''); $('#internal-civil').attr('data-error', 'This felid is required'); } else { $('#telecom-internal-civil').hide(); $('#internal-civil').removeAttr('required'); $('#internal-civil').removeAttr('data-error'); }});
1 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
大多数字段都具有该required
属性,这使得表单在这些字段完成之前不会提交:
<select name="job-type" id="job-type" required>
要更改此设置,只需删除该required
属性即可提交表单。
该required
属性是使字段成为必填字段的简单方法,因为它使用本机浏览器消息和警报。
对于您的 PHP 代码,您可以添加一些条件和默认值。一种方法是使用关键字,但这里empty
还有更多示例。
$jobType = ''
if (!empty($_POST['job-type'])) {
$jobType = $_POST['job-type'];
}
;
- 1 回答
- 0 关注
- 106 浏览
添加回答
举报
0/150
提交
取消