PHP form is falling through the first if($_POST) [duplicate] - php

I am using Twitter Bootstrap to build a web application. I have a form where user should enter his name and last name and click submit. The problem is, that the $_POST comes back empty.
I am using WAMP on Windows 8 machine.
<?php
if(isset($_POST["send"])) {
print_r($_POST)
} else {
?>
<form class="form-horizontal" action="" method="post">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<button type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>

Add names to the input types like
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
Now check the $_POST variable.

Your inputs don't have name attributes. Set those:
<input type="text" id="inputName" placeholder="First name" name="first_name" />
<input type="text" id="inputLastname" placeholder="Last name" name="last_name" >
Also, look at the way you detect a form submission:
if(isset($_POST['send']))
You check if the submit button is present in the post data. This is a bad idea because in the case of Internet Explorer, the submit button won't be present in the post data if the user presses the enter key to submit the form.
A better method is:
if($_SERVER['REQUEST_METHOD'] == 'POST')
Or
if(isset($_POST['first_name'], $_POST['last_name']))
More info - Why isset($_POST['submit']) is bad.

The problem is that you're checking if $_POST['send'] is set, but it won't be. The "send" field in your form is the button, but the button doesn't have a value, so it won't be included in $_POST.
Hope that helps.

<?php
if(isset($_POST["send"])) {
echo $_POST["inputName"];
echo $_POST["inputLastname"];
}
else {
?>
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name" name="inputName">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<input type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>

I have notice that using 'name' attribute doesn't work well with Bootstrap Validator for (if(isset($_POST["send"]))).So in case if you like to add validation on your form then I recommend you to follow this way!
Solution
<button type="submit" class="btn btn-primary">Submit data</button>
php
if($_SERVER['REQUEST_METHOD'] == 'POST')

Add the location of the file in action or use:
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">

Related

My form is shifting to a backend page in php after clicking on Submit

I am clicking on Submit button my form is getting submitted. I just wanted to submit the details, but not to visit the backend page or popup in it that the data have been submitted.
<form action="functionality\app_details.php" method="post">
<!-- Name -->
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Name</span>
</div>
<input type="text" name="name" class="form-control" id="inputName" placeholder="Enter name">
</div>
<!-- Parent's name -->
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Parent's Name</span>
</div>
<input type="text" name="Fname" id="inputFname" class="form-control" placeholder="Enter Father's Name">
<input type="text" name="Mname" id="inputMname" class="form-control" placeholder="Enter Mother's Name">
</div>
<button type="submit" class="btn btn-outline-danger">submit</button>
</form>
</div>
if you want to just submit it and nothing happen. just leave the action empty so it won't go anywhere.
you can also do this if you want the page not to refresh:
document.getElementById("enter you button id here").addEventListener("click", function(event){
event.preventDefault()
});
The data is sent to the page you put in action tag..
If you want to send the data to the same page
<form action="" method="post">
...
</form>
or use Ajax.

Form within a form in HTML?

I created an application form with HTML and on that application the user has to upload his resume. I was thinking to create a form within the form to let the user upload his resume such as:
<div class="application-form">
<form class="contact-form" action="application-form.php" method="post">
<div class="application-data">
<label>First Name</label>
<input type="text" name="first">
</div>
<div class="application-data">
<label>Last Name</label>
<input type="text" name="last">
</div>
<div class="application-data">
<label>Email</label>
<input type="email" name="email">
</div>
<div class="application-data">
<label>Phone Number</label>
<input type="phone" name="phone">
</div>
<div class="resume">
<form action="">
<label>Resume</label>
<input type="file" name="resume">
<button type="submit">Submit Resume</button>
</form>
</div>
</form>
</div>
Seems this is not possible in HTML. What is the best alternative? thanks
I don't see the need for the nested form here...just remove it and make the file upload part of the application-form.php handler. Also add the enctype attribute to your <form> to accept files.
<div class="application-form">
<form class="contact-form" action="application-form.php" method="post" enctype="multipart/form-data">
<div class="application-data">
<label>First Name</label>
<input type="text" name="first">
</div>
<div class="application-data">
<label>Last Name</label>
<input type="text" name="last">
</div>
<div class="application-data">
<label>Email</label>
<input type="email" name="email">
</div>
<div class="application-data">
<label>Phone Number</label>
<input type="phone" name="phone">
</div>
<div class="resume">
<label>Resume</label>
<input type="file" name="resume">
<button type="submit">Submit Resume</button>
</div>
</form>
</div>

php-How to have a button submit and send to page

I'm trying to make a button that submits order input to a database and then takes the user to the home page on my site, but I don't know how to do it. Here is my code:
<button type="submit" class="btn btn-primary" onclick="window.location.href='PurimHome.html'"> Place Order</button>
This button sends the info to the db but it doesn't go to the home page.
Here is more of the code:(I'm using bootstrap)
<form class="form-horizontal" action="" method="post">
<label class="col-sm-2" >Credit Card Number</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="creditnum" name="creditnum" placeholder="Credit Card Number" >
</div>
</div>
<div class="form-group">
<label class="col-sm-2" >Expiration Date</label>
<div class="col-sm-10">
<input type="date" class="form-control" id="date" name="date" placeholder="Expiration Date" >
</div>
</div>
<div class="form-group">
<label class="col-sm-2" >Security Code</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="code" name="code" placeholder="Three Digit Code" >
</div>
</div>
<input type="checkbox" name="contact" id="contact" value="Phone"> Contact Me by Phone<br>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" onclick="window.location.href='PurimHome.html'"> Place Order</button>
</div>
</div>
</form>
If you want to relocate to a different site after you submit.. then maybe you should place in your controller something like..
if(isset($_POST['submit'])){
//do stuff if needed
header('Location: url');
}
so that after you submit the form it will then redirect you to antoher location of your choosing.

How send data one page to another page after clicking submit button

i Have a one problem want to share with all of you.
I am develop a custom page in WordPress here is the link of the page http://muhammadimran.info/testing/ when i try to click on new document button and try to submit form it give me error. Here is my code for this please tell me how i can give link in WordPress in action
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<form action="home1.php" enctype="multipart/form-data" method="POST">
<div class="form-group">
<label for="name1">Name</label>
<input name="name" class="form-control" type="text" />
</div>
<div class="form-group">
<label for="title">Title</label>
<input name="title" class="form-control" type="text" />
</div>
<div class="form-group">
<label for="Startdate">Start Date</label>
<input id="datepicker" class="form-control" class="datepicker" type="text" name="start" required="" />
</div>
<div class="form-group">
<label for="Enddate">End Date</label>
<input id="datepicker1" class="form-control" class="datepicker" type="text" name="end" required="" />
</div>
<div class="form-group">
<label for="Users">Users</label>
<input type="text" class="form-control" name="users" />
</div>
<div class="form-group">
<label for="Fileupload">File Upload</label>
<input type="file" class="form-control" name="file" class="file" name="file" required="">
</div>
<input type="submit" id="submit" class="btn btn-primary submitbutton" name="submit">
</form>
</div>
</div>
You should follow the beloe steps:
create a Template:
Home1 Template with file name home1.php
create a page and select the recently create template (Home1 Template).
replace the form action
<form action="home1.php" enctype="multipart/form-data" method="POST">
with
<form action="http://siteur/templateur/" enctype="multipart/form-data" method="POST">
in home1.php
print_r($_POST);

$_POST is empty after form submit

I am using Twitter Bootstrap to build a web application. I have a form where user should enter his name and last name and click submit. The problem is, that the $_POST comes back empty.
I am using WAMP on Windows 8 machine.
<?php
if(isset($_POST["send"])) {
print_r($_POST)
} else {
?>
<form class="form-horizontal" action="" method="post">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<button type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>
Add names to the input types like
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
Now check the $_POST variable.
Your inputs don't have name attributes. Set those:
<input type="text" id="inputName" placeholder="First name" name="first_name" />
<input type="text" id="inputLastname" placeholder="Last name" name="last_name" >
Also, look at the way you detect a form submission:
if(isset($_POST['send']))
You check if the submit button is present in the post data. This is a bad idea because in the case of Internet Explorer, the submit button won't be present in the post data if the user presses the enter key to submit the form.
A better method is:
if($_SERVER['REQUEST_METHOD'] == 'POST')
Or
if(isset($_POST['first_name'], $_POST['last_name']))
More info - Why isset($_POST['submit']) is bad.
The problem is that you're checking if $_POST['send'] is set, but it won't be. The "send" field in your form is the button, but the button doesn't have a value, so it won't be included in $_POST.
Hope that helps.
<?php
if(isset($_POST["send"])) {
echo $_POST["inputName"];
echo $_POST["inputLastname"];
}
else {
?>
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name" name="inputName">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<input type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>
I have notice that using 'name' attribute doesn't work well with Bootstrap Validator for (if(isset($_POST["send"]))).So in case if you like to add validation on your form then I recommend you to follow this way!
Solution
<button type="submit" class="btn btn-primary">Submit data</button>
php
if($_SERVER['REQUEST_METHOD'] == 'POST')
Add the location of the file in action or use:
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">

Categories