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

时间:2022-08-06 10:02:54

in index.php there were 2 form .

在index.php中有2个表单。

<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>

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 .

在第二种形式,我需要获得以第一种形式提交的变量的值。如何做到这一点?这两个表单在同一页面中,并且不允许使用session进行。使用jquery是首选。请帮忙 。

3 个解决方案

#1


0  

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

如果你想在jquery中完成它。这可能对你有帮助..

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

将ID添加到第一个表单和字段,并向该按钮添加onclick事件。

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

and submit the form using jquery like this:

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

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
}

For second form add id to the hidden variable.

对于第二个表单,将id添加到隐藏变量。

<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.

你可以照常提交第二张表格。

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:

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

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

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

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

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

#2


0  

In the value attribute of your hidden input:

在隐藏输入的value属性中:

<?=$_POST["name"];?>

You likely want to only echo the value if isset returns true.

如果isset返回true,您可能只希望回显该值。

#3


0  

try this first place an id for form,then try below code

首先尝试使用表单的id,然后尝试下面的代码

$( "#form1" ).submit(function( event ) {
  var valForm1 = $(this).find('input').val();
  $( "#form2").find('input').val(valForm1 );
});

may be the get and set value is not right but i think the idea can work

可能是获取和设定值不对,但我认为这个想法可行

#1


0  

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

如果你想在jquery中完成它。这可能对你有帮助..

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

将ID添加到第一个表单和字段,并向该按钮添加onclick事件。

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

and submit the form using jquery like this:

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

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
}

For second form add id to the hidden variable.

对于第二个表单,将id添加到隐藏变量。

<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.

你可以照常提交第二张表格。

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:

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

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

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

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

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

#2


0  

In the value attribute of your hidden input:

在隐藏输入的value属性中:

<?=$_POST["name"];?>

You likely want to only echo the value if isset returns true.

如果isset返回true,您可能只希望回显该值。

#3


0  

try this first place an id for form,then try below code

首先尝试使用表单的id,然后尝试下面的代码

$( "#form1" ).submit(function( event ) {
  var valForm1 = $(this).find('input').val();
  $( "#form2").find('input').val(valForm1 );
});

may be the get and set value is not right but i think the idea can work

可能是获取和设定值不对,但我认为这个想法可行