$_POST returning empty value - php

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);
?>

Related

PHP form not submitting to the next page

The landing page has form but it is not submitting and not redirecting to the next page.After submitting the form, it stays on the same page.
It was alright and was working before but I cant figure out where is the problem.
Code in formPage.php is below:
<form action="insert.php" enctype="multipart/form-data" class="contact_form" method="post" name="htmlform" >
<input class="frm-input" name="name" type="text" size="30" maxlength="50" placeholder="Enter Name" required="required" />
<input class="frm-input" name="email" type="text" size="30" maxlength="80" placeholder="Enter Email" required="required"/>
<input class="frm-input" name="jobtype" type="text" size="30" maxlength="30" placeholder="Job Type" required="required"/>
<input class="frm-input" name="ent_type" type="text" size="30" maxlength="80" placeholder="Entity Type" required="required"/>
<input class="frm-input" name="tas_out" type="text" size="30" maxlength="80" placeholder="Task Outline" required="required"/>
<input class="frm-input" name="l_st" type="text" size="30" maxlength="80" placeholder="Logo style of interest (optional)" />
<textarea required="required" class="frm-input frm-txtarea" name="message" placeholder="Task Description!!" maxlength="1000" cols="25" rows="6" ></textarea>
<input style="float: left;" type="file" name="image" size="66"/>
<input type="submit" class="btn btn-success btn-lg" name="submitt" value="submit" style="float: right" />
</form>
In this file I am trying to get the form information and storing them in database.But this page is not loading after the form submission.
Code in insert.php is below:
<?php
/*
$name = "";
$text = "";
$post = "";
*/
//echo $name;
if (isset($_POST['submitt']))
{
$name = $_POST["name"];
$mail = $_POST["email"];
$j_type = $_POST["jobtype"];
$e_type = $_POST["ent_type"];
$task = $_POST["tas_out"];
$l_st = $_POST["l_st"];
$task_des = $_POST["message"];
$image_name=$_FILES['image']['name'];
$image_type=$_FILES['image']['type'];
$image_size=$_FILES['image']['size'];
$image_temp=$_FILES['image']['tmp_name'];
//$date = date(m-d-y);
echo $name;
echo $mail;
echo $j_type;
echo $e_type;
echo $task;
echo $l_st;
echo $task_des;
if ($image_type=='image/jpeg' || $image_type=='image/png' || $image_type=='image/gif') {
move_uploaded_file($image_temp, "img/$image_name");
}
$connection=mysqli_connect("localhost", "root", "","com");
$query="insert into details (name, mail, j_type, e_type, task_outline, l_style, task_desc, image) values('".$name."','".$mail."','".$j_type."','".$e_type."','".$task."','".$l_st."','".$task_des."','".$image_name."')";
if(mysqli_query($connection,$query)){
//include('test.php');
echo '<h2>Data submitted successfully!!</h2>';
header("refresh:1; url=login.php");
//echo 'Back';
}else{
echo "Data not Submitted!";
# code...
}
}
echo "Data not Submitted!";
?>
echo "Data not Submitted!"; // put this line inside the last bracket
Sorry it was my fault,there was a typo mistake in the form action.Everything else is fine.

2 different action in one form

I have a form called addcustomer.php . it's contain firsname, lastname, mobile fields.i have also 2 button on this form. onr button for saving data and the others for sending sms to customers.
i want to when i click second button 3 data fields passed to another form called smsinfo.php.
now action for saving data works good but when direct to smsinfo . there is no data on this form .
<form method ="POST" name = "custform" action="">
<div class="cell_2_2"><label class="lblfields">Firstname:</label> <input type="text" name="firstname" autocomplete="off"/></div>
<div class="cell_3_2"><label class="lblfields">Lastname :</label> <input type="text" name="lastname" autocomplete="off"/></div>
<div class="cell_5_2"><label class="lblfields">Mobile :</label> <input type="text" name="mobile" autocomplete="off"/></div>
<input type="submit" name="submit" value="Save"/>
<input type="submit" name="sendsms" value="Sms"/>
if (isset($_POST['submit']))
{
Saving code here
} else if (isset($_POST['sendsms'])) {
header("Location: smsinfo.php");
}
here code of smsinfo.php:
<?php
$Fname = $_REQUEST['firstname'];
$Lname = $_REQUEST['lastname'];
$Mob = $_REQUEST['mobile'];
?>
<html>
<body>
<form action="sendingsms.php" method="POST">
<input style="width: 100px;" name="firstname" type="text" value="<?php echo $firstname; ?>"/>
<input style="width: 100px;" name="lastname" type="text" value="<?php echo $lastname; ?>"/>
<input style="width: 100px;" name="mobile" type="text" value="<?php echo $mobile; ?>"/>
</form>
</body>
</html>
thanks all and sorry for my poor english

how to get data from an html form and use it to make an email with php?

I want to send an email from the browser, but my PHP knowledge and experience are not enough.
So I have a html form
<form>
<textarea value="Message" required></textarea>
<input type="text" value="Name" required>
<input type="text" value="Email" required>
<input type="text" value="subject" required>
<input type="reset" value="Reset" >
<input type="submit" value="Submit">
</form>
My question here is how to fill this php code so I can get data from the html form and then send the email.
$to = "my_email#example.com";
$name =
$message =
$from =
$headers =
mail($to,$name,$subject,$message,$headers);
You have to add an action and a method (POST or GET) in your form
<form action="yourpage.php" method="POST">
After that add a name attribute at all your input :
<input type="text" value="Name" name="name" required>
<input type="text" value="Email" name="mail" required>
In yourpage.php
Here the method was POST so :
$_POST['name']; //Here get the posted value in input named 'name'
$_POST['mail'];
You need to set the action in the form, and get the $_POST in PHP code, here is an example:
<form action="test.php">
<textarea value="Message" required></textarea>
<input type="text" name="Name" value="Name" required>
<input type="text" name="Email" value="Email" required>
<input type="text" name="subject" value="subject" required>
<input type="reset" value="Reset" >
<input type="submit" value="Submit">
</form>
//test.php file
<?php
$to = "my_email#example.com";
$name = $_POST['Name'];
$message = $_POST['Message'];
$from = 'test#test.com';
$headers = 'your headers';
mail($to,$name,$subject,$message,$headers);
?>

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>

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.

Categories