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>
Related
Here is my HTML form
<html>
<body>
<h1>HTMl FORM</h1>
<p>
<form action="example.php" method="POST">
<p>Your First name: <input type="text" name="fname" /></p>
<p>Your Last name : <input type="text" name="lname" /></p>
<p><input type="Submit" name="Submit" value="Submit" /> <input type="Reset”/></p>
</form>
</p>
</body>
</html>
PHP file "example.php" Shows the Client what was inputted and should append to the text file "phpfile.txt"
<html>
<body>
<p>
Hi <?php echo htmlspecialchars($_POST['fname']); ?>.<br>
<?php echo htmlspecialchars($_POST['lname']); ?>
</p>
<?php
if(isset($_POST['Submit'])){
$fname = $_POST['fname']."
";
$lname = $_POST['lname']."
";
$file=fopen("phpfile.txt", "a");
fwrite($file, $fname);
fwrite($file, $lname);
fclose($file);
}
?>
</body>
</html>
I am getting the php file to print for the user but it is not appending to the .txt file. I am unsure where I am going wrong
You are probably getting a syntax error.
You also need to use \n to add a new line. You also need to place the echo $_POST parts in the isset function.
<html>
<body>
<h1>HTMl FORM</h1>
<p>
<form action="" method="POST">
<p>Your First name: <input type="text" name="fname" /></p>
<p>Your Last name : <input type="text" name="lname" /></p>
<p><input type="Submit" name="Submit" value="Submit" />
<input type="Reset"/>
</p>
</form>
</p>
<?php
if(isset($_POST['Submit'])){
echo 'Hi '.htmlspecialchars($_POST['fname']);
echo '<br>';
echo htmlspecialchars($_POST['lname']);
$fname = $_POST['fname']."";
$lname = $_POST['lname']."";
$file=fopen("phpfile.txt", "a");
fwrite($file, $fname." ".$lname. "\n");
fclose($file);
}
?>
</p>
</body>
</html>
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";
}
}
}
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.
So recently I've been using action="form.php" in my forms. But now I see that you can do without needing an action, since the PHP and the form are in the same file. Which way would be more secure. A PHP file by itself, followed by another file taking care of the form or both combined?
So would I do? Is it safer?
<?php
$name = $_POST['name'];
?>
<form method="post">
<input type="text" name="name">
<input type="submit>
</form>
user <?php echo $_SERVER['PHP_SELF'];?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr><td> Subject:</td><td> <input type="text" name="subject" /></td></tr>
<tr><td> Message:</td><td> <input type="text" name="message" /></td></tr>
<tr><td> <input type="submit" value="Submit" name="submit"/></td><td><input type="reset" value="Clear" /></td></tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$email = "example#test.com";
mail($email, $subject, $message);
echo "<center>Email sent</center>";
}
?>
EDIT: The code seems to work if I place the PHP code in the same file at the HTML. I'll implement this method for now. Thank you for the help!
Whenever I try to echo the values in a PHP file from an input box the values appear to be empty.
Here is my HTML code
<form action="sendmail.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
This is my sendmail.php file
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo $name;
echo $email;
echo $message;
You can get the value in Your current page itself
<?php
if(isset($_POST['person']))
{
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo "Name".$name;
echo "Email".$email;
echo "Message".$message;
}
?>
<form action="" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
or if you want the data in another page
<form action="send.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
send.php file
<?php
if(isset($_POST['person']))
{
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo "Name".$name;
echo "Email".$email;
echo "Message".$message;
}
?>
I tried this code and it worked fine, so whatever is wrong it is not with the code you have pasted here.
to make sure the post is successful, try this:
if(isset($_POST['person']))
echo "passed fine.";
check:
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
if(isset($_POST['person'])){
$name = $_POST['person'];
echo $name;
}
}
else{
echo"Your form submission is not correct";
}
try this code.
HTML
<form action="sendmail.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
PHP
<?php
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo $name;
echo $email;
echo $message;
?>
<form action="456.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
<?
print_r($_POST);
?>