Serhan BAKIR
Serhan BAKIR

Reputation: 59

Add data to $_POST and submit to another page

I'm trying to add a value to $_POST data while it gets submitted to the target page as follows:

post.php

<?php  $_POST['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
    <form method="post" action="catch.php">
        <input name="field2" type="text"/>
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

catch.php

<?php 
foreach ($_POST as $key => $value) {
    echo $key . " : ". $value;
    echo "<br/>";
}
?>

but I cannot catch 'field1' on the other end. I don't want to use a hidden input field. How can I achieve that? Thanks!

Upvotes: 2

Views: 13771

Answers (5)

Piskvor left the building
Piskvor left the building

Reputation: 92792

You're not submitting field1 anywhere. What happens is this:

  • post.php generates a HTML page (one that doesn't contain any reference to field1)
  • the user's browser renders the page
  • on submit, only the elements inside the form are submitted
  • catch.php receives the elements submitted above.

In other words, you need to get that value into your form:

<form method="post" action="catch.php">
    <input name="field2" type="text"/>
    <input name="field1" type="hidden" value="<?php echo htmlspecialchars($_POST['field1']) ?>"/>
    <input type="submit" value="Submit" />
</form>

There is no other way to get the value into your POST data, if it's not present in the form. What you could do as a workaround is store the data in GET (size limit), session (concurrency issues - what happens when the user has two tabs open, each with different session data?), or cookies (size limit AND concurrency issues).

Upvotes: 2

Faiyaz Alam
Faiyaz Alam

Reputation: 1227

try this: in post.php

 <?php  $_SESSION['field1'] = "Value1";?>
    <html>
    <head>
    </head>
    <body>
        <form method="post" action="catch.php">
            <input name="field2" type="text"/>
            <input type="submit" value="Submit" />
        </form>
    </body>
    </html>

in catch.php

    <?php 
if(isset($_SESSION['field1']))
{
$_POST['field1'] = $_SESSION['field1'];
unset($_SESSION['field1']);
}
    foreach ($_POST as $key => $value) {
        echo $key . " : ". $value;
        echo "<br/>";
    }
    ?>

Make sure you have started the session. Note: you must use hidden elements or query string as other user suggested.

Upvotes: 0

Fanda
Fanda

Reputation: 3786

Since $_POST are data sent by post method to script, you can not use it for another request directly. You need to compose and send another post request. The easiest way for you will be to use hidden input field/s.

Or you can choose another approach to make http post request, for example curl methods.

If you don't need data to be given by post method, you can save it in session, for example.

Upvotes: 0

Edson Horacio Junior
Edson Horacio Junior

Reputation: 3143

When you send the form, the $_POST data is reset and assumes only the inputs inside the form and a possible query string you may have appended to form action.

The best way to accomplish what you want is using hidden field but since you dont want it, you can append a query string to your form action:

<form method="post" action="catch.php?field1=Value1">

Upvotes: 2

Glubus
Glubus

Reputation: 2855

You can't do it this way. If you want to send the data you're trying to add to the POST there only through the users form, you are forced to also store it somewhere client side (e.g. a hidden field or a cookie). What you're doing right now is setting some value into the POST variable, but it gets overridden by the users form post (or rather the $_POST variable you're using after the form post is another instance).

What you could do instead to keep it serverside is save the value in the variable to the session of the user, then in the form post action server side get the value again (given the fact that you're using sessions). Lastly you could just store it in some table in a database, though I wouldn't do this.

Upvotes: 0

Related Questions