Php 选择特定下拉选项时添加错误消息
好了,伙计们,我做了一些研究,但找不到能解决我问题的方法,尽管我确信这是一个简单的解决方法:) 我有一个简单的联系方式,第一行是下拉选择。此下拉列表确定将表单提交给哪个员工。我只想让我的默认选项“请选择类别”返回一条错误消息,以便提交者必须返回并选择其中一个选项以发送表单。如果未选择任何内容,则会在默认电子邮件中创建大量垃圾邮件 以下是代码的下拉位:Php 选择特定下拉选项时添加错误消息,php,forms,drop-down-menu,contact,Php,Forms,Drop Down Menu,Contact,好了,伙计们,我做了一些研究,但找不到能解决我问题的方法,尽管我确信这是一个简单的解决方法:) 我有一个简单的联系方式,第一行是下拉选择。此下拉列表确定将表单提交给哪个员工。我只想让我的默认选项“请选择类别”返回一条错误消息,以便提交者必须返回并选择其中一个选项以发送表单。如果未选择任何内容,则会在默认电子邮件中创建大量垃圾邮件 以下是代码的下拉位: <tr> <td><label for="sendTo"&g
<tr>
<td><label for="sendTo">Category (required):</label></td>
<td>
<select id="sendTo" name="sendTo">
<option id="pleaseSelectcategory1" value="pleaseSelectcategory1">Please Select Category</option>
<option id="aftermarketCustomerservice" value="aftermarketCustomerservice">Aftermarket Customer Service</option>
<option id="technicalAssistance" value="technicalAssistance">Technical Assistance</option>
<option id="aftermarketSales" value="aftermarketSales">Aftermarket Sales</option>
<option id="performanceProducts" value="performanceProducts">Performance Products & Sales</option>
<option id="oemSales" value="oemSales">OEM Sales</option>
<option id="exportSales" value="exportSales">Export Sales</option>
<option id="generalFeedback" value="generalFeedback">General Feedback</option>
</select>
</td>
</tr>
类别(必填):
请选择类别
售后服务
技术援助
售后服务
性能产品与销售
销售集团
出口销售
一般反馈
我只需要在我的html中输入什么,如果有的话,让这个错误消息出现在我的php文件中。提前谢谢 在纯JavaScript中,您可以使用以下函数:
function checkCategory(){
if(document.getElementById('sendTo').value === 'pleaseSelectcategory1') {
alert("You need to select category");
//This is to ensure that the form doesn't get submitted.
return false;
}
}
<form name="form" id="form" action="" onsubmit="return validateForm()" method="POST">
在按钮上使用onclick=“javascript:checkCategory();”
,或者表单本身使用onsubmit=“javascript:checkCategory();”
在PHP中,您只需使用:
if($_POST['sendTo'] == "pleaseSelectcategory1")
{
//However you want to handle the the fact the user selected the option
}
在发布数据之前,应该使用Javascript函数验证表单。这对UX更好,甚至可以防止表单到达PHP post函数 在表单中,让
onsubmit
字段调用Javascript函数:
function checkCategory(){
if(document.getElementById('sendTo').value === 'pleaseSelectcategory1') {
alert("You need to select category");
//This is to ensure that the form doesn't get submitted.
return false;
}
}
<form name="form" id="form" action="" onsubmit="return validateForm()" method="POST">
一旦表单在PHP中发布,您还可以使用以下工具验证表单:
if ($_POST['sendTo'] === 'pleaseSelectcategory1') {
// Redirect back to form or do whatever
}
if($\u POST['sendTo']==“plesselectioncategory1”){/}
使用Javascript处理此问题将避免无用的服务器交换。只需谷歌“javascript表单验证”。(这并不意味着以后在服务器端重复检查是不好的)@Fred ii-这似乎会停止电子邮件,但我如何提醒人们选择该类别以正确发送?@ClémentMalet函数validateForm(){if(document.contactForm.sendTo.value==“plesselectionCategory1”){alert(“请选择一个类别”);document.contacctForm.sendTo.focus();return false;}}}-添加了此位,没有任何内容happens@KJonesif($\u POST['sendTo']==“请选择类别1”){echo“选择类别”;退出;}
如果你愿意,你也可以在那里添加链接。但是混合使用JS会很好。你也可以研究Ajax。我添加了所有这些位,表单仍然发送,其中“请选择类别”字段被选中并在我的邮件中着陆。我遗漏了一些东西:|通过放入警报来检查是否调用了Javascript函数(document.getElementById(“sendTo”).value);
位于validateForm()
中if语句的正上方,查看是否1.正在调用函数,2.正确检索selectbox中的值。