简体   繁体   English

提交后剩余的表单输入值

[英]form input value remaining after submit

now i have a input text, radio, and a submit button .. 现在我有输入文字,单选按钮和提交按钮..

lets say my url = image-search.php 可以说我的url = image-search.php

<form>
<input type="text" name="name"><br>
<input type="radio name="arrange" value="horizontal"><br />
<input type="radio name="arrange" value="vertical"><br>
<input type="submit" name="submit"><br>

when i click the button.. it redirect same page but url = image-search.php?name=ss&arrange=horizontal 当我单击按钮时..它将重定向同一页面,但url = image-search.php?name=ss&arrange=horizontal

and this page still have the button.. 并且此页面上仍然有按钮。

the question is .. after i click button at 1st page = image-search.php 问题是..我单击第一页上的按钮后= image-search.php

i want the user input value remain in the input text of name.. 我希望用户输入值保留在名称输入文本中。

and how to make the checkbox as checked based on user choose? 以及如何根据用户选择使复选框处于选中状态?

If the page is reloaded when you submit the form you could use php to set default values for your form fields 如果在提交表单时重新加载页面,则可以使用php设置表单字段的默认值

<form>
<input type="text" name="name" value="<?php echo isset($_GET["name"])?$_GET["name"]:""; ?>"><br>
<input type="radio" name="arrange" value="horizontal"<?php echo (isset($_GET["arrange"])?($_GET["arrange"]=="horizontal"?" checked='checked'":""):""); ?>><br />
<input type="radio" name="arrange" value="vertical"<?php echo (isset($_GET["arrange"])?($_GET["arrange"]=="vertical"?" checked='checked'":""):""); ?>><br>
<input type="submit" name="submit"><br>
</form>

Here is the answer to your question.. hope this will help everyone.. @Macke - your approach for setting values after submission is really good, but when we have lot of elements on form.. let's say 1000 - it become pain in AS*.. 这是您问题的答案..希望这会对大家有所帮助.. @Macke-您提交后设置值的方法确实很好,但是当表单上有很多元素时..假设为1000-在AS中会变得痛苦* ..

Add this script tag in your HEAD tag of the page - 将此脚本标签添加到页面的HEAD标签中-

<script language="javascript" type="text/javascript">
    var obj = JSON.parse('<?= json_encode($_REQUEST) ?>');
    console.log(obj);

    function __setPostBackValue(element){
        if(obj.length <= 0) return;
        var type = element.type;
        var fval;
        console.log('Processing...'+ element.name);
        try{
            eval('fval = obj'+'.'+element.name);
        }
        catch(ex){

        }
        if(type == 'text'){
            element.value = fval; 
        }
        if(type == 'checkbox'){                    
            if(fval != undefined)
                element.setAttribute("checked","on");                        
        }
        if(type == 'radio'){                    
            if(fval != undefined && element.value == fval)
                element.setAttribute("checked","on");                        
        }

    }

</script>

and at the bottom of the page, yes at the bottom of the page (before body ends) add another script tag - 在页面底部,是,在页面底部(在正文结束之前)添加另一个脚本标签-

<script language="javascript" type="text/javascript">
            var fields = document.getElementsByTagName('input');
            for(var i=0;i<fields.length;i++){
                __setPostBackValue(fields[i]);
            }
        </script> 

What it does ? 它能做什么 ? When you submit your form, var obj = JSON.parse('<?= json_encode($_REQUEST) ?>'); 提交表单时, var obj = JSON.parse('<?= json_encode($_REQUEST) ?>'); this creates local JSON Object usable by Javascript - and the script we added at the end of the page.. loop through all elements and call __setPostBackValue function. 这将创建可被Javascript使用的本地JSON对象-以及我们在页面末尾添加的脚本。.循环遍历所有元素并调用__setPostBackValue函数。 Where we are setting the values of the elements by Javascript. 我们通过Javascript设置元素的值的位置。

This is little bit tricky but it works..!! 这有点棘手,但是可以用.. !!

PS: I had no radio button in my page, but if you have you can add it easily. PS:页面上没有单选按钮,但是如果您有,可以轻松添加。

-Paresh Rathod -Paresh Rathod

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

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