user3520573
user3520573

Reputation: 117

How to post a hidden form value to a PHP page

<!doctype html>
<html>
<head>
<body>
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
</form>
</body>
</html>
<?php
header('Location: question.php');
?>

question.php File...

<?php

$rr = $_POST['session'];

echo $rr;

?>

it should print 12 right?

But i get this error "Notice: Undefined index: session in C:\wamp\www\Project\question.php on line 3".

what is the problem here?

thanks...

Upvotes: 0

Views: 8315

Answers (4)

Frank
Frank

Reputation: 2043

<!doctype html>
<html>
<head>
<body>
<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" name="send" value="send">
</form>
</body>
</html>

question.php:

<?php
# prints all post elements
print_r($_POST); // for method="post"

echo $_POST['session']; # only the hidden input named "session"

Upvotes: 0

user17677305
user17677305

Reputation: 1

wow, pretty complicated... all you needed to do was add

<?php
session_start();
// session_start has to be the first line 
// of your php and before any HTML code
?>
<html>
<body>
</body>
</html>

Upvotes: 0

Ko Cour
Ko Cour

Reputation: 933

But what if i want to submit the from automatically without a submit button:

jsfiddle

<form action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" value="submit" id="sub"/>
</form>

jQuery:

$(function(){
    $('#sub').click();
});

Upvotes: 1

Kevin Lynch
Kevin Lynch

Reputation: 24703

This will echo your value once you click the submit button.

question.php File

<?php  
$rr = $_POST['session'];
echo $rr;
?>

HTML current page

<!doctype html>
<html>
<head>
<body>
<form id="myForm" action="question.php" method="post">
<input type="hidden" name="session" value="12" />
<input type="submit" value="submit" />
</form>
</body>
</html>

// this will auto submit your form using javaScript
<script type="text/javascript">
document.getElementById("myForm").submit();
</script>

Upvotes: 1

Related Questions