如何使用jquery检查数据库中的字段并显示检查或交叉?
我有一个页面,我询问他们邮政编码。当他们在写完 5 个邮政编码后立即填写表格时,它会检查它是否包含在我的数据库中,并在其附近显示一个勾号或十字符号,并禁用提交。
总结一下。
- 将等待访问者输入 5 位邮政编码(如果我们可以检查客户是否只输入数字,这将是一个加分和伟大)
- 它将检查它是否包含在数据库中(我不要求 php 部分。可能我们会将其作为 POST 发送到 php 文件)
- 如果它存在于数据库中,它将显示检查,否则它将显示交叉并且不允许提交表单。
我检查了一些网站,但找不到确切的解决方案。
谢谢
I have a page and I ask them zipcode. While they are filling the form right after they finish writing 5 numbers of zipcode, It will check if it is covered from my database and will show a check or cross sign near it and will disable submit.
To summarize.
- Will wait for visitor to type 5 digits zip code( If we can check if customer only enters number it will be a plus and great)
- It will check if it is covered in database ( I don't ask for php part. Probably we will send it as POST to a php file)
- If it exists in database it will show check else it will show cross and will not allow the form to be submitted.
I checked some websites but couldn't find an exact solution.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了邮政编码文本框之外,您可能还需要一个图像标签,并将
src
属性设置为不可见图像。然后对blur
事件执行 ajax。HTML:
Javascript:
对于数字验证,您可以使用相同的 PHP 脚本返回除“1”之外的另一个标志,并将其显示在另一个 span 元素中,表明输入的数据不是数字。也许只需在数据部分添加另一个键值对即可。
Probably you need have an image tag besides the zip code text box with the
src
attribute set to an invisible image. Then perform an ajax upon theblur
event.HTML:
Javascript:
For the numeric validation, you may use the same PHP script to return another flag besides "1" and display it in another span element that the data entered is not numeric. Just add another key-value pair in the data part, maybe.
您将需要使用 AJAX。 JQuery 具有内置的 AJAX 功能。在每个 keyup 事件中,您可以让它运行此 AJAX 函数。 PHP 应返回 1 或 0 值以使其变得简单。 1显然是匹配,0是不匹配。
您还需要添加清除搜索、检查是否为 null 或空字符串等功能,但这是应该让您继续前进的基础知识。我一直用这个。一旦你掌握了它的窍门,它就非常有用。然后考虑构建一个 jQuery 插件。一旦您可以完成上述功能,您就可以将其构建为插件(具有大量很酷的选项!)祝您好运,快乐编程。
You will need to use AJAX. JQuery has a built in AJAX function. On each keyup event, you can have it run this AJAX function. The PHP should return a value of either 1 or 0 to make it easy. 1 obviously is match, and 0 is no-match.
You will want to also add functionality on clearing the search, checking if null or empty string, etc., etc., but this is the basics that should get you going. I use this all the time. Once you get the hang of it, it's VERY useful. Then look into building a jQuery plugin. Once you can do the above functionality, you can build it into a plugin (with tons of cool options!) GOOD LUCK and happy programming.