passing hidden field values via URL

Posted on 2005-04-11
Last Modified: 2011-10-03

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


Question by:kenabbott
    LVL 5

    Expert Comment


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

    Expert Comment

    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'];


    Accepted Solution

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

    now you can just reference the variable using:


    from the new PHP script.

    Hope this helps,
    LVL 1

    Assisted Solution

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

    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 method="post" name="Form1">
    <input type="hidden" name="VARIABLE_NAME" value="5">
    <input type="button" value="submit" onclick="AddHiddenVariabletoURL(document.Form1)">


    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:


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


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

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

    The resulting values should read:

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



    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now