I have a php page with the following:

echo "<form method='post'><input type='hidden' id='memberIdd' name='memberIdd' value=" . $memberId . "></form>";

Now I created another php page and want to grab the value of this hidden field and place it into another variable. How can I do this? I tried using this for my second php page:

$member = $_POST['memberIdd'];

But I just keep getting "undefined" for $member.

Thanks

Dani AI

Generated

Hidden inputs are only sent if the form actually submits to the target page, and the key must exist in the POST body. In your echo snippet the value attribute is unquoted; if $memberId contains spaces or quotes, the browser may not send it as expected. Also, reading $_POST['memberIdd'] without checking will trigger an undefined index notice when the request is not a POST or the field is missing. Quote and escape the value, then validate the request method on the receiving page.

Example sender (avoid echoing complex HTML so you do not misquote attributes):

<?php $memberId = 123; ?>
<form method="post" action="script2.php">
  <input type="hidden" name="memberIdd"
         value="<?php echo htmlspecialchars((string)$memberId, ENT_QUOTES, 'UTF-8'); ?>">
  <button type="submit">Continue</button>
</form>

Example receiver (robust, avoids undefined notices):

<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    exit('Submit the form to access this page.');
}
$member = filter_input(INPUT_POST, 'memberIdd', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if ($member === null || $member === '') {
    exit('memberIdd was not provided.');
}
// use $member safely here

If it still fails: open your browser dev tools, submit the form, and check Network -> Payload to confirm memberIdd is present; ensure the input sits inside the <form>...</form>; and verify the name matches exactly on both pages.

Recommended Answers

All 6 Replies

Hey there,

Try using $_REQUEST ... if this isn't working, then try renaming the ID and NAME in your form.

Be sure to add an action to your form so it submits to the second script:

echo '<form method="post" action="script2.php"><input type="hidden" id="memberIdd" name="memberIdd" value=' . $memberId . '></form>';

You can then access the value as $_POST.

There is also nothing to submit the form, you need a Subit button

commented: doh! +14

You don't need the ID attribute because when you use these $_GET or $_POST, they look for the name attribute. In your case, it is name='memberIdd'

Cheers,

You might want to try using GET for development, so you can see if anything shows up in the URL bar. If not, the problem is with the source page. If so, the problem is with the destination page.

The destination page should set $member to either $_GET[memberidd] or $_POST[memberidd] because you can't always control which method is used. Pseudocode:

if $_GET[...]
$member=$_GET[...]
else
if $_POST[...]
$member=$_POST[...]

make sense?

in the first place it like the form contains nothing important to send. if I were you I will just use session. hope you get what am saying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.