I have an html code that passes the values of text fields to a php file. To check for the value of these variables , I use echo to print the values but nothing gets printed.
This is my html code:
<form action="connect.php" type="post">
Username: <input type="text" name = "uname"><br>
Confirm Username: <input type="text" name = "cuname"><br>
Password: <input type="password" name = "pword"><br>
Confirm Password: <input type="password" name="cpword"><br>
<input type="submit" value="Sign up">
</div>
</form>
This is my connect.php code:
<?php
$info1 = isset($_POST['uname']);
$info2 = isset($_POST['cuname']);
$info3 = isset($_POST['pword']);
$info4 = isset($_POST['cpword']);
echo $info1;
echo $info2;
echo $info3;
echo $info4;
?>
isset()
is a boolean function that tells you if a variable is set or not. If you want to print the value itself, try:
$info1 = $_POST['uname'];
echo $info1;
If you want to see what comes from the function, try debugging with:
print_r($_POST);
a) correct:
<form action="connect.php" type="post"> to <form action="connect.php" method="post">
b) mostly, you don't "pass the values of text fields to a php file", you are trying to output the input at screen. If you want to save and reuse them, you have to use a database.
<form action="connect.php" method="POST">
Username: <input type="text" name = "uname"><br>
Confirm Username: <input type="text" name = "cuname"><br>
Password: <input type="password" name = "pword"><br>
Confirm Password: <input type="password" name="cpword"><br>
<input type="submit" value="Sign up">
</div>
</form>
the php....
<?php
if (isset($_POST['submit'])) //if form submitted...
{
$info1 = $_POST['uname']; // Get POST value
$info2 = $_POST['cuname'];
$info3 = $_POST['pword'];
$info4 = $_POST['cpword'];
echo $info1;
echo $info2;
echo $info3;
echo $info4;
}
?>
To avoid errors like Undefined index
you can always use the at sign (@)
<?php
$info1 = @$_POST['uname'];
$info2 = @$_POST['cuname'];
$info3 = @$_POST['pword'];
$info4 = @$_POST['cpword'];
echo $info1;
echo $info2;
echo $info3;
echo $info4;
?>