not able to add data from web page to database - php

this is my form .html file getting connected to adddatabase.php page .having firstname and secondname
<form action="addtodatabase.php" method="post">
<div class="container">
<form class="form-inline">
<fieldset>
<legend>Security Department User Registration</legend>
<div class="form-group">
<label for="Firstname">First Name</label>
<input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
</div>
<div class="form-group">
<label for="Secondname">Second Name</label>
<input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
</div>
</form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
addtodatabase.php page as follows .
if (isset($_POST)) {
$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';
echo 'Your first name is ' .$firstname. '<br>';
echo 'Your second name is ' .$secondname. '<br>';
}
mysqli_query($connect,"INSERT INTO form_details(firstname,secondname)
VALUES('$firstname','$secondname')");

You have form inside a form which is wrong. Do it like this:
<form method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
</form>
Also, put your submit button inside the form:
<form method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
<input type="submit" value="Submit" />
</form>
Or if the submit button is outside, set the form attribute of the button to the id of the form:
<form id="myForm" method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
</form>
<input type="submit" value="Submit" form="myForm" />

Related

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

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">

Link a php page to another php page

I am trying to link a make a login page using php without database but the link to the other page is not working . It is staying on the same page. No error message also.
Here is the php part of the code:
<?php
$error = "";
if(isset($_POST['MRDlogin']))
{
if(isset($_POST['username'],$_POST['password'])){
/*** You can change username & password ***/
$user = array(
"user" => "MRD",
"pass"=>"msit"
);
$username = $_POST['username'];
$pass = $_POST['password'];
if($username == $user['user'] && $pass == $user['pass']){
header("Location: http://localhost/portal/MRD.php");
}else{
$error = '<div class="alert alert-danger">Invalid Login</div>';
}
}
}
and my html form part is:
> <div class="panel-body">
> <?php echo $error; ?>
> <form accept-charset="UTF-8" role="form" method="post">
> <fieldset>
> <div class="form-group">
> <input class="form-control" placeholder="Username" name="username" type="text">
> </div>
> <div class="form-group">
> <input class="form-control" placeholder="Password" name="password" type="password" value="">
> </div>
> <input class="btn btn-lg btn-success btn-block" type="submit" value="Login">
> </fieldset>
> </form>
> </div>
The following variable cannot be found
$_POST['MRDlogin']
Add it in you button as shown below
<div class="panel-body">
<?php echo $error; ?>
<form accept-charset="UTF-8" action="page.php" role="form" method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="username" type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Login" name="MRDlogin">
</fieldset>
</form>
in form tag add action tag with value of page name that will process submited form:
<form action="page.php" accept-charset="UTF-8" role="form" method="post">

Bootstrap form does not send file name

I have wrote a form in bannervipsites.php :
<form role="form" action="index.php" method ="post">
<div class="form-group">
<label>نام بنر :</label>
<input class="form-control" name="sname" placeholder="نام فارسی یا لاتین بنر">
</div>
<div class="form-group">
<label>URL : </label>
<input class="form-control" name="surl" placeholder="http://">
</div>
<div class="form-group">
<label>Upload banner :</label>
<input type="file" name='upfile' id='upfile' class="filestyle" data-buttonName="btn-primary" data-buttonBefore="true" data-icon="false" >
<input type='hidden' name='bform' value='file'>
<input type='hidden' name='MAX_FILE_SIZE' value='100720'>
</div>
<?php
if ($acctype != 1) {
?>
<div class="form-group">
<label>حداکثر بازدید در ساعت : (عدد 0 یعنی نامحدود)</label>
<input class="form-control" name="scph" placeholder="سقف بازدید در ساعت از چه عددی فراتر نرود؟">
</div>
<?php
}
?>
<div class="form-group">
<label>روی مربع کلیک کنید : </label>
<div class="g-recaptcha"></div>
</div>
<input type="hidden" name="fform" value="bannervip">
<input type="hidden" name="sid" value="0">
<input id="submit" name="submit" value="اضافه کردن" class="btn btn-success" type="submit">
<button type="button" class="btn btn-default" data-dismiss="modal">بستن</button>
</form>
and then post it to index.php
but upfile (the name of uploaded file) does not send.
I can not get
echo $_FILES['upfile']['tmp_name'];
in index.php
what is the problem? it's very odd for me.
it was working in last template .(index.php is ok i'm sure)
what is wrong with bootstrap form
Add
enctype= "multipart/form-data"
to your form.

$_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">

$_POST not being read

i have a form which inserts book information into the database. however it is not reading the $_POST attribute.
book.php:
<form action="books_manage.php" id="addbook_form" method="post">
<div id="ab_wrapper">
<div id="ab_leftcolumn">
<div id="bookinfo">
<fieldset>
<legend>Book Details</legend>
<div class="field">
<label>Book ID</label>
<input type="text" name="bid" id="bid"/>
</div>
<div class="field">
<label>Name</label>
<input type="text" name="bname" id="bname"/>
</div>
<div class="field">
<label>Author</label>
<input type="text" name="bauthor" id="bauthor"/>
</div>
<div class="field">
<label>Info</label>
<textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
</div>
<div class="field">
<label>Date Added</label>
<input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
</div>
<div class="field">
<label>Date Updated</label>
<input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
</div>
<div>
<input type="hidden" name="action" value="save">
<input type="submit" value="Save">
<input type="button" id="addcontent" value="Add Content">
<input type="reset" value="Reset">
</div>
</fieldset>
</div>
</div>
<div id="ab_rightcolumn">
<div id="bookcontents">
<fieldset>
<legend>Book Content</legend>
<div class="field">
<label>Chapter</label>
<input type="text" id="bchapter" name="bchapter"/>
</div>
<div class="field">
<label>Sub-Chapter</label>
<input type="text" id="bsubchapter" name="bsubchapter"/>
</div>
<div class="field">
<label>Content</label>
<textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
</div>
<br />
<div>
<input type="hidden" name="action" value="addnext">
<input type="submit" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
</div>
</fieldset>
</div>
</div>
</div>
</form>
books_manage.php:
<?php
if (isset($_POST['action']) && $_POST['action'] == 'save')
{
echo "You clicked the save button";
}
else {
echo "Hello. The date is " . date("D d M Y") ;
}
?>
the output:
Hello. The date is Thu 08 Jul 2010
it seems it isn't reading the value of the hidden button. it should display "You clicked the save button". Am I missing something?
First of all, multiple <input>s in the same <form> with the same name attribute isn't going to get you the behavior you're looking for.
Instead, you need to provide a name to the submit buttons, and then you can check which button was pressed:
<input type="submit" name="save" value="Add Content">
<input type="submit" name="done" value="No more content">
<?php
if(isset($_POST['save'])) {
echo "saved";
} else if(isset($_POST['done'])) {
echo "done";
}
?>
See comment below by Lèse majesté to learn how the HTML working group effed this one up.
You have two inputs with the name "action" in the same form. Make sure your form field names are unique.
Don't forget you can organise your names using this syntax -
<input name="form1['name']" value="".....
<input name="form2['name']" ..... etc
Then access these variables like this:
$_POST['form1']['name']...
Very useful!
Its becuase you have defined action 3 times
<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">
Do the followin on your books_manage.php
echo "<pre>";
print_r($_POST);
echo "</pre>";
You will see where you are going wrong.
<input type="hidden" name="action" value="addnext">
<input type="submit" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
you have two inputs with name "action". the action you get is probably "Done", not "save"
You have multiple inputs named action you will get
<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">
You need to remove the hidden variables and change the name of your first 'submit' to 'action'
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Done">
You need to name your submit button "action" and use the value of that button to determine the action. Your code basically has two action form values and the last one is what takes precedence.
<form action="books_manage.php" id="addbook_form" method="post">
<div id="ab_wrapper">
<div id="ab_leftcolumn">
<div id="bookinfo">
<fieldset>
<legend>Book Details</legend>
<div class="field">
<label>Book ID</label>
<input type="text" name="bid" id="bid"/>
</div>
<div class="field">
<label>Name</label>
<input type="text" name="bname" id="bname"/>
</div>
<div class="field">
<label>Author</label>
<input type="text" name="bauthor" id="bauthor"/>
</div>
<div class="field">
<label>Info</label>
<textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
</div>
<div class="field">
<label>Date Added</label>
<input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
</div>
<div class="field">
<label>Date Updated</label>
<input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
</div>
<div>
<input type="submit" name="action" value="Save">
<input type="button" id="addcontent" value="Add Content">
<input type="reset" value="Reset">
</div>
</fieldset>
</div>
</div>
<div id="ab_rightcolumn">
<div id="bookcontents">
<fieldset>
<legend>Book Content</legend>
<div class="field">
<label>Chapter</label>
<input type="text" id="bchapter" name="bchapter"/>
</div>
<div class="field">
<label>Sub-Chapter</label>
<input type="text" id="bsubchapter" name="bsubchapter"/>
</div>
<div class="field">
<label>Content</label>
<textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
</div>
<br />
<div>
<input type="submit" name="action" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
</div>
</fieldset>
</div>
</div>
</div>
<?php
if (isset($_POST['action']) && $_POST['action'] == 'Save')
{
echo "You clicked the save button";
}
else if (isset($_POST['action']) && $_POST['action'] == 'Save and Add Next Chapter')
{
echo 'You clicked the "Save and Add Next Chapter" button';
}
else if (isset($_POST['action']) && $_POST['action'] == 'Done')
{
echo 'You clicked the done button';
}
else
{
echo "Hello. The date is " . date("D d M Y") ;
}
?>

Categories