I have two tables in my database. I have a form to add information to table news. In my form I have dropdown list with information from another table courses. I want to save the result of the selection and save it in table news. How I can do that?
code:
<p>Course</p>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("university");
$sql = "SELECT name FROM courses";
$result = mysql_query($sql);
print '<select>';
while ($row = mysql_fetch_assoc($result)) {
print '<option value='.$row['name'].'>'.$row['name'].'</option>';
}
print '</select>';
?>
First you would need to give a nameattribute to your select tag, then as soon as you post your form, you will get the value of the selected option the exact same way as you get the value of other input fields.
I would also advise to get both the id and the name of your courses from your selct query. You should then use the id as the value attribute of your option , qhile you can keep the name to display it to the user. That way you would store the id of the selected course instead of the name, meaning that if a name slightly changes, you will still have the proper course id stored.
So your final HTML for this field should look something like that :
<select name="selectedcourse">
<option value="1">Course 1</option>
<option value="2">Course 2</option>
<option value="3">Course 3</option>
</select>
And you would get the seleted value server-side in PHP using something like :
$_POST["selectedcourse"]
If, of course, your form method was POST and not GET...
Related
I'm struggling with something simple, I found a similar question on Stack but I couldn't make the solution work.
I have a html/php form to edit user's details. Because it is editing already existing information, a dropdown needs to pre-load a user's organisation from the mysql database. Because of db normalisation I put the organisation details in a separate table to the users table, so the user's record just holds their organisation's id.
The code fetches the user's info and populates the form for editing, but I can't get the organisation dropdown to display the user's organisation. I need it to have pre-selected the user's organisation (if applicable), but still have the list of options to alter it.
<td align="right">Organisation:</td>
<td>
<select name="user_org">
<option>Select the organisation</option>
<?php
//query to get the organisations from the db
$get_org = "SELECT * FROM organisations";
$run_org = mysqli_query($con, $get_org);
while ($row_org=mysqli_fetch_array($run_org)){
$org_id = $row_org['org_id'];
$org_name = $row_org['org_name'];
echo "<option value='$org_id';>$org_name</option>";
}?>
</select>
You can pick an option to be the selected one by default with the "selected" attribute. In your situation, you can just check within the loop whether or not the current organization is the user's one. And add the "selected" attribute when it is.
For example, let's say your user's organization id is stored in a variable user_org.
while ($row_org=mysqli_fetch_array($run_org)){
$org_id = $row_org['org_id'];
$org_name = $row_org['org_name'];
echo "<option value='$org_id'";
if($org_id === $user_org) echo " selected";
echo ">$org_name</option>" . PHP_EOL;
}?>
Then, if an organization has an id that matches the user's one, the option will be selected by default.
Try something like this:
while ($row_org=mysqli_fetch_array($run_org))
{
$org_id = $row_org['org_id'];
$org_name = $row_org['org_name'];
$selected = '';
if( $org_id == $already_selected_value )
{
$selected = 'selected="selected"';
}
echo <option value="'.$org_id.'" '.$selected.'>'.$org_name.'</option>';
}
Explanation: Initially $selected is blank but when condition becomes true, its value set the selected attribute of option to make that option default selected.
I have two select fields(country and state) in "My Account" form for user which are auto populated with countries and state list from countries.js. This works fine
But whenever user opens his account details(like amazon) I want the respective fields to be populated with current values from database (since these are active input fields user can also change the values). I am doing this by echoing values in input field like(see code below)
<input type="text" class="form-control" id="tgender" name="tgender" value="<?php echo "$trainer_gender"?>" >
nter code here
But this doesn't work for country and state. If I am echoing the values in country and state field then dropdown of countries doesn't work(if user wants to change or for first time user)
Values of select boxes are set different compared to text input fields.
What you are searching for is the selected attribute of the <option> tag (see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option).
Wherever you build your selection box for the country/state, you need to iterate over that list, and check if the current iteration value matches the one from the database.
For example:
<select name="state">
<option value=''>SELECT</option>
<?php
$conn = mysqli_connect('localhost', 'root', '');
$result = mysqli_query($conn, 'SELECT id, name FROM states');
while ($row = mysqli_fetch_assoc($result))
{
$selected = ($stateFromDB == $row['name']) ? 'selected' : '';
echo "<option value='$row[id]' $selected >$row[name]</option>";
}
?>
</select>
You can also select the correct value via Javascript, e.g. by using jQuery:
$("#state-list").val("Your State from DB");
Im currently using a select statement for a dropdown box to select from multiple products. On an edit form i want the select to automatically select the choice that was originally chosen which is stored in the variable $product. Im using an array to bring in all choices from the database table however can't get the array to work aswell as select the data stored in $product.
The code i've currently got is;
$selectquery = "SELECT * FROM `loanproducts`";
$selectresult = mysqli_query($connection, $selectquery);
<select name="product" style="width: 150px">
<?php while($row1 = mysqli_fetch_array($selectresult)):;?>
<option><?php echo $row1[1];?></option>
<?php endwhile;?>
</select></p></div>
Im not sure if I've explained what im trying to do well enough as it is confusing me even trying to explain.
-Basically for example I have 3 choices from the array- A, B & C.
-If the user goes through the form and selects B, when I then go to edit their choices I want their option to appear in the select box (B) - rather than the first choice on the array which would presumably be A
Thanks
I changed a little your programming style which is not very confortable to me. Anyway, the important thing here is that I create the variable $selected with two possible values : "" or "selected", depending if the current product ($row1[1]) is equal to the product selected ($product). All options but one will get "", only the selected product will get "selected" :
<select name="product" style="width: 150px">
<?php while($row1 = mysqli_fetch_array($selectresult))
{ if ( $row1[1] == $product ) // IF CURRENT PRODUCT WAS SELECTED
$selected = "selected";
else $selected = "";
echo "<option $selected>{$row1[1]}</option>";
}
?>
</select></p></div>
I have made a autocomplete control as given below.
var PrimaryPhy = <?php include('getdata.php'); ?>;
$("#PatientID").autocomplete({
source: PrimaryPhy,
autoFocus:true
});
getdata.php
<?php
$con = mysql_connect("localhost","***","***");
if(!$con){
die("Error : ".mysql_error());
}
mysql_select_db("DBname",$con);
$patient_info = array();
$result = mysql_query("select * from tablename");
while($row = mysql_fetch_array($result)){
$patient_info[] = $row['Name'];
}
echo json_encode($patient_info);
?>
Here I have brought all the Name information of all records from the table. But i wanted to do like Dropdown with option values.
For example :
<option value="1">Item 1</option>
Likewise i need to bring the primary key ID of the table as a value for that item. How can i achieve that?
And also If i place one more autocomplete control next to this control, How can i make that first autocomplete's output as a input to the second autocomplete's input?
You can accomplish it by <datalist>.
The <datalist> element, a new addition in the HTML5 specification, allows developers to create native autocomplete dropdowns for their web applications.
The <datalist> element is used to specify all of the possible values for the autocomplete list like below:
<datalist id="languages">
<option value="HTML">
<option value="CSS">
<option value="JavaScript">
</datalist>
For detailed example, you can check here
Can any one please help me? I am new to php, currently I am trying to use $_POST['item'] to obtain a string (consists of many words) from a dropdown box and then process it. However, when I use $_POST['item'], only first word is returned, the rest of words are missing.
I have a dropdown box, something like:
echo "<form action='process_form.php' method='post'>";
echo "Select an item<br />";
echo "<select name='item' id='item'>";
...
...
...
each item in the dropdown box is a string that has product names like:
dressmaker mannequin size 12
torso mannequin in white color
...
User will then select an item from the dropdown box. When I used $_POST['item'] to obtain this string, I get the first word "dressmaker", all the rests were missing.
How do I get the whole string?
Many thanks in advance.
I am not sure exactly what you are doing but I would do something like this. For this example I assume that the values "dressmaker mannequin size 12" will correspond to the values of the columns in the database to which I will refer to as "colA, colB, colC, and colD", and in addition I assume you have an "ID" column in your database.
Here is the code I would use to generate the select drop-down list:
//$query is the variable storing the result of the mysql_query();
//assumption is that the result-set is non-empty
echo '<select name="item" id="item">\n'; //\n - new line character
//run through the loop to generate the items inside the list
while ($result = mysql_fetch_assoc($query)) {
//note the id - it will be used to find data in the
//database after the POST is complete
//couple of temp variables (not necessary but makes code cleaner)
$database_id = $result["ID"];
$colA = $result["colA"]; //ex:dressmaker
$colB = $result["colB"]; //ex:mannequin
$colC = $result["colC"]; //ex:size
$colD = $result["colD"]; //ex:12
//add option to the select drop-down
echo "<option value=\"$database_id\">$colA $colB $colC $colD</option>\n";
}
echo "</select>";
Now to retrieve the data from the POST. I am including the code Drewdin suggested.
//form was submitted already
//assumption is that the database connection is established
$item_id = mysql_real_escape_string(trim($_POST["item"]));
//Now get the info from the database for this id
//table is "table"
$string = "SELECT * FROM table WHERE ID = $item_id";
$query = mysql_query($string) or die("Could not complete the query: $string");
//assumption here is that the result set is non-empty
$result = mysql_fetch_assoc($query);
$colA = $result["colA"]; //ex:dressmaker
$colB = $result["colB"]; //ex:mannequin
$colC = $result["colC"]; //ex:size
$colD = $result["colD"]; //ex:12
//now you can use the values of colA-D to compute whatever you want
Hope this helps. Using database ids is nice for security plus it makes things more manageable.
Regarding using this blog. You can post comments to the answers people post. If you want however to add something to your question, you can edit your original question and just make sure its obvious what was added.
When the issue is resolved and if any answer helped and you liked it, you can pick it as the final answer by clicking the check mark next to it.
Best of luck
without seeing your select options i think this might help you
if (isset($_POST['submit'])) {
// Grab the output of the select list
$Select_Output = mysqli_real_escape_string( $dbc, trim($_POST['item']));
//What ever else you want to do here...
}
I also would use Miki725's post to make sure your select list is setup correctly
This is how the php gets the values from the HTML select item:
<select name="item" id="item">
<option value="this is option 1">option 1</option>
<option value="this is option 2">option 2</option>
<option value="this is option 4">option 3</option>
...
</select>
Basically you would do something like:
$var = $_POST['item'];
The $var will be the whatever was entered into the "value" of the option tag. So just make sure you have the proper values entered into the value fields.
Hope that helps.