Insert value into database input or drop down menu - php

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

Related

How to wipe a search query in PHP?

I have a search form that uses Javascript to change the input method depending on what's selected. So if you have "Name" selected you get a text box, but if you have "Rating" selected you get a drop-down list from 1-5.
<form method="GET" action="results_sample.php">
<p>Search by: <!-- Dropdown list allows users to search by different elements -->
<select id="searchtype" name="searchtype" onchange="changeview()"> <!-- onchange calls changeview() every time the dropdown value is changed. Allows ratings to be seen -->
<option value="School">School</option>
<option value="Name">Name</option>
<option value="Rating">Rating</option>
<option value="Class Location">Class Location</option>
<option value="Subject">Subject</option>
</select>
</p>
<span id="standard" style="display:inline">Search Query: <input type="text" name="query"></span>
<span id="locationsearch" style="display:none"> (Longitude-Lattitude pair) <input type="number" id="longitude" step="any"> - <input type="number" id="latitude" step="any"> </span>
<span id="ratevalue" style="display:none"> Choose rating:
<select id="ratings">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
</span>
<p><input type="submit"></p>
</form>
The relevant code in results_sample.php is:
<?php
if (isset($_GET['searchtype'])) {
if (!empty($_GET['query'])) {
$search = $_GET['query'];
} elseif (!empty($_GET['ratings'])) {
$search = $_GET['ratings'];
} elseif (!empty($_GET['longitude']) || isset($_GET['latitude'])) {
$search = 'more to come...';
}
echo '<p>Entries containing "', $search, '" in "', $_GET['searchtype'], '"</p>';
}
?>
I have two problems:
1) If I type text into the search query, then switch to ratings, and try to search by ratings, it still has the textbox invisible in the background, and uses that information instead.
2) If the textbox is empty and I want to search by Longitude-Latitude, Rating is still set at a default of 1 (invisible in the background) and it takes that value.
One option is to check $_GET['searchtype'] value and select another $_GET variable according to it, something like:
switch ($_GET['searchtype']) {
case 'Name':
$search = $_GET['query'];
break;
case 'Rating':
$search = $_GET['ratings'];
break;
case 'Class Location':
$search = 'other value';
break;
// add more cases as you wish
}
Other option can be using javascript to clear other fields' values when you change #searchtype but in this case select#ratings still will be sent to server.

On Drop-Down Select, store value in database

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' ");
}

getting the value in selection php

I have this selection box if I choose other textbox will show and the problem is the data I put in there only get the value "other" even I put text in the textbox the only value it gets is "other" so what do you suggest me to do this.
I'm using $_POST['talent'] to get the value.
Talent:
<select name="talent" onchange="if( this.value=='other' ) { this.form['other'].style.visibility='visible' }else { this.form['other'].style.visibility='hidden' };" required/>
<option value="Dancing">Dancing</option>
<option value="Singing">Singing</option>
<option value="other">Other</option>
<input type="text" name="other" style="visibility:hidden;" />
</select>
$_POST['talent'] will only return the value of the select element. If that value is 'other', that means that the user has selected that option. In that case, you need $_POST['other'] to get the value they typed into the box.
$talent = $_POST['talent'];
if ($talent === 'other')
$talent = $_POST['other'];
You'll need to use $_POST['other'] to get the contents of the text box. Also, you need to put the text box after the </select>.

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.

Why does my HTML5 form not submit the value for my option list with PHP?

In my code everything is submitted fine except for the "category" field in the option drop down list. The PHP file that used $category = $_POST['category']; can't get the string of text associated with each option value, but assigns the other variables with no problem.
This information from the form is then put into my database, and the user can make a search - all works fine apart from the category field.
<form method="post" action="add_item_action.php">
<table>
<tr><td>Item Name:</td> <td><input type="text" name="name"></td></tr>
<tr><td>Vendor Name:</td> <td><textarea name="vendor"></textarea></td></tr><tr><td>Item Details:</td> <td><textarea name="description"></textarea></td></tr>
<tr><td>Item Start Price ($):</td> <td><textarea name="price"></textarea></td></tr>
<tr><td>Item </td>
<tr><td><label>Category:</label></td>
<td><select id = "category">
<option value= "1">Select an option</option>
<option value = "Electronics">Electronics</option>
<option value = "Cars">Cars</option>
<option value = "Fashion">Fashion</option>
<option value = "Pets">Pets</option>
<option value = "Miscellaneous">Miscellaneous</option>
<option value = "Books">Books</option>
<option value = "Sports">Sports</option>
</select>
</td></tr>
<tr><td colspan=2><input type="submit" value="Add item">
</table>
</form>
Jquery uses the "ID" or the "class" to identify your elements, however, for a webbrowser to send the form data back to the webserver upon submit, you need the "name" attribute:
<select id="category" name="category">
Just remember: forms need names!
Your select
<select id = "category">
must have a name to be posted
<select id = "category" name="category">
user input values withou a name will not be posted.
All your other have one :) why not your category ..?
add name attribute here
<select id = "category" name="category">
and access in add_item_action.php like this
$_POST['category']
In your select box you have just given
<select id = "category">
give it as
<select id = "category" name="category">
Should be name=category
<select id = "category" name="category">
<option value= "1">Select an option</option>
<option value = "Electronics">Electronics</option>
<option value = "Cars">Cars</option>
<option value = "Fashion">Fashion</option>
<option value = "Pets">Pets</option>
<option value = "Miscellaneous">Miscellaneous</option>
<option value = "Books">Books</option>
<option value = "Sports">Sports</option>
</select>

Categories