简体   繁体   English

jQuery表单提交丢失的表单数据

[英]Jquery form submit losing form data

I have this form which is supposed to send the info to the database for a username search. 我有这种形式,应该将信息发送到数据库以进行用户名搜索。 I'm putting together a search function for private servers in our student dormitory. 我正在为学生宿舍中的私人服务器提供搜索功能。 We have a dude with username <lol> and it seems we can't search him. 我们有一个使用用户名<lol>的家伙,看来我们无法搜索他。 Other usernames search just fine. 其他用户名搜索就可以了。 I'm I missing something? 我想念什么吗?

This is the form.php file: 这是form.php文件:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.js"></script>
<script src="//cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.validation/1.13.1/additional-methods.js"></script>
<script>

  $(document).ready(function() {
    $('#checkForm').validate({
    rules: {
    IpSearch: {
    ipv4:true,
    require_from_group: [1, ".required_group"]
    },
    UserName: {
    minlength: 3,
    require_from_group: [1, ".required_group"]
    }
    },
    submitHandler: function(form) {
    console.log( $("#checkForm").serialize());
    $('#results').text( $("#checkForm").serialize());
    $.post('file.php', $("#checkForm").serialize(), function(data) {
    $('#checkResult').html(data);

    });
    }
    });
   });
</script>
</head>
<body>


                    <form role="form" id="checkForm" name="checkForm" method="post">
                       <div class="form-group">
                          <label>Internal IP:</label>
                          <input id="IpSearch" name="IpSearch" placeholder="Enter IP" class="form-control optional required_group" type="text">
                       </div>
                       <div class="form-group">
                          <label>Username</label>
                          <input id="UserName" name="UserName" value="<?php if(!empty ($_POST) && isset($_POST['checkThis'])){echo $_POST['checkThis']; } else{}?>" placeholder="admin" class="form-control optional defaultInvalid required_group" type="text">
                       </div>
                       <button id="search" name="search" value="doSearch" class="btn btn-info">Search</button>
                    </form>


<div id="results"></div>
<div id="checkResult"></div>

</body>
</html>

and this is the file.php beginning: 这是file.php的开头:

<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";
// .... continues but irrelevant since I'm not getting any post data

On clicking the button, I get this in browser console: 单击按钮后,我将在浏览器控制台中看到以下内容:

"IpSearch=&UserName=%3Clol%3E&search=doSearch"

on #results I get: IpSearch=&UserName=%3Clol%3E&search=doSearch 关于#results我得到:IpSearch =&UserName =%3Clol%3E&search = doSearch

on #checkResult I get: 在#checkResult上,我得到:

array(3) {
  ["IpSearch"]=>
  string(0) ""
  ["UserName"]=>
  string(5) ""
  ["search"]=>
  string(8) "doSearch"

} }

Try vardumping $_GET 尝试输入$ _GET
var_dump($_GET)
And post results here :) 并在这里发布结果:)

Looks like your script doesn't prevent default action and form gets submitted anyway 看起来您的脚本不会阻止默认操作,并且无论如何都会提交表单

it doesn't show <lol> because it considered HTML tag when jquery print it. 它不显示<lol>因为它在jquery打印时考虑了HTML标记。

you must escape html entities before printing it. 您必须先将HTML实体转义,然后再打印。

so change this line: $('#checkResult').html(data); 因此更改此行: $('#checkResult').html(data); to $('#checkResult').text(data); $('#checkResult').text(data);

so this will print <lol> as text not html 所以这会将<lol>打印为文本而不是html

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

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