我有一個表單,一旦從主下拉列表中進行選擇,它就會分成兩個不同的下拉列表。問題是,當我提交表單時,我需要從每個子下拉列表中進行選擇才能提交表單。如果未使用該變量,我該如何讓 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 = "[email protected]"; $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 關注
- 135 瀏覽
添加回答
舉報
0/150
提交
取消