There is a page: login.php
I try to receive username and password via form.
login.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Giris Yap</title>
</head>
<body>
<form id="form1" name="form" method="post" action="welcome.php">
<table width="275" border="1">
<tr>
<td width="103">user; name: </td>
<td width="156"><label>
<input type="text" name= "name" />
</label></td>
</tr>
<tr>
<td>password:</td>
<td><label>
<input type="password" name="textfield2" />
</label></td>
</tr>
</table>
<p><label>
<input type="submit" name="Submit" value="submit" />
</label>
<label>
<input type="reset" name="Submit2" value="reset" />
</label>
</p>
</form>
<p> </p>
<p> </p>
</body>
</html>
After that I want to lead my page into welcome.php and says welcome ...name... But it does not work. Can anyone help me.
welcome.php:
<?php
$name = $_REQUEST['name'];
echo "Welcome"$name;
?>
you can try it.
<?php
if(isset($_POST['name']))
{
$name = $_POST['name'];
echo "Welcome ". $name;
}
?>
Try
?php
if(isset($_POST['name']))
{
$name = $_POST['name'];
echo "Welcome $name";
}
else
{
echo "Name not set";
}
?>
Try this:
<?php
// let us make sure we have a post value. we will use isset to make sure it's set.
if (isset($_POST['name'])) {
// you should scrub the $name value before displaying. Never display or work with
// raw post values.
$name = $_POST['name'];
echo "Welcome ";
// what if they didn't really put in a name?
// maybe only show it if it's longer than 2?
if (strlen($str) > '2')
echo $name;
}
}
else {
// we don't have a post value? You should redirect with error message or something.
}
?>
It is all depends on the method you have specified in your form. If your form method="post" then you need retrieve like $_POST['name']
and if your form method="GET" then you need retrieve like $_GET['name']
$name = $_POST['name']; // you have used method='post'
echo "Welcome" .$name;
Also, $_REQUEST - An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
Don't forget, because $_REQUEST is a different variable than $_GET and $_POST, it is treated as such in PHP -- modifying $_GET or $_POST elements at runtime will not affect the ellements in $_REQUEST, nor vice versa.
e.g:
<?php
$_GET['foo'] = 'a';
$_POST['bar'] = 'b';
var_dump($_GET); // Element 'foo' is string(1) "a"
var_dump($_POST); // Element 'bar' is string(1) "b"
var_dump($_REQUEST); // Does not contain elements 'foo' or 'bar'
?>
Ref: http://php.net/manual/en/reserved.variables.request.php
Try like this.
Always make use of isset() construct in this context. Since we know that your <form> method has action POST, you can make use of the $_POST instead of $_REQUEST
<?php
if(isset($_POST['name']))
{
$name = $_POST['name'];
echo "Welcome $name";
}
?>
Also, you are missing the submit button code on your <form> , Do like this
<form id="form1" name="form" method="post" action="welcome.php">
<table width="275" border="1">
<tr>
<td width="103">User; Name: </td>
<td width="156"><label>
<input type="text" name= "name" />
<input type="submit" name="submit" />
</label></td>
</tr>
</form>
Related
I have PHP code with HTML inline, yet I need them to be separate on two pages. A HTML page with a form and a PHP form with the process on it.
The bits with the PHP form verification I need on a PHP page and the form I need on a HTML sheet the rest of the website will be on
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php // define variables and set to empty values
$nameErr = $emailErr = "";
$name = $email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["name"])) {$nameErr = "Name is required";}
else{$name = test_input($_POST["name"]);}
if(empty($_POST["email"])) {$emailErr = "Email is required";}
else{$email = test_input($_POST["email"]);}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
?>
</body>
</html>
updated HTML code:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php require 'php.php'; echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php require 'php.php'; echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php require 'php.php'; echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
You'd need to put your php code in an other file (for example form.php). In your html file you need to include your php script. If your php file is situated in the same directory as your html file, your include statement could look like this.
<?php include 'form.php';?>
I'd like to refer you to W3schools php include where they explain in detail how you can link your php file.
I am just giving you a basic example to separate form file and php file.
create a index.php file and write those code there
<form action="post.php" method="post">
<input type="text" name="txtName"/>
<input type="submit" name="btnSubmit"/>
</form>
Then create a post.php file and put below code there
<?php
if(isset($_POST['btnSubmit'])){
$val = $_POST['txtName'];
echo $val;
}
?>
Hope it will help.
So here you have all the code for html form in your file. Now create a new file for process the form like "process.php" and post your form on that file. place this new file on the same directory where the HTML file is being placed and give path of this file into your form action = " " like
<form method="post" action="process.php">
and in the process.php page you can process you form as you are doing.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
}
I have recently installed Apache 2.4.7 server, as well as, PHP 5.5.10. I'm just beginning to learn PHP. I'm currently working through the w3school's tutorial on form processing with PHP, but I can't get a particular example to work. Does anyone here see an error?
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Website: <input type="text" name="website"><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male<br>
<input type="submit">
</form>
</body>
</html>
When I visit this website, I get a quote and a greater than sign before the input field name specifically: "> Name. When I submit the form, the URL shows as
http://127.0.0.1/%3C?php%20echo%20$_SERVER[
I can't enter the exact contents of the URL, because this website won't let me, but the %3C actually shows up as a less than sign. The %20 shows up as a space. So, the problem is that the php script inside the action tag does not run. The action variable is then filled with the php script instead of the location of the current page. Why is the PHP script not running inside my form tag?
Solution:
Thank you everyone for helping. Abhik Chakraborty your comment led me to the solution. No, I did not save my file with a .php extension. I changed the extension to .php and it worked perfectly. I would have posted this as a solution, but I have to wait eight hours because I don't have enough reputation points.
If you leave action tag undefined it will use current location.
somefile.php:
<form method="post">
will execute the same uri
Replace action="<?php echo $_SERVER["PHP_SELF"];?>" by action=""
Remove action tag in your form. bcoz action is not mandatory for current page.
if u given $_SERVER['PHP_SELF'] . concatinate to your full path.
Try this one, this should work
<?php echo "<form method='post' action='".$_SERVER["PHP_SELF"]."' >";
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
try this one this is the method posted on w3school to refer to self page at till now faced no problems with it.
you are not processing the data you can try this
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Website: <input type="text" name="website"><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male<br>
<input type="submit">
</form>
Name: <?php echo $name?> <br />
Email: <?php echo $email?> <br />
Gender: <?php echo $gender?> <br />
Comment: <?php echo $comment?> <br />
Website: <?php echo $website?> <br />
</body>
</html>
Im unable to find out the solution , please help. below is the code. Thanks in advance
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$Lname = $_POST['lastName'];
$num = $_POST['number'];
echo $name.$Lname.$num;
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><Person></Person>");
Header('Content-type:text/xml');
$name = $xml->addChild('name',$name);
$Lname = $xml->addChild('LastName',$Lname);
$Number = $xml->addChild('Number',$num);
print($xml->asXML());
}
?>
<!DOCTYPE html>
<head>
<title>XML</title>
</head>
<body>
<form method="post" action="" enctype= multipart/form-data>
<input type="text" name="name" value="Name" required/>
<input type="text" name="lastName" value="LName" required/>
<input type="text" name="number" value="Number" required/>
<input type="submit" value="send" name="submit"/>
</form>
</body>
</html>
Im unable to find out the solution , please help. below is the code. Thanks in advance
You need to wrap the <form> in the else part and remove the unneccesary echo statement.
The working code..
<?php
if(isset($_POST['submit'])){
header('Content-type:text/xml');
$name = $_POST['name'];
$Lname = $_POST['lastName'];
$num = $_POST['number'];
//echo $name.$Lname.$num; //<---- Commented
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><Person></Person>");
$name = $xml->addChild('name',$name);
$Lname = $xml->addChild('LastName',$Lname);
$Number = $xml->addChild('Number',$num);
print($xml->asXML());
}
else
{
?>
<!DOCTYPE html>
<head>
<title>XML</title>
</head>
<body>
<form method="post" action="" enctype= multipart/form-data>
<input type="text" name="name" value="Name" required/>
<input type="text" name="lastName" value="LName" required/>
<input type="text" name="number" value="Number" required/>
<input type="submit" value="send" name="submit"/>
</form>
</body>
</html>
<?php } ?>
OUTPUT :
<Person>
<name>Nameasdasd</name>
<LastName>LNameasdasd</LastName>
<Number>Number32424</Number>
</Person>
You can not mix XML and HTML in that way. It invalidates the XML document.
Remove:
echo $name.$Lname.$num;
Ref. prolog. This is what causes the error you witness.
Abort script on form data (or use an else clause):
print($xml->asXML());
exit(0);
Else you get data in trailing section of XML-document.
For valid HTML you also need to add a value for action in the form. Quotes on attribute values are usually optional, but would advice consistency, thus also quote enctype.
<form method="post" action="" enctype= multipart/form-data>
replace it with
<form method="post" action="" enctype="multipart/form-data">
this is my first form
<style>
.wrap-form{
width: 700px;
min-height: 20px;
background-color: lightblue;
margin: 0 auto;
}
</style>
<div class="wrap-form">
<form method="post" action="advanced-form-send"></form>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</div>
And this is my form 2 where the data will send to
<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo $name;
echo $email;
?>
How would I fix this?
That's how your form code should be, surrounding your submit button by <a href="..." is causing the form not to be submitted, at the place you're just telling the browser that when this button is clicked, take the user to the page advanced-form-search.php. What you should do is put the script name where the form should be submitted in the action of the form tag then just add a submit button. And don't forget to close your tags... you missed the </form>
<div><?php if(isset($_GET['err'])){ if($_GET['err']==1){ echo 'You didn\'t fill in your name'; } elseif($_GET['err']==2){ echo 'You didn\'t fill in a correct email address';}}?></div>
<div class="wrap-form">
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
Here's the PHP code to send an email:
<?php
if(isset($_POST['name']) && !empty(trim($_POST['name']))){
$name = $_POST["name"];
} else {
header("Location: page_where_the_form_is.php?err=1");
die()
}
if(isset($_POST['email']) && filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)){
$email = $_POST["email"];
}
else {
header("Location: page_where_the_form_is.php?err=2");
die()
}
$subject='Form Submitted On The Website';
$message="Name: {$name}\nEmail: {$email}";
mail($to_email, $subject, $message);
?>
You've wrapped your <input type="submit" name="submit"... button with an <a href=.... What this does is prevents your form from being submitted as a POST request, and instead gets linked to as a GET request.
git rid of the <a href..., and and change your action=advanced-form-send to action="advanced-form-send.php".
You can improve your php code:
<?php
if (isset($_POST["name"])) {
$name = $_POST["name"];
echo $name;
} else {
echo 'Eror: Attribute "name" is missing!';
}
if (isset($_POST["email"])) {
$email = $_POST["email"];
echo $email;
} else {
echo 'Eror: Attribute "email" is missing!';
}
?>
And your form should be changed:
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
You need to wrap your variables. The other answers are about the form. This answer is about your handling of variables. PHP will throw a notice if you use a variable that haven't been set. By using isset() you can determine if the variable is set and then use it.
<?php
$name = isset($_POST["name"]) ? $_POST["name"] : '';
$email = isset($_POST["email"]) ? $_POST["email"] : '';
echo $name;
echo $email;
?>
Doing it this way ensures that you wont get a notice if one of your fields haven't been posted. If they have you will have the value in $name and $email. If not then the variables will be empty strings.
Try Like this,
<div class="wrap-form">
<form method="post" action="advanced-form-send">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
The submit button should be in the two "form" markups. Also the link is unnecessary because the input "submit" will send the form with the data to the page you set in the attribute "action" of the form markup.
<div class="wrap-form">
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
Youre missing an enctype ... you ALL forgot it. Shame above you!
<form method="post" action="advanced-form-send.php" enctype="multipart/form-data"></form>
In order to work properly my form needs to be set up to first check the validation and then post the data if the validation passes. This is fine but I am not sure how to combine the validation code with the post code in the form action. Example if the action is: action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> the form validates correctly but does not send anywhere! If I change the form action to action="contact-engine.php"> then the form is posted but without validation! With this in mind I need to combine into the action both the validation and then (once passed validation) the contact-engine.php problem is I simply do not know how to do this? I really am a learner in php and this is complicated for me! Any help is really appreciated I have been working on this from now for a few days! (N.B. both pages are .php) Full code is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Help!</title>
<style type="text/css">
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = "";
$name = $email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//Name
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "Only letters and white space allowed";
}}
//Email
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" id="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p><span class="error">* required field.</span></p><br />
<div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Name:<br />
<input type="text" name="name" class="border" size="25" value="<?php if(isset($_POST['name'])) {echo $_POST['name']; } ?>">
<span class="error"><?php echo $nameErr;?></span>
</div>
<div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Email: (please double check enty)<br />
<input type="text" name="email" class="border" size="25" value="<?php if(isset($_POST['email'])) {echo $_POST['email']; } ?>"><span class="error">
<?php echo $emailErr;?></span>
</div>
<div>
<input type="submit" value="Send" id="submit">
</div>
</form>
</body>
</html>
And below is the contact-engine code:
<html>
<head>
<title>Contact Engine</title>
</head>
<body>
<br />Name:<?php echo htmlspecialchars($_POST['name']); ?><br />
<br />Email:<?php echo htmlspecialchars($_POST['email']); ?><br />
</body>
</html>
You can try this. This is a simple code below through which you can achieve this:
//sample index.php file
<?php
include 'submitted.php';
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type="text" name="foo" />
<input type="submit" value="submit">
</form>
</body>
</html>
Here, I am submitting the form to the same page using post method. But, I have included another file which is include 'submitted.php'.
Here is the test script inside the submitted.php
//sample submitted.php
<?php
if(isset($_POST['foo']))
{
if(strlen($_POST['foo']) < 5){
echo "String length too small";
}
else
{
echo $_POST['foo'];
}
}
?>
For test, it simply checks if the length is more than five or not. If it is not, the error message is displayed on the same page where your page is present.
Test it yourself too.