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...
}
Related
I need when user not select any database value option tag print error color in to parent div. I used that code for <input type="text"> html tag code to print text field pattern not matching time printing error. code is below
<label class="col-md-3 control-label">Product Title</label>
<div class="col-md-6">
<input type="text" name="product_title" class="form-control" required
placeholder="Product Name" value="" pattern="([A-z0-9À-ž\s]){2,}"
oninvalid="setCustomValidity('Please enter least two characters long name')"
onblur="this.parentNode.parentNode.style.backgroundColor=/^([A-z0-9À-ž\s]){2,}/.test
(this.value)?'inherit':'orange'" >
</div>
That is done for me onblur event.
Then now i want print error color when user not select any database value option in <select></select>tag. this is the default value i given. <option>Select a Manufacturer</option> I need print error when user select this or forget select database values in that <option> tag.
This is the code section
<div class="form-group"> <!-- form-group Start -->
<label class="col-md-3 control-label"> Select a Manufacturer </label>
<div class="col-md-6">
<select class="form-control" name="manufacturer" required="required">
<option>Select a Manufacturer</option>
<?php
$get_manufacturer ="select * from manufactures";
$run_manufacturer = mysqli_query($con, $get_manufacturer);
while($row_manufacturer = mysqli_fetch_array($run_manufacturer)){
$manufacturer_id = $row_manufacturer['manufacturer_id'];
$manufacturer_title = $row_manufacturer['manufacturer_title'];
echo "<option value='$manufacturer_id'>$manufacturer_title</option>";
}
?>
</select>
</div>
How can i do this?
Just do<option value="">Select a Manufacturer</option>.
Please see next link for reference: https://www.w3schools.com/tags/att_select_required.asp
I use J-Query here my code
<select class="form-control" name="manufacturer" required="required" onchange="getval(this);">
<option value="0">Select a Manufacturer</option>
Then i used this script to change background
<script type="text/javascript">
function getval(sel)
{
if(sel.value == "0"){
$("#sel").css("background-color", "orange");
}
}
</script>
Finally i fixed it
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>
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.
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 am using the popular ChoronoForm plugin for Joomla. I have created a form which contain dynamic fields. A javascript function is used to add multiple "items". A User clicks on the button "Add another item" and the item field is redisplayed for them to insert additional "items". Similar to the form on this website http://www.unclesmoney.co.uk/home3.php (Fill in the Item Details Section and click on "add another item"). When the form is submited the information is sent via email. The problem, only the item that was inserted last is sent and other itens are ignored (not sent) e.g. if I fill out the form with 2 or more items only he detail of the last item that was filled out is sent. Assume I have to write some php or javascript to pass these values to the html template or is there a more simple way? either way I can do with some pointers on how to do acheive this. There is a similar thread on this forum which was a great help but it doesnt show how to modify the html template. Thanks
<div class="form_element cf_dropdown">
<label class="cf_label" style="width: 150px;">Item Type:</label>
<select class="cf_inputbox validate-selection" id="select_19" size="1" title="" name="select_19">
<option value="">Choose Option</option>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textarea">
<label class="cf_label" style="width: 150px;">Detail</label>
<textarea class="cf_inputbox required" rows="3" id="text_2" title="Please enter Item Detail" cols="30" name="text_2"></textarea>
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_fileupload">
<label class="cf_label" style="width: 150px;">File</label>
<input class="cf_fileinput cf_inputbox" title="" size="20" id="file_3" name="file_3" type="file" />
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">File 2</label>
<input class="cf_inputbox required validate-number" maxlength="150" size="30" title="Must contain a value" id="text_4" name="text_4" type="text" />
</div>
<div class="cfclear"> </div>
</div>
</div>
<span id="writeroot"></span>
<input type="button" value="Add Another Item" onclick="moreItems()" />
.....
Javascript
var counter = 0;
function moreItems() {
counter++;
var newFields = document.getElementById('readRoot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
Chronoforms does a simple replacement in the email template to fill in the form values. Unless you've some code to manipulate that, it is only expecting a single value. What you need to do is add some PHP in the "On Submit Code - before sending email" area in the Form Code tab. A quick loop through the all of the extra fields to combine them in to the single variable name that Chronoforms is expecting should fix your problem.