• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

passing hidden field values via URL

Hi

I have a PHP form with a value stored in a hidden field.  I want to pass this value to another page via the url when a sumbit button is clicked - what is the easiest way of doing this??

Cheers

Ken
0
kenabbott
Asked:
kenabbott
2 Solutions
 
jericotolentinoCommented:
Hi,

Will it be all right if the values are displayed on the address bar? If it is, just type something like:

<a href="page1.php?field1=<? echo $field1Value; ?>">New page with value inserted</a>

Then in the next page just call the value using $_GET.

Hope this can help.
0
 
sajuksCommented:
why do you want to pass the value in a url any special case ?
if not cant u directly refer to it ?
$hidden1 = $_POST['myhide'];  
$hidden1 = $_GET[''myhide'];

0
 
shai126Commented:
Just do the following:

<form action="THE_SITE_THAT_NEEDS_TO_GET_THE_VARIABLE" method="post">
<input type="hidden" name="VARIABLE_NAME" value="VARIABLE_VALUE">
<input type="submit">
</form>

now you can just reference the variable using:

$_POST['VARIABLE_NAME'];

from the new PHP script.

Hope this helps,
Shai
0
 
Jimmy_LauCommented:
kenabbott, Your question is kinda unnecessary in a way because as shai126 explains in his/her code above, your hidden value inside the form will be sent to the Target page automatically, without needing to add it to the URL (actually, if im not wrong, it is done this way by the browser internally when using the method="get", explained in the following paragraph I copied from a site:

"The HTML specifications technically define the difference between "GET" and "POST" so that former means that form data is to be encoded -by a browser- into a URL while the latter means that the form data is to appear within a message body"

)


so in Shail26 example

<form action="THE_SITE_THAT_NEEDS_TO_GET_THE_VARIABLE" method="post">
<input type="hidden" name="VARIABLE_NAME" value="VARIABLE_VALUE">
<input type="submit">
</form>

adding $_POST['VARIABLE_NAME'] inside the THE_SITE_THAT_NEEDS_TO_GET_THE_VARIABLE.php page, will retrieve the hidden value (or  $_GET['VARIABLE_NAME'] in case you use method="GET" in the form header) without needing to do it through the URL "manually".

Anyways, lets say that for an X reason you still want to be able to see the hidden value within the URL, I would use javascript to do it:


<script type="text/javascript" language="javascript">
function AddHiddenVariabletoURL(form)
{   form.action = "Targetpage.php?Variable="+form.VARIABLE_NAME.value;
    form.submit();
}
</script>

<html>
<body>

<form method="post" name="Form1">
<input type="hidden" name="VARIABLE_NAME" value="5">
<input type="button" value="submit" onclick="AddHiddenVariabletoURL(document.Form1)">
</form>

</body>
</html>


in this example, I converted the submit button into a plain button, as I want to change the "action" with the onclick method.
In this line:   form.action = "Targetpage.php?Variable="+form.VARIABLE_NAME.value; You can see the target URL with the variable explicitly added.

To read the value in the target php file, $_GET['Variable']  should be used instead, since the value was passed through the URL as the paragraph explained above.

You might try a final test by creating your Targetpage.php this way:

<html>
<body>

<?=$_POST['VARIABLE_NAME'] ?><br>
<?=$_GET['Variable'] ?>

</body>
</html>

and edding the AddHiddenVariabletoURL function (added "*2" at the end"):

function AddHiddenVariabletoURL(form)
{   form.action = "Targetpage.php?Variable="+form.VARIABLE_NAME.value*2;
    form.submit();
}

The resulting values should read:
5
10

"5" being received through POST shai126's way (without adding anything to the URL), and 10 received through the URL as you asked (5 multiplied by 2 previously)


Regards

   J.L.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now