在放置在同一页面中的表单之间传递值

编程入门 行业动态 更新时间:2024-10-26 22:25:52
本文介绍了在放置在同一页面中的表单之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在index.php中有2种形式.

in index.php there were 2 form .

<form method='post' action='res.php' name='form1' id='form1'> Enter Name<input type='text' name='CardName' > <input type='submit' name='submit'> </form>

. .

<form method='post' action='....' name='form2' id='form2'> <input type='hidden' name='CardName' id='CardName' value=''> <form>

在第二种形式中,我需要获取在第一种形式中提交的变量的值.两种形式都在同一页面中,&不允许使用session来做.首选使用jquery.请帮忙 .

in second form i need to get the value of variable that is submitted in the first form.. How to do that? The two forms are in same page,& not allowed to do that using session.Using jquery is preferred . Please help .

推荐答案

如果要在jquery中完成.这可能对您有帮助.

If you want it to be done in jquery. This may help you..

在第一个表单和字段中添加一个ID,并在按钮上添加一个onclick事件.

Add an id to the first form and the field and add an onclick event to the button.

<form method='post' action='res.php' name='form1' id="form1" onclick="submitform()"> Enter Name<input type='text' name='name' id='name'>

并使用jquery提交表单,如下所示:

and submit the form using jquery like this:

function submitform() { var name=$('#name').val(); $('#variable').val(name); // set the value of name field to the hidden field $('form#form1').submit(); // submit the form }

对于第二种形式,将id添加到隐藏变量中.

For second form add id to the hidden variable.

<form method='post' action='....' name='form2'> <input type='hidden' name='variable' id='variable' value=''> <form>

您可以正常提交第二份表格.

And you can submit the second form as normal.

在这种情况下,如果您重定向回index.php,则此方法将失败,因为一旦刷新/重新加载页面,则隐藏字段值将被重置.在这种情况下,最好通过res.php文件中的url传递值:

In the case, if you are redirecting back to index.php, this method will fail because the hidden field value will get reset once the page is refreshed/reloaded. In that case it is better to pass the value through url like this in res.php file:

$val=$_POST['name']; header('Location: index.php?val='.$val);

并在index.php中访问隐藏字段中的值,如:

and in index.php access the value in hidden field like:

<input type='hidden' name='variable' id='variable' value='<?php if(isset($_GET['val'])) echo $_GET['val']; ?> '>

更多推荐

在放置在同一页面中的表单之间传递值

本文发布于:2023-11-24 21:27:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   页面   在同一

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!