$_POST array not populated by form submission - php

I am working on an email subscribe feature on my website. I encountered an issue where the php is not getting the form values. I have looked at numerous threads on this issue and have not had any luck.
So, how can I get the value of the email field in php?
<div>
<form action="/email.php">
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" value="Submit">
</form>
</div>
Email.php
<?php
$email = $_POST["email"];
echo $email;
?>

don't use / before email.php and you forgot method type
<div>
<form action="email.php" method='POST'>
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" value="Submit">
</form>
</div>

add form method Attribute get or post
<form action="/email.php" method = "POST">

Try this way,
HTML:
<form action="email.php" method='POST'>
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" name="save" value="Submit">
</form>
PHP:
<?php
if(isset($_POST["save"]))
{
print_r($_POST);
echo $_POST["email"];
}
?>
This will helpful to you.
Regards

Every thing written perfectly, just fault's in form tag, remove "/" from action and give method="POST" in form tag.
As you are calling that value with post method in email.php, you have to give the post method on form
like this:-
<div>
<form action="email.php" method="POST">
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" value="Submit">
</form>
</div>
rest is all fine
now you can call that value:-
<?php
$email = $_POST["email"];
echo $email;
?>

Related

Run PHP file (PHPMailer to send email through Gmail) in the background and alert on the same page when the email is sent

I am using PHPMailer to send email from my Gmail account to an email from a submitted form on my index.html page. This works perfectly fine. But since I am using the send.php file in the action attribute, the send.php page appears after form submission. I want to stay on the index.html page and let send.php work in the background and alert on the index.html page, when the email is sent.
Note: There isn't any SQL or any database job here. I am using PHPMailer in send.php to send an email from my Gmail account to the user submitted Gmail id in the form.
<form action="send.php" class="form" id="frm-js" method="POST">
<div class="form__group">
<input type="text" class="form__input" id="name" name="name" placeholder="Full Name" required>
<label for="name" class="form__label">Full Name</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="email" name="email" placeholder="Email Address" required>
<label for="email" class="form__label">Email Address</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="phone" name="phone" placeholder="Phone Number">
<label for="phone" class="form__label">Phone Number</label>
</div>
<div class="form__group">
<textarea id="message" rows="4" cols="50" name="message" class="textarea-frm form__input" maxlength="1000" placeholder="Your message"></textarea>
</div>
<div class="form__group">
<button class="btn" type="submit" name="submit">Book</button>
</div>
</form>
You can use jQuery. Try this: remove action from the form tag. I add a p to show the message from send.php:
<form class="form" id="frm-js" method="POST">
<p id="resp_msg"></p>
<div class="form__group">
<input type="text" class="form__input" id="name" name="name" placeholder="Full Name" required>
<label for="name" class="form__label">Full Name</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="email" name="email" placeholder="Email Address" required>
<label for="email" class="form__label">Email Address</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="phone" name="phone" placeholder="Phone Number">
<label for="phone" class="form__label">Phone Number</label>
</div>
<div class="form__group">
<textarea id="message" rows="4" cols="50" name="message" class="textarea-frm form__input" maxlength="1000" placeholder="Your message"></textarea>
</div>
<div class="form__group">
<button class="btn" type="submit" name="submit">Book</button>
</div>
</form>
And add a jQuery function (integrate jQuery if you haven't already library):
$("#frm-js").on("submit", function(event){
event.preventDefault();
var data = $( this ).serialize();
$.post("send.php",{data:data }, function (data) {
// Do other stuff like
// reset form items
// Show message response
$("#resp_msg").text(data).show();
});
});
This is send.php and is how it should be:
if(isset($_POST['data'])) {
$params = array();
parse_str($_POST['data'], $params);
// Send mail
echo "Hi, " . $params['name'] . " your email has been sent";
}

Run php file when submit button in another html file

I would like to work on registration process for which with the help of Google and Youtube i have created "Sign-in & Sing-up" page togather with toggle option however unable to run registration.php file once user provide registration info at login.html file. Codes are as follows :
<form id="login" class="input-group">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<button type="submit" class="submit-btn">Sign-In</button>
</form>
<form Id="register" class="input-group">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="email" class="input-field" placeholder="Email Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="password" class="input-field" placeholder="Confirm Password" required>
<input type="phone-number" class="input-field" placeholder="Mobile Number" required>
<input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
<button type="submit" class="submit-btn">Sign-Up</button>
</form>
How to execute registration.php file when Sign-up button clicked at login.html file? Same goes for login option too.
Make sure to add method="POST" and action="path/function.php" and name="desiredName"in your forms like this:
<form id="login" class="input-group" method="POST" action="file_path/login.php">
<input type="text" class="input-field" placeholder="User Id" name ="user" required>
<input type="password" class="input-field" placeholder="Enter Password" name="password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<button type="submit" class="submit-btn">Sign-In</button>
</form>
And then in PHP to "catch" the data from the post, you'll use something like this:
$this->getpost['user'];
Or
$_POST['user'];
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
check here for more details w3schools
<form id="login" class="input-group" method="POST" action="file_path/login.php">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<input type="submit" class="submit-btn" value="Sign-In">
</form>
<form Id="register" class="input-group" method="POST" action="file_path/register.php">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="email" class="input-field" placeholder="Email Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="password" class="input-field" placeholder="Confirm Password" required>
<input type="phone-number" class="input-field" placeholder="Mobile Number" required>
<input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
<input type="submit" class="submit-btn" value="Sign-Up">
</form>
EDIT according to your comment
replace button with input type="submit"
and place # before the php variable so you wont get undefined error notice(# is used to avoid error notice)
<div class="header">
<h2>Register here</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo #$username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo #$email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm Password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<label>Mobile number</label>
<input type="number" name="mobile" value="<?php echo #$mobile; ?>">
</div>
<div class="input-group">
<input type="submit" class="btn" name="reg_user" value="Sign-Up">
</div>
</form>
create register.php file
and in register.php
<?php
$con = mysqli_connect("localhost", "username", "password", "dbname") or trigger_error("Unable to connect to the database");
if(isset($_POST['reg_user'])){
$name = $_POST['username']; //here "username" is what you defined in the "name" field of input form
//define other variables
//write your own sql query , here is an example
$query = "INSERT INTO table(name) VALUES(?)";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt,$query)){
echo "Error";
}else{
mysqli_stmt_bind_param($stmt,"s",$name);
mysqli_stmt_execute($stmt);
}
}
?>

PHP - dynamic fields array not

I have a form with dynamic fields. When I add a dynamic field and do a var_dump of that field, I am getting only the first result.
Form:
<div class="form-group halltype">
<label class="col-sm-2 col-sm-2 control-label">HallType</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="title1[]" placeholder="Main Title"><br />
<input type="text" class="form-control" name="title2[]" placeholder="Title 2"><br />
<input type="text" class="form-control" name="seating[]" placeholder="Seating Capacity"><br />
<input type="text" class="form-control" name="floating[]" placeholder="Floating Capacity"><br />
</div>
</div>
<div class="form-group addhalltype">
<label class="col-sm-2 col-sm-2 control-label"></label>
<div class="col-sm-10">
<input type="button" class="btn btn-info" id="">Add Hall Type</button>
</div>
Jquery:
$(".addhalltype").click(function() {
$halltype = '<div class="form-group halltype"><label class="col-sm-2 col-sm-2 control-label">HallType</label><div class="col-sm-10"><input type="text" class="form-control" name="title1[]" placeholder="Main Title"><br /><input type="text" class="form-control" name="title2[]" placeholder="Title 2"><br /><input type="text" class="form-control" name="seating[]" placeholder="Seating Capacity"><br /><input type="text" class="form-control" name="floating[]" placeholder="Floating Capacity"><br /></div></div>';
$($halltype).insertBefore(".addhalltype");
});
and if I do var_dump($_POST['title1']), I get
array(1) {
[0]=>
string(4) "1212"
}
you are printing $_POST['title1'] in var_dump it will show you only value that u inserted for name="title1[]".
Here you are using dynamic array but with different name ie name equal to title1[] title1[] seating[] and floating[].
if you want to get all the post values in one name like title[0],title[1],...title[n] to get the result in this format you have to write your above code with this format.
<input type="text" class="form-control" name="title[]" placeholder="Main Title"><br />
<input type="text" class="form-control" name="title[]" placeholder="Title 2"><br />
<input type="text" class="form-control" name="title[]" placeholder="Seating Capacity"><br />
<input type="text" class="form-control" name="title[]" placeholder="Floating Capacity"><br />

unable to insert data into wordpress db using form

When i click on submit it doesn't create new posts in the posttype. It just refreshes the page.The below is a pagetemplate assigned to page.
<?php
if(isset($_POST['submit'])){
global $wpdb;
$wpdb->insert( 'contacts', array('post_title'=>$_POST['name'],'email'=>$_POST['email']));
}
?>
<form method="post" action="">
<input type="name" id="inputName" placeholder="Name">
<input type="email" id="inputEmail" placeholder="Email">
<button type="submit">SUBMIT</button>
</form>
You need to add name attribute in your form element
<input type="name" id="inputName" name="name" placeholder="Name">
<input type="email" id="inputEmail" name="email" placeholder="Email">
<button type="submit" name ="submit">SUBMIT</button>
You just need to change the HTML
<form method="post" action="">
<input type="text" id="inputName" placeholder="Name" name="name">
<input type="text" id="inputEmail" placeholder="Email" name="email">
<button type="submit" name="submit">SUBMIT</button>
</form>
<form method="post" action="">
<input type="text" id="inputName" placeholder="Name" name="username">
<input type="text" id="inputEmail" placeholder="Email" name="useremail">
<input type="submit" name="submit" value="submit">
</form>
<?php if(isset($_POST['submit'])){
global $wpdb;
$wpdb->insert( 'contacts', array('post_title'=>$_POST['username'],'email'=>$_POST['useremail']));} ?>
Now Check. I think it's working properly.

PHP $_POST vars empty with Bootstrap Forms

I'm having trouble figuring out what is wrong here. I have Bootstrap 3.0.
My form.php
<form role="form" action="../../sgm/leadinsert" method="post">
<div class="form-group">
<label for="fName">First Name</label>
<input type="text" class="form-control" id="fName" name="fName" placeholder="Enter first name" />
</div>
<div class="form-group">
<label for="lName">Last Name</label>
<input type="text" class="form-control" id="lName" name="lName" placeholder="Enter last name" />
</div>
<div class="form-group">
<label for="pNo">Phone Number</label>
<input type="text" class="form-control" id="pNo" name="pNo" placeholder="Ex: 555-555-5555" />
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#gmail.com" required />
</div>
<div class="form-group">
<label for="comm">Comments</label>
<textarea class="form-control" rows="3" id="comm" name="comm" placeholder="Enter any comments here; max size 500 chars."></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-default" value="Next"/>
</div>
</form>
Here is my leadinsert.php
<?php
$con=mysqli_connect("localhost","user","pass","db");
$fName = $_POST[ "fName" ];
$lName = $_POST[ "lName" ];
$pNo = $_POST[ "pNo" ];
$email = $_POST[ "email" ];
$comm = $_POST[ "comm" ];
$sql="INSERT INTO Leads (fName, lName, pNo, email, comm)
VALUES ('$fName', '$lName', '$pNo', '$email', '$comm')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con) . print_r($_POST));
}
echo "1 record added";
mysqli_close($con);
?>
Note: on my action= I have my .htaccess set to interpret that. I have never had a problem getting forms to submit to my db, until I implemented bootstrap. It is submitting to my db table, but it is an empty row.
Installing Bootstrap shouldn't play any effect onto how you process forms, since Bootstrap is simply a template of css and javascript. I would place some checks in your insert.php such as issets and see if there is actually any information in the posts. You can also check this in the Chrome Developer Console.
in your action you have this file
action="../../sgm/leadinsert"
while you provide insert.php ?
you should have this
action="path/insert.php"
and also you forgot value = '' in all your inputs , write them like that:
<input type="text" value="" class="form-control" id="fName" name="fName" placeholder="Enter first name" />
Please correct you from action then it will work
missing </div> and the action as already said
<div class="form-group">
<input type="submit" name="submit" class="btn btn-default" value="Next"/>
</div>
</form>

Categories