简体   繁体   English

jQuery表单。 为什么要使用它?

[英]jQuery form. Why to use it?

I wanted to ask, why is it good or better to have a script print the form than plain html. 我想问一个问题,为什么让脚本打印表格比纯HTML更好还是更好? And what's the difference? 有什么区别? When I type: 当我输入:

<form><input type="button" value="button" /></form>

or 要么

<script type="text/javascript">
     $form = $("<form></form>");
     $form.append('<input type="button" value="button" />');
     $('body').append($form);
</script>

What if user disables javascript in his browser? 如果用户禁用浏览器中的javascript怎么办?

For security, is the plain html and php enough or should I use javascript to build and validate the registration form? 为了安全起见,普通的html和php是否足够,还是我应该使用javascript构建并验证注册表格?

I will start with your first question . 我将从你的第一个问题开始。
It's better to make forms in HTML , because in case of disabling the javascript your costumers will get problem using your application . 最好以HTML格式制作表单,因为如果禁用javascript,则客户将在使用应用程序时遇到问题。

Secons Question : Secons问题:
Better to use PHP validation , because the javascript can be fooled by a person with basic hacking skills . 最好使用PHP验证,因为javascript可能会被具有基本黑客技能的人所欺骗。 for exemple after disabling the JavaScript your validation script won't work . 例如,禁用JavaScript后,您的验证脚本将无法使用。

There is no need to generate it with Javascript, all you did was to make it much, much more hard to maintain. 无需使用Javascript生成它,您要做的就是使其变得更加难以维护。 When you need to change the form html, it is really annoying to deal with Javascript string literals. 当您需要更改html格式时,处理Javascript字符串文字确实很烦人

I use to skip form tags altogether, they are ugly, you can only have one action, and you cannot nest them. 我曾经完全跳过表单标签,它们很丑陋,您只能执行一个操作,而不能嵌套它们。 Instead I use ajax postings in for instance onclick handlers and reload the webpage if necessary in the success handlers of those: 取而代之的是,我在onclick处理程序中使用ajax发布,并在必要的成功处理程序中重新加载网页:

var data = {formvar1:3, formvar2: "foo"};
$.post('/foo/bar.php', data, function(webpage)
{
  location.reload();
});

or if the ajax handler returns json: 或者,如果ajax处理程序返回json:

var data = {formvar1:3, formvar2: "foo"};
$.post('/foo/bar.php', data, function(result)
{
  if (result.success)
  {
    alert("nice work!");
    location.reload();
  }
  else
  {
    alert(result.message);
  }
}, 'json');

Oh, my answer came a bit late so you are free to ignore it. 哦,我的回答来得有点晚,所以您可以自由地忽略它。 I never care about people disabling javascript because I think they are a myth btw :) 我从不关心禁用JavaScript的人,因为我认为这是一个神话:)

(It is worth mentioning that this stuff requires including jquery) (值得一提的是,这些东西需要包括jQuery)

So like i said this is one way to prevent bots from spamming you. 因此,就像我说的那样,这是防止漫游器向您发送垃圾邮件的一种方法。 It will stop most bots. 它将停止大多数机器人。 After implementing this I never had a problem with bots anymore 实施此功能后,我再也不会遇到机器人问题了

MySQL 的MySQL

CREATE TABLE IF NOT EXISTS `key_validation` (
  `key` varchar(64) NOT NULL,
  `date` varchar(32) NOT NULL,
  UNIQUE KEY `key` (`key`),
  KEY `date` (`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

PHP REST Web Service (getKey.php) PHP REST Web服务(getKey.php)

<?php
//file that creates a connection to the database
require "db_connect.php";


if($_GET['mode'] == "get_key") {
  $key = md5(microtime().rand());

  mysql_query("INSERT INTO key_validation (`key`,`date`) VALUES ('$key','".time()."')");

  echo json_encode($key);
  exit();
}

?>

JavaScript 的JavaScript

function insertKeyIntoForm(form) {
    $.getJSON('getKey.php?mode=get_key', function(data) {
        $('<input>').attr({
            type: 'hidden',
            id: 'verify_key',
            name: 'verify_key',
            value: data
        }).appendTo(form);
    });
}
insertKeyIntoForm('#myFormId');

PHP File the processes the registration PHP File进程注册

//TODO
//permanently block ip addresses that requests this service more than x number of times a minute

function func_query_first_cell($query) {
    if ($p_result = mysql_query($query)) {
        $result = mysql_fetch_row($p_result);

    }
    return $result[0];
}

      //to make sure is no spammer
  if(func_query_first_cell("SELECT `key` FROM key_validation WHERE `key`='$verify_key'") == "")
    exit();

  else
    db_query("DELETE FROM key_validation WHERE `key`='$verify_key'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM