简体   繁体   English

HTML表单调用后隐藏字段

[英]HTML form post call hidden field

I am trying to do a post call for the login page. 我正在尝试对登录页面进行后期处理。 User manually enters username/password. 用户手动输入用户名/密码。 However, I want the below field to be set name=username when user inputs his/her username. 但是,我希望在用户输入用户名时将以下字段设置为name = username。

I have these fields below. 我在下面有这些字段。 And I have a hidden field and would like to replace the username bit with the username that user manually enters. 我有一个隐藏字段,想用用户手动输入的用户名替换用户名。

<input class="form-control" type="hidden" required name="username" placeholder="Username">
<input class="form-control" type="password" required name="password" placeholder="Password">

<input class="form-control" type="hidden" required name="arg" placeholder="name">

So it has to be 所以必须

username=stack
password=userset
arg='name=stack'

If I'm understanding correctly what you want to do, it sounds like you want to get the input DOM object and change its placeholder attribute every time the user changes the value in the username text input box. 如果我正确理解了您想做什么,这听起来像是您希望在用户每次更改用户名文本输入框中的值时获取input DOM对象并更改其占位符属性。

You can grab the DOM elements using getElementsByName('NAME')[0] (assuming the names are unique, or even better use IDs instead to make sure they are unique). 您可以使用getElementsByName('NAME')[0]来获取DOM元素(假设名称是唯一的,或者甚至最好使用ID来确保它们是唯一的)。

Then attach a function to listen for changes and swap in placeholder 's new value using inputDomObject.addEventListener("change", functionToHandleChanges); 然后附加一个函数以侦听更改,并使用inputDomObject.addEventListener("change", functionToHandleChanges);交换placeholder的新值inputDomObject.addEventListener("change", functionToHandleChanges);

You can set the placeholder value on the input's DOM object (and therefore on the page) using placeholderDomObject.setAttribute("placeholder", "thingToSetItTo"); 您可以使用placeholderDomObject.setAttribute("placeholder", "thingToSetItTo");在输入的DOM对象上(因此在页面上)设置placeholderplaceholderDomObject.setAttribute("placeholder", "thingToSetItTo");

If you need any clarification just leave a comment :) 如果您需要任何澄清,请发表评论:)

You could try using jQuery. 您可以尝试使用jQuery。 Here I have an example where the user still hasn't submited the form. 在这里,我有一个示例,其中用户仍未提交表单。

$(document).ready(function() {

    $("input[name='username']").keyup(function() {
         $("input[name='arg']").val("'name=" + $(this).val() + "'");
    });

});

If you really want to get $_POST values, you have to serialize them using some php. 如果您真的想获取$ _POST值,则必须使用一些php将它们序列化。

<script type="text/javascript">
    var $post = <?php echo json_encode($_POST); ?>;
</script>

Then you can access your variable using $post["username"]. 然后,您可以使用$ post [“ username”]访问变量。 But i don't see why you would use this solution, since you can just use regular php to do this. 但我不明白为什么要使用此解决方案,因为您可以使用常规php来做到这一点。

In your code this should look like this : 在您的代码中,该代码应如下所示:

<script type="text/javascript">

     $(document).ready(function() {
        $('form#form1').submit(function(){
            $.ajax({type: "Post",
                    url: $('form').attr('action'),
                    data: $(this).serialize(),
                    success: function(data, status, xhr) {
                        alert("Login successful. Please press OK to be redirected. Redirecting in 5 seconds.");
                        window.setTimeout(function(){window.location.href = "/login"},5000);},
                        error: function(data, status, xhr) {
                            $('form').trigger("reset");
                            alert("Failed to login. Please try again.");
                        }
                    });

                return false;
                alert("AJAX request successfully completed");
            });



          $("input[name='username']").keyup(function() {
              $("input[name='arg']").val("'name=" + $(this).val() + "'");
          });
     });

</script>

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

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