HTML form input is not grabbed by PHP script - php

When I click on the submit button after filling name and mail id columns , data entered in those fields is not shown in php page.
HTML code :-
<html>
<body>
<form action="data.php" method="post">
Name: <input type="text" name="name" /><br>
E-mail: <input type="text" name="email"/><br>
<input type="submit">
</form>
</body>
</html>
PHP code :-
Welcome <?php echo $_REQUEST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

Try it like this;
HTML
<body>
<form action="data.php" method="post">
Name: <input type="text" name="name" /><br>
E-mail: <input type="text" name="email"/><br>
<input type="submit">
</form>
</body>
</html>
PHP
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST["name"]) && isset($_POST["email"])){
$name = $_POST["name"];
$email = $_POST["email"];
if(!empty($name) && !empty($email){
echo "Your name is $name with the email : $email";
} else {
echo "Name or Email is not set";
}
}
}

Related

Passing data from form to controller in php

I used following code in view.php file. How to pass name and email to the controller.php file.
<?php $input = '1' ?>
<div>
<form action="<?php echo $input; ?>" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</div>
</body>
Any help can be really appreciated.
I'm not quite sure what you want, but this might be what you are looking for:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
echo "Name: " . $name . "<br />";
echo "Email: " . $email;
?>
<div>
<form action="" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</div>

Submit input text using HTML and PHP

I am trying to see inputed texting using HTML and PHP.
From what I have written so far, I am trying to display a name and email after running through PHP, but the only thing shown is:
Welcome
Your email address is:
HTML:
<form action="index.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
PHP:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is:
<?php echo $_POST["email"]; ?>
</body>
</html>
How can I make it so I see the input submitted?

PHP Form Get and Post Output with If statement to output results on same page

I'm learning PHP and working on forms right now. I have an assignment to create a form, one GET and one POST on the same page and have the results posted on the same page below each form.
I created an if/else statement but maybe I'm going about it incorrectly.
<div>
<h1>POST Method Form</h1>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
First Name:<br>
<input type="text" name="firstname"><br>
Last Name:<br>
<input type="text" name="lastname"><br>
E-mail: <input type="text" name="email"><br>
Database Utilized: <br>
<input type="checkbox" name="dba" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba" value="Oracle">Oracle<br>
<input type="checkbox" name="dba" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="postsubmit" value="Submit">
</form>
<br>
<h1>Database Consulting POST Form Results</h1>
<p>
<?php
if (isset($_POST['postsubmit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['laststname'];
$email = $_POST['email'];
$dba = $_POST['dba'];
echo $firstname; <br>
echo $lastname; <br>
echo $email; <br>
echo $dba; <br>
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
<br>
<h1>GET Method Form</h1>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
First name:<br>
<input type="text" name="firstname_get"><br>
Last name:<br>
<input type="text" name="lastname_get"><br>
E-mail: <input type="text" name="email_get"><br>
Database Utilized<br>
<input type="checkbox" name="dba_get" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba_get" value="Oracle">Oracle<br>
<input type="checkbox" name="dba_get" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="getsubmit" value="Submit">
</form>
<br>
<br>
<h1>Database Consulting GET Form Results</h1>
<p>
<?php
if (isset($_GET['getsubmit'])) {
$firstname_get = $_GET['firstname'];
$lastname_get = $_GET['laststname'];
$email_get = $_GET['email'];
$dba_get = $_GET['dba'];
echo $firstname_get; <br>
echo $lastname_get; <br>
echo $email_get; <br>
echo $dba_get; <br>
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
</div>
You misspelled $_GET['lastname'] as $_GET['laststname'] , change your action to action="" and include your <br> in your echo statement like echo $firstname .'<br>'; instead of echo $firstname; <br>
<h1>POST Method Form</h1>
<form method="POST" action="" >
First Name:<br>
<input type="text" name="firstname"><br>
Last Name:<br>
<input type="text" name="lastname"><br>
E-mail: <input type="text" name="email"><br>
Database Utilized: <br>
<input type="checkbox" name="dba[]" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba[]" value="Oracle">Oracle<br>
<input type="checkbox" name="dba[]" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="postsubmit" value="Submit">
</form>
<br>
<h1>Database Consulting POST Form Results</h1>
<p>
<?php
if (isset($_POST['postsubmit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$dba = $_POST['dba'];
echo $firstname .'<br>';
echo $lastname .'<br>';
echo $email .'<br>';
foreach ($dba as $database) {
echo $database .'<br>';
}
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
first of all, I would suggest having a hidden field called action like:
<input type="hidden" name="action" value="submituserinfo" />
then put your php logic in the top not the bottom, process before the page shows! Your PHP_SELF deal is good. But your logic is off. The correct logic should be:
if(isset($_REQUEST['action']) && $_REQUEST['action']=='submituserinfo'){
//analyze the data
if($errors){
//set a var to show errors below
}else{
//enter the data, whatever
}
}
That should get you started in the right direction. If you put the coding at the head of the document, you can better handle output or even use header() to redirect based on success or error.

How to connect page between signin,signup and profile in php

i'm still newbie in php, i want to connect this three page. I want to know how to tansfer data from signup to pfofile and also the sign in page. I tried to use tag but not working.
page=signup
<body>
<?php
// define variables and set to empty values
$email = $username = $name = $contact = $birthday = $gender = $address = $password = $payment ="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = test_input($_POST["email"]);
$username = test_input($_POST["username"]);
$name = test_input($_POST["name"]);
$contact = test_input($_POST["contact"]);
$birthday = test_input($_POST["birthday"]);
$gender = test_input($_POST["gender"]);
$address = test_input($_POST["address"]);
$password = test_input($_POST["password"]);
$payment = test_input($_POST["payment"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="login">
<form id="registeration" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<fieldset>
<legend><h1>Registeration</h1></legend>
<p>Email:<br/>
<input type="email" name="email"></p>
<p>Username:<br/>
<input type="text" name="username" />
</p>
<p>Your Name:<br/>
<input type="text" name="name"/></p>
<p>Contact Number:<br/>
<input type="text" name="contact"/></p>
<p>Birthday:<br/>
<input type="date" name="birthday">
<p>Gender:<br/>
<input type="radio" name="gender" value="male"/>Male
<input type="radio" name="gender" value="female"/>Female
</p>
<p>Address: <br/>
<textarea name="address" cols="50"></textarea></p>
<p>Password:<br/>
<input name="password" type="password" size="50"/></p>
</p>
<h2>Choose payment method:<br/></h2>
<p><input type="radio" name="payment" value="cod"/>CASH ON DELIVERY</p>
<p><input type="radio" name="payment" value="Online Banking"/>ONLINE BANKING : CIMB Clicks<br/>
<p><input type="radio" name="payment" value="Online Banking"/>ONLINE BANKING : Maybank<br/>
<p><input type="radio" name="payment" value="Online Banking"/>ONLINE BANKING : BSN<br/>
<p><input type="radio" name="payment" value="atm"/>ATM</p>
<p><button type="submit" value="submit">Submit</button><span> <span><span><span><span>
<button type="reset" value="cancel">Cancel</button></p>
</fieldset>
</form>
</div>
echo $email;
echo "<br>";
echo $username;
echo "<br>";
echo $name;
echo "<br>";
echo $contact;
echo "<br>";
echo $birthday;
echo "<br>";
echo $gender;
echo "<br>";
echo $address;
echo "<br>";
echo $password;
echo "<br>";
echo $payment;
?>
</body>
page=signin
<body>
<div id="login">
<fieldset>
<legend><h1>Login</h1></legend>
<form id="signin" method="POST">
<p>Username:<br/>
<input type="text" name="username"/></p>
<p>Password:<br/>
<input type="password" name="password"/></p>
<p><button type="submit" value="submit">Submit</button><span>
<button type="reset" value="cancel">Cancel</button></p>
</form>
</fieldset>
<!-- end .content --></div>
<?php
error_reporting(0);
if (!empty ($_POST)){
if ($_POST ["username"] == NULL){
echo "Please insert your username!";}
else{
$strusername=$_POST["username"];
echo "<p>$strusername</p>";
}}
if (!empty ($_POST)){
if ($_POST ["password"] == NULL){
echo "Please insert the password!";}
else {
$strpassword=$_POST["password"];
echo "<p>$strpassword</p>";
}}
?>
</body>
page=profile
<body>
<div class="container">
<div class="content">
<table>
<td colspan="5"><b>PROFILE</b></td>
<tr>
<td><div align="left">Username:</div></td><td><?php echo $username ?></td></tr>
<td><div align="left">Name:</div></td><td><?php echo $name ?></td></tr>
<td><div align="left">Username:</div></td><td><?php echo $username ?></td></tr>
<td><div align="left">Birthday:</div></td><td><?php echo $birthday ?></td></tr>
<td><div align="left">Contact Number:</div></td><td><?php echo $contact ?></td></tr>
<td><div align="left">Gender:</div></td><td><?php echo $gender ?></td></tr>
<td><div align="left">Address:</div></td><td><?php echo $address ?></td></tr>
<td><div align="left">My payment Method:</div></td><td><?php echo $payment ?></td> </tr>
<tr>
<td><div align="left"><button type="submit" value="Edit Profile" >Edit Profile</button></div> </td>
</tr>
</table>
<!-- end .content --></div>
<!-- end .container --></div>
</body>
Well, Try to use the post and get.
The PHP superglobals $_GET and $_POST are used to collect form-data.The example below displays a simple HTML form with two input fields and a submit button:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.
To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
The output could be something like this:
Welcome John
Your email address is john.doe#example.com
The same result could also be achieved using the HTTP GET method:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
and "welcome_get.php" looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
I Suggest you to follow this tutorial here
I hope it will help you, best regards
Use $_SESSION
In every page start with <?php session_start(); ?>
then store $_SESSION['email'] = test_input($_POST["email"]);
Then in any page you can use echo $_SESSION['email'];
You should use SESSION for simple login functionality....
refer this tutorial about basic login and functionality

Why is this not working?

I've this little script to send email! But it's not working... Firstly it says that my variables are not defined and then, they confirm me that the message has been sent, but it doensn't happen!
Name:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="name">
</form> <br><br>
E-Mail:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="header">
</form> <br><br>
Subject:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="subject">
</form> <br><br>
Message: <br>
<form action="contactus.php" method="POST">
<textarea rows="10" cols="40" name="message" value=""></textarea>
</form>
<br><br>
<form action="contactus.php" method="POST">
<input type="submit" value="Submit" name="submit">
</form>
<?php
// php script to send emails
$to = 'some#email.com';
if (isset ($_POST['message'])) {
$message = "$name" . "<br><br>" . $_POST['message'];
}
if (isset ($_POST['header'])) {
$header = "From:" . $_POST['header'];
}
if (isset ($_POST['subject'])) {
$subject = ($_POST['subject']);
}
if (isset ($_POST['name'])) {
$name = ($_POST['name']);
}
if (isset($_POST['submit'])){
mail($to, $subject, $message, $header);
echo "Your message has been sent!";
}
//end of the php script
?>
If some of you can help me would be great!
Thank you.
You can't use all those different form elements. You are using 5 separate forms and the only one that is being submitted is the one with the submit button.
Thus, when the form is being submitted there $_POST['submit'] is set, but none of the other ones exist.
So you need your HTML to be:
<form action="contactus.php" method="POST">
Name:
<input type="text" size="32" value="" name="name"><br><br>
E-Mail:
<input type="text" size="32" value="" name="header"><br><br>
Subject:
<input type="text" size="32" value="" name="subject"><br><br>
Message: <br>
<textarea rows="10" cols="40" name="message" value=""></textarea><br><br>
<input type="submit" value="Submit" name="submit">
</form>
and contactus.php:
<?php
$to = 'some#email.com';
if(isset($_POST['message']) && isset($_POST['header']) &&
isset($_POST['subject']) && isset($_POST['submit']) &&
#mail($to, $_POST['subject'], $_POST['message'], $_POST['header'])) {
echo "Your message has been sent!";
}else{
echo "There has been a problem.";
}
?>
Try:
if(mail($to, $subject, $message, $header)) {
echo "Mail sent successfully.";
} else {
echo "PHP's mail() function failed!";
}
Why you have a Form element for each input elements?
You should put all of your elements in a Form together.

Categories