在放置在同一页面中的表单之间传递值 [英] passing values between forms that are placed in same page

查看:79
本文介绍了在放置在同一页面中的表单之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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='https://....' 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='https://....' 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']; ?> '>

这篇关于在放置在同一页面中的表单之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆