Reputation: 15
i want to know how to send value form my_name in index.php below to result.php.
below is the php code to check for errors after the input form. and how to send form results that there is no error to result.php
here is index.php
<?php
if ($_POST["_submit_check"]) {
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
process_form();
}
} else {
show_form();
}
function process_form() {
//if doesnt makes error
//i want to send result "my_name" to result.php
header('Location: result.php');
}
function show_form($errors = '') {
if ($errors) {
print implode('</li><li><br>', $errors);
print '</li></ul>';
}
print<<<_HTML_
<form method="POST" action="$_SERVER[PHP_SELF]">
Your name: <input type="text" name="my_name">$errors[0];
<br/>
<input type="submit" value="Submit">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}
function validate_form() {
$errors = array();
if (strlen($_POST['my_name']) < 3) {
$errors[] = "Your name must be at least 3 letters long.";
}
return $errors;
}
?>
here is result.php
<?php
//prints result form from index.php
?>
Upvotes: 0
Views: 164
Reputation: 4718
PHP session variables hold information among pages.
Please try this.
At index.php
<?php
session_start();
// set post date into session variable
$_SESSION['my_name'] = $_POST['my_name'];
$_SESSION['my_number'] = $_POST['my_number'];
?>
And at result.php
<?php
session_start();
// get date from session variable
$my_name = $_SESSION['my_name'];
$my_number= $_SESSION['my_number'];
echo $my_name;
echo $my_number;
?>
Upvotes: 1
Reputation: 125564
you can do it with HTTP GET variables
header('Location: result.php?name='.$_POST['my_name']);
in result.php
you can see the value
echo $_GET['name'];
Upvotes: 1