I have a dropdown on a page. I wish to publish the selected option as a link to another web page. If a user selects Kenya in the code below, for example, the value is stored in a database and then the record is retrieved and published as a link to a certain page. See code below:
HTML:
<select name="country" id="country">
<option value="kenya">Kenya</option>
<option value="uganda">Uganda</option>
</select>
PHP/SQL
//Data saving
$user_country = $_POST['country']);
$sql = "INSERT INTO countries (country_name)
VALUES ($user_country)";
//publishing the saved data as a link
$sql = "SELECT user_country FROM countrydb";
$result = $conn->query($sql);
if ($result->num_rows > 0) && ($row["user_country"] = "kenya") {
while($row = $result->fetch_assoc()) {
echo ' Kenya';
}
}
elseif ($result->num_rows > 0) && ($row["user_country"] = "uganda") {
while($row = $result->fetch_assoc()) {
echo 'Uganda';
}
}
else {
echo "no results";
}
It's not clear what the purpose of the database table is in this example - the data stored in it is just recording what the user selected, which PHP already knows, and doesn't seem to add any value. If I've misunderstood the purpose of it, you would need to clarify the question.
And also, if you have many countries in the list, hard-coding the if statements to generate the hard-coded links is going to get very tedious.
There are various ways you could improve this depending on exactly how you want it to work, but if you want to generate links, do it dynamically based on the user's selection, for example:
$user_country = $_POST['country']);
echo ''.$user_country.'';
To take it a step further, rather than having pre-defined pages for each country, (again depending on your exact requirements) you could possibly have a single page which just takes the country name as an input, and then retrieves information about that country from the database. You could also potentially make it redirect to the page immediately after the user has chosen that page in the dropdown, rather than generating a link which they then have to click on again to make the same selection. Those are some things to think about, according to how you want the application to work.
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 a list box and have 2 questions.
1st how can i still have the text to say Select a country?
2nd on my update page it displays the proper country but will not allow it to be change for it will not list any of the other countries in edit page
<?php
$sql = "SELECT countries.country_id, countries.country_name, users.user_country FROM countries, users
WHERE users.user_country = countries.country_id";
$result = query($sql);
?>
<select class="form-control input-lg box" id="country" name="country">
<?php
$i = 0;
while (($row = mysqli_fetch_assoc($result)) != false) {
?>
<option value="<?=$row["country_id"];?>"><?=$row["country_name"];?></option>
<?php
$i ++;
}
?>
Well, to have a default option not provided by your database call, you just need an HTML statement such as
<option value="">Select a country</option>
right below your <select> statement.
And we can't see your table definitions, but your SQL code seems to be picking the specific country that the user is associated with, so that there is only one country in the list. You probably need two queries: One that obtains the user's current country, and a second that obtains the entire list of countries.
After the first query, save the user's current countryid in a variable - say, $usercountryID.
Then, in your loop through the countries, compare each countryID to the user's country, like this:
while (($row = mysqli_fetch_assoc($result)) != false) {
if ($usercountryID == $row["country_id"]) $selected = " selected";
else $selected = "";
echo "<option value='{$row['country_id']}$selected>{$row['country_name']}</option>\n";
}
$selected could be defined many ways, of course, including in a conditional assignment statement. I omitted the $i counter, which isn't needed here, but, of course, you might need it later.
Can anybody help me how to obtain this with (php/js or jquery)? I'm still beginner.
I have table in mysql which contains beyond other (and I don't know it before checking the DB)
name of company (for ex. Abc, Def...)
year
month
and some data for each month different ones
I need to create a form which in first select field I load available name of company. When user select one of them (Abc) it should display only appropriate year - filled for that company (2000, 2008, 2012) and etc. for (selected year just filled month).
I've done working the first part (bellow) but how to handle with different selects for different companies if I don't know possible combinations without checking DB.
<?php
echo "Select company: <select name=\"company\">";
$con=connect();
$result = mysqli_query($con,"SELECT * FROM Company");
while($row = mysqli_fetch_array($result)) {
echo "<option value=\"".$row['Name']."\">".$row['Name']."</option>";
}
mysqli_close($con);
echo "</select><br>";
?>
in order to do that you need to use Ajax , suppose the ajax file is relatedYears.php
in relatedYears.php, write the following code
<?php
$con=connect();
$result="";
//this is the company name sent by url using ajax in the main page
$company_name= $_GET['company_name'];
$query= "SELECT years FROM COMPANY WHERE company = $company_name";
$x=mysql_query($query) or die('error query');
$years=array();
while($row=mysql_fetch_row($x))
$years[]= $row[0];
foreach($years as $year)
$results .= "<option value='".$year."'>".$year."</option>";
echo $results;
?>
needing some advice.
I am wanting to include 4 drop down lists on a website, which all contain data from different fields in a mysql table.
I then want to be able to press a submit button and display the required data on a webpage.
I am having trouble with knowing what programming language to use and also finding it difficult to find any tutorials. Any help would be greatly appreciated.
Thanks
You mean a HTML dropdown list or just a select dropdown in a form?
If you mean a select dropdown in a form you could do something like this:
<?PHP
$query = mysql_query("SELECT value FROM table ORDER BY value ASC") or die(mysql_error());
$result = mysql_num_rows($result);
// If no results have been found or when table is empty?
if ($result == 0) {
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="your_result.php">';
echo '<select name="list" id="lists">';
// Fetch results from database and list in the select box
while ($fetch = mysql_fetch_assoc($query)) {
echo '<option id="'.$fetch['value'].'">'.$fetch['value'].'</option>';
}
echo '</select>';
echo '</form>';
}
?>
And then in your_result.php you should fetch the data from your MySQL database based on value from the (when you fetch use mysql_real_escape_string):
<?PHP $_POST['list']; ?>
You could also do everything in just one file, but thats up to you. Try to use google, there are dozens of tutorials out there.
I'm building a simple web form (or tying to!) which displays a list of football teams. This list of teams is located in a single column mysql table. How can I make it so that if a team has been selected on the web form, that it cannot be selected again on the form? i.e. to make sure they are not accidentally selected to play each other, or play twice at the same time. My code so far is as below, which seems to be working fine. It includes several other iterations of the same code for the other home teams and away teams. Any pointers/help greatly appreciated.
<select name="hometeam">
<?php
$query = "SELECT * FROM teams order by teamname";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['teamname']."\">".$row['teamname']."</option>\n ";
}
?>
</select>
<select name="hometeam2">
<?php
$query = "SELECT * FROM teams order by teamname";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['teamname']."\">".$row['teamname']."</option>\n ";
}
?>
</select>
I Method: A possible and simple solution of this can be through javascript. That is, once the selection has been made at both the combo-boxes, then before submitting the form to the server, you can compare the two values and if they are found to be same then you can alert the user about this.
Also, just to add, though you validate using javascript, but its always advisable to re-validate at the server side to avoid any hack. Hope this helps.
II Method: If you don't want to show the team chosen in one drop-down in other drop-down then for this you will have to use Ajax in following manner:
On selection of a value in one drop-down - fire an ajax request - sending the chosen value from the dropdown to the server. Now, modify the query at the server to select all the values excluding this value and then send the values back to the client. This way the user will be able to see the values excluding the chosen one.
Hope this helps.
jQuery would make this quite simple:
<script type="text/javascript">
$(document).ready(function() {
$("select[name=hometeam]").change(function() {
if ( $(this).val() == $("select[name=hometeam2]").val() ) {
alert("This team cannot play itself!");
}
});
$("select[name=hometeam2]").change(function() {
if ( $(this).val() == $("select[name=hometeam]").val() ) {
alert("This team cannot play itself!");
}
});
});
</script>
http://jquery.com/