Here is the code, it is telling me I have an undefined index which is weird because I already defined it with HTML code. Any way around this error? or any ways to make it better would be much appreciated...
<h1>make a username</h1>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Username: <input type="text" name="username" value="<?php echo $username; ?>">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
$username = $_POST["username"];
echo $username;
?>
$_POST["username"] will only be defined after you submit the form. When you first load the page (with an http GET), $_POST["username"] will be undefined.
After form submission it will work. You should try this:
<?php
if(isset($_POST["username"])){
$username = $_POST["username"];
}
?>
<h1>make a username</h1>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Username: <input type="text" name="username" value="<?php echo (isset($username))? $username: ''; ?>">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo (isset($username))? $username: '';
?>
Do following changes to your code :
<h1>make a username</h1>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Username: <input type="text" name="username" value="<?php echo $username; ?>">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
/// This portion of code will execute only after you hit submit button
if(isset($_POST['username']))
{
$username = $_POST["username"];
echo $username;
}
?>
if ( isset( $_POST["username"] ) ){
$username = $_POST["username"];
} else{
$username = '';
}
echo $username;
Try this code instead of your php code. This may help you.
$_POST["username"];
add an # before this,#$_POST['username'],'#' will ignore this error
Related
I have 3 pages. How can I print 1st page value to 3rd page. And my code is following (it just for sample).
page1.php
<form methord="post" action="page2.php">
username:<input type="text" name="username" >
password:<input type="text" name="password" >
<input type="submit" name="">
</form>
page2.php
<?php
$u=$_POST['username'];
$p=$_POST['password'];
?>
<form methord="post" action="page3.php">
username:<input type="hidden" name="username" value="<?php echo $u?>">
password:<input type="hidden" name="password" value="<?php echo $p?>">
mobile:<input type="text" name="mobile">
<input type="submit" name="">
</form>
Now how to print username, password, mobile in page3.php ?
Mistake 1 :
You method name is wrong.
Change this
<form methord="post" action="page2.php">
to
<form method="post" action="page2.php">
Implementation
In the page2
<?php
$u=$_POST['username'];
$p=$_POST['password'];
setcookie("u", $u);
setcookie("p", $p);
?>
You can get those values in the page3.php
<?php
$u = $_COOKIE["u"];
$p = $_COOKIE["p"];
?>
The value of Username is <?php echo $u?><br>
The value of Password is <?php echo $p?>
<br>
Learn more about Cookies here
Here is the eval for you
username:<input type="hidden" name="username" value="<?php echo $u;>">
There is a typo,
replace methord to method
and simply echo username and other fields on 3rd page like
<?php echo $_POST['username']; ?>
I want to pass the value of input field address to php. So that when i click on submit button it will be store in php.
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
print(document.address.value);
}
?>
However when i click on button it response nothing.
You are mixing PHP and JavaScript. If you want a pure PHP solution you could do something like:
<?php
if(isset($_POST['go'])){
$address = $_POST['address'];
echo $address;
}
?>
<form method="POST">
<input type="text" name="address">
<input type="submit" name="go" method="post">
</form>
With PHP once the form is submitted you can access form values with $_POST so use $_POST['address'] instead of document.address.value
what you intend to do can be done as below method.
<form method="post" >
<input type="text" name="address">
<input type="submit" name="go">
</form>
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>
Try like this,this is working fine as per your requirement:
<html>
<body>
<form action="" method="post">
Address: <input type="text" name="name"><br>
<input type="submit" name="go" value="call" />
</form>
<?php
if (isset($_REQUEST['name'])) {
call();
}
?>
<?php
function call() {
$address= $_POST['name'];
echo "Address:".$address;
exit;
}
Output:-
For output Click here
use this code
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>
I have these two forms that submits to different blocks of the same script. i am unable to access variable of one block in other, though both variables are in same script.
html form :(1.php)
<html>
<form method="POST" action="2.php" enctype="multipart/form-data">
</br>
Choose a user name:</font>
<input type="text" name="username">
<input type="submit" name="submit" value="Save and Proceed">
</form>
</html>
2.php:
<?php
$name=$_POST['username'];
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font>
<input type="text" name="age">
<input type="submit" name="submit" value="done">
</form>
</html>
<?php
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
How do I access $_POST['username'] in the code following the html form in the same script? Thank you in advance.
On the second form you can use a hidden input, i.e.:
<input type="hidden" name="username" value="$name">
Example:
<?php
$name=$_POST['username'];
if (!empty($_POST['username']) && $_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
echo <<< LOL
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="hidden" name="username" value="$name">
<input type="submit" name="submit" value="done">
</form>
</html>
LOL;
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
If I understand well you want to pass username twice.
Than you can use hidden input, which is not visible (only transports data):
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />
My code is below. It's working fine, but i want it not to display form again after the errors were found. How can it be done?
This is a single page, username validation program i just want to know why form is displayed again after, and how will i solve it.
<?php require'function.php'; ?>
<?php
$errors = array();
$username = "";
$password = "";
$msg = "Please Log in.";
?>
<?php if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if($username != "rish" && $password != "password"){
$msg = "Try again.";
$errors['cred'] = "Wrong Credentials.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Single Page Form</title>
</head>
<body>
<?php
echo display_error($errors);
?>
<?php echo $msg ?>
<form action="singleform.php" method="post">
Username: <input type="text" name="username" value="<?php echo htmlspecialchars($username); ?>"><br>
Password: <input type="password" name="password" value="<?php echo "" ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
You can do verifying if the $_POST data is empty. If was not empty, the users just load the page.
Can be do like code below:
<?php if(empty($_POST)):?>
<form action="singleform.php" method="post">
Username: <input type="text" name="username" value="<?php echo htmlspecialchars($username); ?>"><br>
Password: <input type="password" name="password" value="<?php echo "" ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php endif;?>
user isset ,, if the error initializes then it won't be displayed
<? if (!isset($errors['cred'])) { ?>
<form action="singleform.php" method="post">
Username: <input type="text" name="username" value="<?php echo htmlspecialchars($username); ?>"> <br>
Password: <input type="password" name="password" value="<?php echo "" ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php } ?>
i am trying to post the some data on another page at this i have a back button onclick of back button i wanna reload the original page with posted data.
use $_SESSION:
Page1.php
<?php
session_start();
$name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
?>
<html>
<form method="post" action="Page2.php">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="submit" value="Submit" />
</form>
</html>
Page2.php
<?php
session_start();
if (isset($_POST['name']))
{
$_SESSION['name'] = $_POST['name'];
}
$name = $_SESSION['name'];
?>
<html>
<form>
<span><?php echo $name; ?>" </span>
Back
</form>
</html>
you can use $_SESSION
<?php
if (!isset($_SESSION)) session_start();
if ($_POST['btnsubmit']){
$_SESSION['data1'] = $_POST['data1'];
$_SESSION['data2'] = $_POST['data2'];
$_SESSION['data3'] = $_POST['data3'];
//... and so on
}
?>
<form>
<input type="text" id="data1" value="<? echo $_SESSION['data1'];?>"/>
<input type="text" id="data2" value="<? echo $_SESSION['data2'];?>"/>
<input type="text" id="data3" value="<? echo $_SESSION['data3'];?>"/>
<input type="submit" id="btnsubmit" value="Submit"/>
</form>
<!-- and so on -->
Hope this gives you an idea