This is my html form and php file i have to insert my budget dropdown values by select into database
i have to select values from budget and selected value can insert in database
and my data is inserting in my database when i click twice on submit button
<form action="insert.php" method="post" id="register-form" onsubmit=" return add();">
<div class="label"> Name</div><input type="text" id="name" name="name" /><br />
<div class="label">Email</div><input type="text" id="email" name="email" /><br />
<div class="label">Phone Number</div><input type="text" id="phone" name="phone" /><br />
<div class="label">budget</div>
<select id="budget" name="budget">
<option value="">select</option>
<option value="1">0-100</option> <!-- first option contains value="" -->
<option value="2">100-200</option>
<option value="3">200-300</option>
</select>
<br /><br />
<input type="submit" onclick="add()" name="submit" />
<div id="message"></div>
</form>
insert.php
<?php
include("config.php");
if(isset($_POST['submit'])){
$name=$_POST["name"];
$email=$_POST["email"];
$phone=$_POST["phone"];
$budget=$_POST["budget"];
$insert_query="insert into form(name,email,phone,budget) values ('$name','$email','$phone','$budget')";
$con=mysql_query($insert_query);
}
?>
Do the following:
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
Ensure that your field in the database is string/varchar
Re-visit the add() as well. That might be the cause for inserting successfully after the second click.
dropdown pass option value in variable so change the option value which you want to insert in database
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
if u want to select multiple option u use
<select id="budget" name="budget" multiple="multiple">
The $_POST["budget"]; is taking the value that is in the option, in this case the value for "0-100" is 1. If you want the value to be stored in the database use "0-100" as the value.
As far as the insert problem goes, try using this syntax:
$budget=$_POST['budget'];
$query = "INSERT INTO users ( database_budget )
VALUES ('$budget');";
the top value being the value in the database and the bottom value being the post value.
This is assuming that you're connecting to the database correctly. Also use single quotes for post values not double quotes.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have basic login form :
....
<input type="text" required autocomplete="off" name="user_name"/>
<input type="password" required autocomplete="off" name="password"/>
....
But I want user choose username from section option instead of input username from keyboard. So I make a select option form
<div class="col-md-2">
<select name="Grade" id="Grade"
data-live-search="true" class="chosen selectpicker form-control" required>
<option value="">Select Grade</option>
</select>
</div>
<div class="col-md-1 col-sm-12" id="lable1"><id="lable1">user</div>
<div class="col-md-2">
<!--select username-->
<select class="selectpicker form-control" name="user" id="user" autofocus="autofocus" required>
<option value="">Select an Option</option>
</select>
<!--select username-->
</div>
How can I hanlde data from select option form instead of input username to
<input type="text" required autocomplete="off" name="user_name"/>
Accessing HTML <form> elements in PHP
All HTML elements in a HTML form can access by PHP with their name with these variables:
$_POST['NAME'] or $_GET['NAME']
GET or POST depends on your form action.
And the value of these Variables are the value attribute of the HTML element
As described in form.guide Handling select box (drop-down list) in a PHP form
This is an example for select element
HTML :
<p>
What is your Gender?
<select name="formGender">
<option value="">Select...</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</p>
PHP :
<?php
if(isset($_POST['formSubmit']) ) //IF FORM SUBMIT IS PRESSED
{
$varMovie = $_POST['formMovie']; // Other form inputs
$varName = $_POST['formName']; // Other form inputs
$varGender = $_POST['formGender']; //Our Select element
$errorMessage = "";
// - - - snip - - -
}
?>
In your example :
HTML :
<!--select username-->
<select class="selectpicker form-control" name="user" id="user" autofocus="autofocus" required>
<option value="user1">USER ONE</option>
<option value="user2">USER TWO</option>
</select>
<!--select username-->
And you can use in your PHP:
if(isset($_POST['user']) ){
$name=$_POST['user']; //user1 if USER ONE selected or user2 if second one...
}
I'm working on something that is likely simple, but boggling my mind, as I'm relatively new to PHP and am trying to fine tune my skills.
So basically what I'm looking to have done is to have a form where a user inputs information into text boxes, picks something from a drop down list, and also chooses yes or no in a radio button, and submits.
Once the submission happens, where I'm stuck is the following:
A) What do I type in my "create.php" code that puts the information from the text boxes, selection from the drop down list, and choice picked in the radio button into the database? I know how to get the text area characters into it, and my sample code below reflects that, but how do options selected from a drop down list and radio button fit in?
and B) I am hoping that each inputted set of values each have an ability to be deleted by ONLY myself or the user that inputted it. Basically, create a password for each entry.
I'm now pasting my code. First, here is the form that is in the HTML document:
<div id="post form"><form action="create.php" method="post">
Your Information<br />
Name <input type="text" name="name" ><br />
E-mail <input type="text" name="email" ><br />
Phone <input type="text" name="phone" ><br />
<br />Other Information<br />
Location <input type="text" name="location" ><br />
Age <input type="text" name="age" ><br />
Budget <input type="text" name="price" ><br />
<br />Number of Cars
<select name="formCars">
<option value="">Select...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
<br />Number of Bicycles
<select name="formBicycles">
<option value="">Select...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
<br />What You Drive
<select name="formType">
<option value="">Select...</option>
<option value="C">Car</option>
<option value="T">Truck</option>
<option value="M">Motorcycle</option>
<option value="V">Van</option>
</select><br />
<br />Are You Happy With It?
<input type="radio" name="fix" value="Yes" />Yes
<input type="radio" name="fix" value="No" />No
<br />
<br /><input type="submit" name="Submit" value="Post" /><br />
</form>
</div>
Here is the PHP code I've got so far:
<?php
echo "<a href='index.html'><img src='header.png' border=0></a>";
$con = mysql_connect("localhost","123456","sampleTable");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("123456", $con);
$sql = "CREATE TABLE Information
(
name varchar(50),
email varchar(50),
phone varchar(10),
location varchar(50),
age varchar(50),
price varchar(50),
)";
mysql_query($sql,$con);
mysql_select_db("123456", $con);
$sql="INSERT INTO information (name, email, phone, location, age,
price)
VALUES
('$_POST[name]','$_POST[email]','$_POST[phone]','$_POST[location]',
'$_POST[age]','$_POST[price]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<br />";
echo "Thanks for posting!";
echo "<br />";
echo "<p><a href='index.html'>Back</a>";
mysql_close($con);
?>
Thank you all so much for your help! Again, newbie-ish here, so detailed help would be greatly appreciated!
Here is some information. Do you know how can you check your post data ?
<?php
// After post you can use dump functions
var_dump($_POST);
// or you can use array export for print
print_r($_POST);
?>
You will see input type text, select option value or input radio values same data structure. But if they are not array definition. For example;
<form .... method="POST">
<input type="checkbox" name="cars[]" value="audi">
<input type="checkbox" name="cars[]" value="vw">
</form>
Cars will be array because name has []
Second question a little interesting. Because you don't need a password for every entity normally. When you use user session, you can use only user id as a field in your form table. If users has a session ( logged ) and his user id same with form data record id you can show delete button.
Here is a user management sample here https://daveismyname.blog/login-and-registration-system-with-php
By the way you don't need the create table for every request for your php file.
Another important advice is do not use mysql_* functions. You can use mysqli_* functions or you can use PDO.
I have a following form in my HTML code.
When I forget to fill in the surname,
the form will show me a "bubble message" You have to fill in the surname, then I
will fill in the surname, press the submit button and the values are submitted.
When I forget to change the default option from <select> the form will show
me a "bubble message" You have to choose something, then I will choose another option,
press the submit button, but the values are not submitted. I always have to refresh
the page. Do you know where is the problem and how to achieve it without refreshing?
<form action="add" method="post">
...
<select id="choice" name="choice"
required oninvalid="setCustomValidity('You have to choose something')"
oninput="setCustomValidity('')">
<option value="" selected="selected"></option>
<option value="first">first</option>
<option value="second">second</option>
</select>
...
<input type="text" name="surname" id="surname" value=""
required oninvalid="setCustomValidity('You have to fill in the surname')"
oninput="setCustomValidity('')">
...
</form>
There is no problem with the form data submitting as you will see from this jsfiddle example:
http://jsfiddle.net/vbnh1a4y/
<form action="/echo/html" method="post">
<select id="choice" name="choice">
<option value="" selected="selected"></option>
<option value="first">first</option>
<option value="second">second</option>
</select>
<input type="text" name="surname" id="surname" value="" />
<input type='submit' />
</form>
I am totally new in PHP, in fact the reason I am doing this is to customize a wordpress plugin so it can fit my need. So far I have a default form, what I am doing now is to add a country dropdown. Here's how I add it
<div class="control-group">
<label class="control-label" for="Country">Country :</label>
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
</div>
So far I only able to retrieve the value of selected item with
$cscf['country'];
How can I get the display text which is the country name in this case ?
You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<input type="hidden" name="country" id="country_hidden">
<script>
$(document).ready(function() {
$("#itemType_id").change(function(){
$("#country_hidden").val(("#itemType_id").find(":selected").text());
});
});
</script>
Then when your page is submitted, you can get the name of the country by using
$_POST["country"]
<select name="text selection" onchange="getText(this)">
<option value="">Select text</option>
<option value="1">my text 1</option>
<option value="2">my text 2</option>
</select>
First put a java script function on the attribute "onchange" of the select.
Then, create your function that will transfer the text in a text box by using getElementById
<script>
function getText(element) {
var textHolder = element.options[element.selectedIndex].text
document.getElementById("txt_holder").value = textHolder;
}
</script>
Then create a temporary holder:
<input type="" name="txt_holder" id="txt_holder"> type should be hidden
Then assign it in a PHP variable:
$variableName=$_POST['txt_holder'];
Try this
<?php
if(isset($_POST['save']))
{
$arrayemail = $_POST['cscf'];
$mail = $arrayemail['country'];
$explode=explode('#',$mail);
// print_r($explode);
if(isset($explode[0]))
echo ucfirst($explode[0]);
}
?>
<form method="post">
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
<div class="controls">
<input type="submit" name='save' value="Save"/>
<span class="help-inline"></span>
</div>
</form>
Try it like,
<?php
if(isset($_POST['save']))
{
//Let $_POST['cscf']['country']= malaysia#email.com
// you can explode by # and get the 0 index name of country
$cnt=explode('#',$_POST['cscf']['country']);
if(isset($cnt[0]))// check if name exists in email
echo ucfirst($cnt[0]);// will echo Malaysia
}
?>
<form method="post">
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
<div class="controls">
<input type="submit" name='save' value="Save"/>
<span class="help-inline"></span>
</div>
</form>
I'm trying to pass two variables (a search query (String) and a column name (also a String)) via a HTML form. The first string is entered into a text box and the second is selected from a drop-down menu.
Obviously this method doesn't work:
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You need to give your <input> field a name in order for this to work. Unnamed inputs won't get passed through the form post.
You don't have a 'name' parameter on your sString input boxes. The name parameter is what gets sent back to the server, not the field's ID:
<input type="text" id="sString" name="sString">
and then in PHP
$search = $_GET['sString'];
You need to use attribute name instead of value.
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString" name="sString"/>
<select name="sField">
<option name="Name">Name</option>
<option name="HostID">Host ID</option>
<option name="OrderNumber">Order Number</option>
<option name="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
Try:
<form action="search.php" method="get">
<input type="text" name="sString" id="sString" value="">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You should be able to access form entered variables by $_GET array in search.php. To see what is going on try var_dump($_GET);