On Drop-Down Select, store value in database - php

I am working on a registration form, to store family details in database table. I have my drop-down list of the number of brothers or sisters field.
<select id="purpose" required="required" name="purpose">
<option value="0">No Brother nor Sister</option>
<option value="1">Brother or/and Sister</option>
I would like to know on select of first option how to allocate null values for bro and sis fields in database.
The options with value 1 has got further additional fields that is is brother married or sister married. But if no bro nor sis exist then it should wrap at first stage only indicating bro and sis as null, followed by all further fields being null, like bro_married and sis_married.
My main form code :
<select id="purpose" required="required" name="purpose">
<option value="0">No Brother nor Sister</option>
<option value="1">Brother or/and Sister</option>
</select>
<div style='display:none;' id='business'>
<div>
<label for="username" class="uname">Number of Brothers</label>
<select id="bro" name="bro" required="required">
<option value="<?php if(isset($_GET['bro']))
{echo $_GET['bro'];}else{echo "";}?>"><?php if(isset($_GET['bro']))
{echo $_GET['bro'];}else{echo "Select";}?>
</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
My PHP file to save
if(isset($_POST['step_four_save']))
{
$bro=$_POST['bro'];
update_user_info=mysql_query("update partners_registration set bro='$bro' where email_username='$username' ");
Kindly advise, thanks.

I am not pretty sure why you need to check the null for your dropdown. Dropdown will have at least blank '' or some value of first child (option) if you do not select any value on form submit.
Here is the code if you are looking for them:
if("" == trim($_POST['bro'])){
//
}
Or
if (is_null($_POST['bro'])){
//
}

Assuming bro column in partners_registration is integer then following code can work
if(isset($_POST['step_four_save']))
{
if(isset($_POST['bro']) && $_POST['bro'] != "") {
$bro = $_POST['bro'];
} else {
$bro = NULL;
}
$update_user_info=mysql_query("update partners_registration set bro=$bro where email_username='$username' ");
}

Related

Unknown issue when trying to make a dropdown in html and php

Making a new tool and require a dropdown menu but it doesn't seem to work the way I have coded it.
Not sure exactly what the problem is. Have researched online but I haven't been able to figure it out.
if(!isset($_POST["ReasonList"]))
{
$error .= '<p><label class="text-danger">Please select a reason</label></p>';
}
<div class="form-group">
<label>Select Reason for Request</label>
<select id="ReasonList" name="ReasonList" class="form-control" value="<?php echo $ReasonList; ?>" />
<option value = "">Select...</option>
<option value = "1">Original Engineer has left the company</option>
<option value = "2">Actively involved in field work on customer site</option>
<option value = "3">No capacity due to customer mandated deadlines</option>
<option value = "4">Exception request by manager</option>
</select>
</div>
If option is selected then able to submit the form. If option is not selected it will give the error in the if statement - Please select a reason
See the corrected <select> element, and how a bit of code indentation make the HTML easier to read and debug.
<div class="form-group">
<label>Select Reason for Request</label>
<select id="ReasonList" name="ReasonList" class="form-control">
<option value = "">Select...</option>
<option value = "1">Original Engineer has left the company</option>
<option value = "2">Actively involved in field work on customer site</option>
<option value = "3">No capacity due to customer mandated deadlines</option>
<option value = "4">Exception request by manager</option>
</select>
</div>
The <select> element does not have a value property and you accidentally closed the <select> before the name attribute.

Using $_POST to get select option value from more than two dropdown lists HTML

How can I fill a MySql query using php's $_POST from more than two dropdown lists?
Lets say I have the following three dropdowns in a HTML form:
<form method="post" action="myphpscript.php">
<select name="country">
<option value="1">Belgium</option>
<option value="2">Canada</option>
<option value="3">Spain</option>
</select>
<select name="year">
<option value="2001">First</option>
<option value="2002">Second</option>
<option value="2003">Third</option>
</select>
<select name="virus">
<option value="hiv">HIV</option>
<option value="hcv">HCV</option>
<option value="hbv">HBV</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
Then I have my query:
<?php
$query=sprintf("select country, year, virus from f_report where country='%s' and year ='%s' and virus ='%s'",
mysqli_escape_string($con,$_POST["country"]),
mysqli_escape_string($con,$_POST["year"]),
mysqli_escape_string($con,$_POST["virus"]));?>
How can I get this query to work if, for example, the user selected only "country"?
There are two ways you can go about this.
you can assign default values by verifying $_POST['key'] is empty for example
$_POST['year'] = isset($_POST['year']) ? $_POST['year'] : 'default value';
or
Set the optional fields to null in your database.
I hope it helps.

Whats the possibility of one updating a database field with a form having a drop down without changing the value of the drop down field

Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.
I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.
Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>
What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.

Insert value into database input or drop down menu

I have a form that a user can insert his/her level of education either by picking a value from a drop down menu or just typing some text. My code is simple:
<label for="education">Education: <input type="text" name="education"/> </label>
<select name="education">
<option value=""></option>
<option value = "Primary education" name="Primary education">Primary education</option>
<option value = "Secondary education" name="Secondary education">Secondary education</option>
<option value = "Bachelor" name="Bachelor">Bachelor</option>
<option value = "Master" name="Master">Master </option>
<option value = "Doctoral" name="Doctoral">Doctoral </option>
</select>
So i want to insert that value in a column called education in database. Using code above the value is inserted only when someone is picking a value from menu. Input text is not stored in database.
In your textbox part;
Change
<label for="education">Hobby: <input type="text" name="education"/> </label>
to
<label for="education">Hobby: <input type="text" name="education_text"/> </label>
Duplicate name causes your problem(select and textbox name are same). I have given education_text as an example, update it according to your needs. Do not forget to handle it backend with updated name.
On your backend;
$education = "";
if(!empty($_REQUEST["education_text"])) {
$education = $_REQUEST["education_text"];
} else if(!empty($_REQUEST["education"])) {
$education = $_REQUEST["education"];
} else {
die("Invalid education");
}
And use it in your query. You can change $_REQUEST to $_POST or $_GET according to your needs

PHP code to get selected text of a combo box

I have a combo box named "Make". In that combo box I'm loading vehicle manufacturer names. When I click SEARCH button I want to display the selected manufacturer name. Below is part of my HTML code.
<label for="Manufacturer"> Manufacturer : </label>
<select id="cmbMake" name="Make" >
<option value="0">Select Manufacturer</option>
<option value="1">--Any--</option>
<option value="2">Toyota</option>
<option value="3">Nissan</option>
</select>
<input type="submit" name="search" value="Search"/>
Below is my PHP code so far I've done.
<?php
if(isset($_POST['search']))
{
$maker = mysql_real_escape_string($_POST['Make']);
echo $maker;
}
?>
If I select Toyota from the combo box and press SEARCH button, I'm getting the answer as '2' . It means it gives me the value of the 'Toyota'. But I want to display the name 'Toyota'. How can I do that? Please help me ....
Try with this. You will get the select box value in $_POST['Make'] and name will get in $_POST['selected_text']
<form method="POST" >
<label for="Manufacturer"> Manufacturer : </label>
<select id="cmbMake" name="Make" onchange="document.getElementById('selected_text').value=this.options[this.selectedIndex].text">
<option value="0">Select Manufacturer</option>
<option value="1">--Any--</option>
<option value="2">Toyota</option>
<option value="3">Nissan</option>
</select>
<input type="hidden" name="selected_text" id="selected_text" value="" />
<input type="submit" name="search" value="Search"/>
</form>
<?php
if(isset($_POST['search']))
{
$makerValue = $_POST['Make']; // make value
$maker = mysql_real_escape_string($_POST['selected_text']); // get the selected text
echo $maker;
}
?>
Put whatever you want to send to PHP in the value attribute.
<select id="cmbMake" name="Make" >
<option value="">Select Manufacturer</option>
<option value="--Any--">--Any--</option>
<option value="Toyota">Toyota</option>
<option value="Nissan">Nissan</option>
</select>
You can also omit the value attribute. It defaults to using the text.
If you don't want to change the HTML, you can put an array in your PHP to translate the values:
$makes = array(2 => 'Toyota',
3 => 'Nissan');
$maker = $makes[$_POST['Make']];
You can achive this with creating new array:
<?php
$array = array(1 => "Toyota", 2 => "Nissan", 3 => "BMW");
if (isset ($_POST['search'])) {
$maker = mysql_real_escape_string($_POST['Make']);
echo $array[$maker];
}
?>
if you fetching it from database then
<select id="cmbMake" name="Make" >
<option value="">Select Manufacturer</option>
<?php $s2="select * from <tablename>";
$q2=mysql_query($s2);
while($rw2=mysql_fetch_array($q2)) {
?>
<option value="<?php echo $rw2['id']; ?>"><?php echo $rw2['carname']; ?></option><?php } ?>
</select>
Change your select box options value:
<select id="cmbMake" name="Make" >
<option value="">Select Manufacturer</option>
<option value="Any">--Any--</option>
<option value="Toyota">Toyota</option>
<option value="Nissan">Nissan</option>
</select>
You cann't get the text of selected option in php. it will give only the value of selected option.
EDITED:
<select id="cmbMake" name="Make" >
<option value="0">Select Manufacturer</option>
<option value="1_Any">--Any--</option>
<option value="2_Toyota">Toyota</option>
<option value="3_Nissan">Nissan</option>
</select>
ON php file:
$maker = mysql_real_escape_string($_POST['Make']);
$maker = explode("_",$maker);
echo $maker[1]; //give the Toyota
echo $maker[0]; //give the key 2
you can make a jQuery onChange event to get the text from the combobox when the user select one of them:
<script>
$( "select" )
.change(function () {
var str = "";
$( "select option:selected" ).each(function() {
str += $( this ).text() + " ";
});
$('#EvaluationName').val(str);
})
.change();
</script>
When you select an option, it will save the text in an Input hidde
<input type="hidden" id="EvaluationName" name="EvaluationName" value="<?= $Evaluation ?>" />
After that, when you submit the form, just catch up the value of the input
$Evaluation = $_REQUEST['EvaluationName'];
Then you can do wathever you want with the text, for instance save it in a session variable and send it to other page. etc.
I agree with Ajeesh, but there are simpler ways to do this...
if ($maker == "2") { }
or
if ($maker == 2) { }
Why am I not returning a "Toyota" value? Because the "Toyota" choice in the Selection Box would have already returned "2", which, would indicate that the selected Manufacturer in the Selection Box would be Toyota.
How would the user know if the value is equal to the Toyota selection in the Selection Box? In between my example code's brackets, you would put $maker = "Toyota" then echo $maker, or create a new string, like so: $maketwo = "Toyota" then you can echo $makertwo (I much prefer creating a new string, rather than overwriting $maker's original value.)
If the user selects "Nissan", will the example code take care of that as well..? Yes, and no. While "Toyota" would return value "2", "Nissan" would instead return value "3". The current set value that the example code is looking for is "2", which means that if the user selects "Nissan", which represents value "3", then presses "Search", the example code would not be executed. You can easily change the code to check for value "3", or value "1", which represents "--Any--".
What if the user clicks "Search" while the Selection Box is set to "Select Manufacturer"? How can I prevent them from doing so? To prevent them from proceeding any further, change the set value of the example code to "0", and in between the brackets, you may place your code, then after that, add return;, which terminates all execution of any further code within the function / statement.

Categories