php drop down list - php

i’m new to codeigniter and i’m working on a project. i have to create a dynamic drop down menu with values from my database, when a selection is made in the drop down as soon as you click on the submit button a new page has to occur where all the cities associated with the province selected in the drop menu appear, the cities are also in my database .My database consists of an id field, province field and a cities field.The drop menu is fine but cant seem to make the cities appear in the next page. your help will be highly appreciated
ok here's my code
this is from my view file which displays my drop menu this side is ok
<?
function writeCities($id)
{
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("msansi", $con);
$query = "SELECT cities FROM provinces WHERE id =";
$query .= $id;
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row[0];
}
function populateDropBox()
{
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("msansi", $con);
$result = mysql_query("SELECT id,title,cities FROM provinces");
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[0]>" . $row['title']."</option>";
}
}
?>
<form name="myform" action="http://localhost/CodeIgniter_1.7.3/index.php/ndivhuho/submit" method="post">
<select name = "province" onChange="onChangeDropBox();"/>
<? populateDropBox(); ?>
<input type="submit" value="submit"; />
</form>
and here's my other view file which is supposed to display the cities in a text area
<?
function writeCities($id)
{
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("msansi", $con);
$query = "SELECT cities FROM provinces WHERE id =";
$query .= $id;
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row[0];
}
?>
<script type="text/javascript">
function onChangeDropBox()
{
var selected =0;
selected = document.myform.province.value;
var t = "<? writeCities(1);?>";
document.myform.textArea.value = t;
}
</script>
<form name=myform>
<textarea name="citites" readonly="true";></textarea>
</form>
i'm sure theres something i need to do in my controller which i don't know of
thanxx in advance!!!

Take a look at the following two guides on how to do what you're talking about doing:
http://php-ajax-code.blogspot.com/2007/07/ajax-triple-dropdown-with-states-cities.html
http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html

There are a few problems here.
The code you have provided is using native php functions to connect to mysql. You should be using the proper CodeIgniter libraries. Start by reading this.
http://codeigniter.com/user_guide/database/examples.html
Once you've read that..
"this is from my view file which displays my drop menu"
Take the code out of your view file! The database calls should be in a model, and that should be called by a controller, which passes the data through to your view file.
Probably read this too:
http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller

Related

Loading combobox from database in PHP

<?php
function __autoload($classname) {
$filename = "./". $classname .".php";
include_once($filename);
}
class empidload{
//function for load combo box
public function loadCombo(){
$connection=new connectdb();
$con=$connection->getCon();
$query = "SELECT * FROM department";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row["dno"]."'>".$row["detail"]."</option>";
}
}
}
?>
<html>
<body>
<select name="department" id="department" style="width:204px;" onchange="change(this);">
<?php
$emp = new empidload();// calling function for load combo boxes
$emp.loadCombo();
?>
</select>
</body>
</html>
Here I try to load combobox from MySQL database. I wrote function and call it. But it doesn't work.can anyone help me please? actually what i want to load comboboxes from database table. And also I want to change another combobox according to the value of first combobox. Please help me.
You're mixing MySQL functions, you can't do that.
You're using mysql_query and other mysql_ functions but with a mysqli_ DB connection.
Change $result = mysql_query($query) or die(mysql_error()); to
$result = mysqli_query($con,$query) or die(mysqli_error());
and while($row=mysql_fetch_assoc($result)){
to
while($row=mysqli_fetch_assoc($result)){
Plus, as stated in Notulysses' answer:
change $emp.loadCombo(); to $emp->loadCombo();

having trouble getting selected value from php dynamic selection option

I want to show options from my database for users to check, but having trouble getting user's choice.
So, I write two php files,
the first one doing things like: getting data from database, displaying in select option, then submit value by post to and the second php file.
And the second php file just display the recieved value.
Here's the first php file:
<html>
<body>
<form method="post" action="second.php">
<Select name=”select_value”>
<?
//connect to server
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Error " . mysqli_error($con));
$query = "SELECT * FROM MYTABLE" or die("Error in the consult.." . mysqli_error($con));
$result = $con->query($query);
//display result in select option
while ($row = mysqli_fetch_array($result)) {
echo "<Option value=".$row['ENTRY_ID']."> ".$row['ENTRY_NAME']."</Option><br>";
}
mysqli_close($con);
?>
</Select>
</form>
</body>
</html>
And the second php file:
<?
$option = isset($_POST['select_value']) ? $_POST['select_value'] : false;
if($option) {
echo $_POST['select_value'];
} else {
echo "not getting value of select option";
exit;
}
?>
If this works fine, I should see the selected value by the second php file, but I keep recieving my echo "not getting value of select option".
There must be something wrong between select option and my recieving file.
Can someone help?
try this double quotes
<Select name="select_value">
instead of <Select name=”select_value”>

I'm not able to change the page contents when i select combo-box options in php

<?php
// select box open tag
$selectBoxOpen = "<select name='store_name' >";
// select box close tag
$selectBoxClose = "</select>";
// select box option tag
$selectBoxOption = '';
// connect mysql server
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
// select database
mysql_select_db("store", $con);
// fire mysql query
$result = mysql_query("SELECT store_name FROM store_input");
// play with return result array
while($row = mysql_fetch_array($result)){
$selectBoxOption .="<option value = '".$row['store_name']."'>".$row['store_name'] . "</option>";
}
// create select box tag with mysql result
$selectBox = $selectBoxOpen.$selectBoxOption.
$selectBoxClose;
echo $selectBox;
?>
this is my sample code, I have created combobox in php with option values from database values
but i'm not able to change the page contents when i select options.
any answers
Your code is lacking Javascript, which would be required for the outcome you are expecting. Seeing as you haven't really defined the complete outcome you expect, I will just show you what you need to update the url with the currently selected store:
$selectBoxOpen = "<select name='store_name' onchange=\"location.href=location.href+'?store='+this.value\" >";
I've added an onchange event (read more about DOM events here) so that when you select a different value you are changing the location.
Now when you change the value of the select, your url should change to scriptname.php?store=SomeStoreName

Setting variable from combo box

How do I get the value in the combo box and store it in a variable? And use the variable in another PHP file? Here is my code.
Note: I am already able to get data from database and store it in the combo box.
$con = mysql_connect("localhost","root","aaaa");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("maptemp", $con);
$sql = "SELECT * FROM users";
$rs = mysql_query($sql) or die(mysql_error());
$selectbox='<select name=\'userst\'>';
while ($row = mysql_fetch_assoc($rs)) {
$selectbox.='<option value=\"' . $row['username'] . '\">' . $row['username'].'</option>';
}
$selectbox.='</select>';
mysql_free_result($rs);
echo $selectbox;
How do I get the value in the combo box and store it in a variable?
Assuming you are talking about a plain select element.
Submit the form it is in
Read $_POST['foo'] or $_GET['foo'] where foo is the name of the select element
And use the variable in another PHP file?
include that file
Store the data in a session
Redirect to that file while passing the data in the query string
various other options depending on precisely what you want to acheive

How to use the form value in php function?

I am newbie to php.I have coded auto-complete text box using php,and i have a submit button.i have not given form action.
This is the HTML form code that i used for autocomplete textbox.this autocomplete textbox selects the value
<form method="post" autocomplete="off">
<p>
<b>Theater Name</b> <label>:</label>
<input type="text" name="theater" id="theater" />
</p>
<input type="submit" value="Submit" />
</form>
I have another php function that retrieves the values based on where clause.in the where statement i want to use selected value from form.
for ex: select address from theaters where theater_name ="form value"
How to use the form value in php function?can any one help me?
<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("theaterdb", $con);
$result = mysql_query("SELECT * FROM theter
WHERE theater_name="<!-- This could be value that we get after clicking submit button-->);
while($row = mysql_fetch_array($result))
{
echo $row['thearer_name'];
echo "<br />";
}
?>
Thanks in advance......
You could get the value from $_POST by $_POST['theater'].
And note, you should not use this value directly in the sql, you need to escape it to prevent sql injection.
$theater = mysql_escape_string($_POST['theater']);
$result = mysql_query("SELECT * FROM theter WHERE theater_name='$theater'";
Last, you could take a look at PDO, which is suggested over the old mysql_* functions.
First, change your submit button code to the following:
<input name="submit" type="submit" value="Submit" />
Now, this is the code you should use for the query:
<?php
if (isset($_POST['submit'])) {
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("theaterdb", $con);
$result = mysql_query("SELECT * FROM theater
WHERE theater_name='" . mysql_real_escape_string($_POST['theater']) . "'");
while($row = mysql_fetch_array($result))
{
echo $row['theater_name'];
echo "<br />";
}
}
First, I check that the user submitted the form. Then, I escape the data he has submitted and inserting it into your query.
* NOTE: All of what I've wrote is based on the assumption that the code is executed after the form is submitted.
* ANOTHER NOTE: You should read about using PDO rather than MYSQL functions.
First and foremost, try using mysqli instead of mysql (mysqli_query, mysqli_connect). There are numerous security / speed advantages to using it and it has pretty much the exact same functionality.
While the above answers mention using $_POST['theater'] (the name of your input), be SURE to escape your post before putting it into your query.
$con = mysqli_connect("localhost","root", "YOUR PASSWORD HERE", "YOUR DATABASE HERE");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
// No need for this, please see the updated mysqli_connect as the 4th parameter selects your DB
//mysqli_select_db("theaterdb", $con);
// Please notice the last parameter of the mysqli_real_escape_string is your Input's POST
$query = "SELECT * FROM theater WHERE theater_name=".mysqli_real_escape_string($con, $_POST['theater']);
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo $row['thearer_name'];
echo "<br />";
}
$_POST["your_variable_name"] // for POST
$_GET["your_variable_name"] // for GET
For in-depth information please go to: http://www.php.net/manual/en/language.variables.external.php

Categories