I'm new with php and want to ask about this code.. I have one dropdown menu and one button..
I want to search in sql database what I choose in those drop down menu..
What is the sql syntax for search item in sql database by using drop down menu..
By default, I write --> SELECT * FROM helpline.
It should be --> SELECT * FROM helpline WHERE MISC = %**item in drop down menu**%.
This is my database = helpdesk
Table = helpline
NAME | DATE | MISC |
John | 02/01/2011 | Item 1 |
Mark | 03/01/2011 | Item 2 |
This is my code
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<label for="namelist"></label>
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<input type="submit" name="show" id="show" value="Submit" />
<?php
mysql_select_db("helpdesk",mysql_connect("localhost","root",""))or die (mysql_error());
$query = "SELECT * FROM helpline"; */ This line should select what I choose in drop down menu /*
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
?>
<table border="0" cellpadding="6" cellspacing="6" class="curve">
<thead>
<tr>
<th> <div align="right"><span class="font">NAME</span></div></th>
<th> <div align="right"><span class="font">DATE</span></div></th>
</tr>
</thead>
<tbody>
<tr>
<th><div align="left"><span class="font"><?php echo $row['name']; ?></span></div></th>
<td><div align="left"><span class="font"><?php echo $row['date']; ?></strong></span></div></td>
</tr>
</tbody>
<?php
}
?>
</table>
</form>
So, it should display all items in sql database according to what I choose in drop down menu when I click the button..
You can access your posted form data through the php $_POST[] associative array as described in PHP's Online Manual. From this you can select namelist and pass the value to your SQL.
$query = "SELECT * FROM helpline";
if(isset($_POST['namelist'])){
$dropdown_val = $_POST['namelist'];
$query .= " WHERE MISC = '$dropdown_val'";
}
But you should really be using ajax for this and jQuery would be a great javascript framework to help you accomplish this. Also, mysql_* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
Without Page Refresh You can do it like this
<script>
$(document).ready(function(){
$("#namelist").change(function(){
var data = $(this).val();
$.ajax({
type:'POST',
data:'search_value='+data,
url:'search_process.php',
success:function(data){
$("#result").html(data);
}
});
});
});
</script>
The HTML part
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<input type="button" name="show" id="show" value="Submit" />
<div id="result"></div>
The search_process.php Page
<?php
// DATABASE CONNECTIVITY
if(isset($_POST['search_value'])) {
$val = $_POST['search_value'];
$query = mysql_query("SELECT * FROM helpline WHERE MISC LIKE %$val%");
// NOW RUN YOUR QUERY
$result = '<table border="0" cellpadding="6" cellspacing="6" class="curve">';
$result .= '<thead>';
$result .= '<tr>';
$result .= '<th> <div align="right"><span class="font">NAME</span></div></th>';
$result .= '<th> <div align="right"><span class="font">DATE</span></div></th>';
$result .= '</tr>';
$result .= '</thead>';
while($row = mysql_fetch_array($query)){
$result .= '<tr>';
$result .= '<th><div align="left"><span class="font">'.$row['name'].'</span></div></th>';
$result .= '<td><div align="left"><span class="font">'.$row['date'].'</strong></span></div></td>';
$result .= '</tr>';
}
$result .= '</table>';
echo $result;
}
?>
Hope this gives an Idea
your select options not have value attribute
your dropdown
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
change it :
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled" value="">PLEASE CHOOSE ONE ITEM:-</option>
<option value="Item 1">Item 1</option>
<option value="Item 2">Item 2</option>
</select>
after submit get value from $_post["namelist"]
Related
I have a dropdown in which one is for states and second one is it's sub category, which shows name of cities on basis of first dropdown.
Right now user need to select one category then subcategories load,
What change I want is "To set one value as default and load it's subcategories too" at load of page, and further user select different category so it should load it's subcategories accordingly.
Index.php:
<form name="insert" action="" method="post">
<table width="100%" height="117" border="0">
<tr>
<th width="27%" height="63" scope="row">Sate :</th>
<td width="73%">
<select onChange="getdistrict(this.value);" name="state" id="state" class="form-control">
<option value="">Select</option>
<?php $query =
mysqli_query($con,"SELECT * FROM state");
while($row=mysqli_fetch_array($query))
{ ?>
<option value="<?php echo $row['StCode'];?>">
<?php echo $row['StateName'];?>
</option>
<?php }?>
</select>
</td>
</tr><tr>
<th scope="row">District :</th>
<td>
<select name="district" id="district-list" class="form-control">
<option value="">Select</option>
</select>
</td>
</tr>
</table>
</form>
<script>
function getdistrict(val) {
$.ajax({
type: "POST",
url: "get_district.php",
data: "state_id=" + val,
success: function (data) {
$("#district-list").html(data);
},
});
}
</script>
get_district.php:
<?php
require_once("config.php");
if(!empty($_POST["state_id"])){
$query =mysqli_query($con,"SELECT * FROM district WHERE StCode = '" . $_POST["state_id"] . "'");
?>
<option value="">Select District</option>
<?php
while($row=mysqli_fetch_array($query)){
?>
<option value="<?php echo $row["DistrictName"]; ?>"><?php echo $row["DistrictName"]; ?></option>
<?php
}
}
?>
You would need to do 2 things
1.) insert a selected flag into the input
2.) activate the second dropdown on page load
1.) If the selected entry does never change then hardcode the default selected field in your code like if $var = XXX then echo "selected".
If you have any function which calculates the default value e.g. a geolocation service like e.g. maxmind.com which takes the users remote IP address and outputs his current state, then use this result to set the selected entry.
<?php
while($row=mysqli_fetch_array($query)){
$row["StCode"] == "DESIRED_VALUE" ? $selected ="selected" : $selected ="";
?>
<option value="<?php echo $row['StCode'];?>" <?= $selected ?> ><?php echo $row['StateName'];?>
</option>
[...]
Or add a field in the table SELECTED where you mark the default record with 1.
<?php
while($row=mysqli_fetch_array($query)){
$row["selected"] == 1 ? $selected ="selected" : $selected ="";
?>
<option value="<?php echo $row['StCode'];?>" <?= $selected ?> >
<?php echo $row['StateName'];?>
</option>
[...]
2.) You have to set the second select dropdown on page load. There are several ways to do this - this is explained here in detail:
Jquery execute onchange event on onload
This is the picture where new row will add after user click's on [add more] button..It works fine but i want to send these values using ajax.I can access all textbox values with array map function in ajax except dropdown values.
how I would implement it with this example. Thanks!
<tr>
<td><input type='checkbox' class='case'/></td>
<td>1.</td>
<td> <select name='relation[]' id='relation' >
<option value='brother'>Brother</option>
<option value='sister'>Sister</option>
</select>
</td>
<td><input type='text' id='name' name='name[]'></td>
<td><input type='text' id='school' name='school[]'></td>
<td<select name='medium[]' id='medium'>
<option value='marathi'>marathi</option>
<option value='english'>english </option>
</select>
</td>
<td> <?php
//Get all class data
$query = $db->query("SELECT * FROM `class_master`");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="std[]" id="std" class="form-control focusedInput" >
<option value="">Select Standard </option>
<?php
if($rowCount > 0){
while($row72 = $query->fetch_assoc()){
echo '<option value="'.$row72['class_id'].'">'.$row72['class_name'].'</option>';
}
} else {
echo '<option value="">Class not available</option>';
}
?>
</select>
User form at the starting of the page and and while sending data using ajax just use ("#form_id").serialize() instead of using array map , serialize is the method of jquery. Please read about the method and your problem will be solved.
This ps_search.php page has dropdown for categories like laptop, bags etc... and a dropdown for city. if a user wants to post an ad means he/she wants to sell something on website , first of all a category is selected like laptop, bag or anything else. and then city is selected from where the ad is posted. if, for instance, a user posts an ad of laptop from city1 and we have no more ads of laptops from other cities . then after selecting laptop , city 1 should appear in a dropdown menu. i have two files ps_search.php ang get_city.php
//ps_search.php
<form name="search" method="post" action="<?php echo $base_url ?>search_rides.php?go">
<table width="100%" border="0" cellpadding="4" cellspacing="0" id="tbsrch-engine">
<tbody>
<tr>
<td height="61">
<strong>Choose Category:</strong>
</td>
<td>
<select name="category_id" id="category_id" style="width: 155px;" onChange="get_city(this.value,'<?php echo $base_url ?>dropdown/get_city.php')">
<option value="0">Any</option>
<?php $query = "SELECT category_id as id, category_name as name FROM tbl_ps_category ";
$result = mysql_query($query);
?>
<?php while ( $row = mysql_fetch_array($result)) {?>
<option value="<?php echo $row['id'].'-'.$row['name']; ?>"><?php echo $row['name']?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td height="40">
<strong>City:</strong>
</td>
<td>
<div id="models">
<select name="city_id" id="city_id">
<option value="0">Any</option>
</select>
</div>
</td>
</tr>
<tr>
<td height="32"/>
<td>
<input type="submit" name="button2" id="button2" value="Search an Ad" class="fbutton"/>
</td>
</tr>
</tbody>
</table>
</form>
//get_city.php
<?php
include('../Connections/photohive.php');
$id = $_REQUEST['id'];
$explode = explode('-',$id);
$id = $explode[0];
$sql = "SELECT city_id FROM ".$ps_prefix."product WHERE category_id=".$id;
$query = mysql_query($sql);
//exit;
?>
<select name="city_id" id="city_id" style="width:155px;">
<option value="0">Any</option>
<?php
while ($row = mysql_fetch_array($query)){
$q1= sprintf("Select cityname from tbl_city where city_id='%s'" , mysql_real_escape_string($row['city_id']), " ORDER BY cityoreder ASC");
$r1= mysql_query($q1);
while($row2= mysql_fetch_assoc($r1))
{
?>
<option value="<?php echo $row['city_id']?>"><?php echo $row2['cityname']?></option>
<?php
}
}
?>
</select>
i have included js file as well
<script src="<?php echo $base_url;?>js/jquery.ajaxq-0.0.1.js" type="text/javascript"></script>
Please help me....
Your javascript should look like this:
(function ($) {
// When category select changes
$('#category_id').change(function() {
// make ajax query to get cities
$.get('get_city.php', {id: $(this).val())
.done(function(response) {
// get the cities dropdown from response and insert into page
$('#models').html(response);
});
});
})(jQuery);
I personally don't like returning HTML in my ajax calls. I usually prefer to return a JSON object and then parse out the HTML client side (usually using Handlebars). I think it's cleaner that way.
in my page when a user login into his acount there are two fields name and project so whenever he login his named filled in name field and his all projects are listed in dropdown in project field.my problem is tha when i edit this record it shows me the name and the project selected by him on submission but i want that on edit his all project will b listed there as on submission time.
Here is my project code for dropdown on submission time:
<tr>
<td>Select Project</td>
<td>
<select id="project" name="project">
<option value="">-- select --</option>
<?php while($row = mysql_fetch_array($result))
{
$pm = $row['assignpm'];
$assignpm = explode(",",$pm);
if(in_array($_SESSION['id'], $assignpm)){ ?>
<option value="<?php echo htmlspecialchars($row['projectname']);?>"><?php echo htmlspecialchars($row['projectname']); ?></option>
<?php } }?>
</select></td>
</tr>
and here is my code for project field on editing:
<tr>
<td>Select Project</td>
<td><select name="project" id="project" style="width:145px">
<option>Select</option>
<option selected="selected" value="<?php echo $fetuser1['project'];?>"><?php echo $fetuser1['project'];?></option>
</select></td>
</tr>
So when editing you must do things much the same as on "submission time".
<tr>
<td>Select Project</td>
<td>
<select name="project" id="project" style="width:145px">
<option>Select</option>
<?php
while($row = mysql_fetch_array($result)) {
$pm = $row['assignpm'];
$assignpm = explode(",",$pm);
if(in_array($_SESSION['id'], $assignpm)) {
?>
<option
<?php if($fetchuser1['project'] == $row['projectname']) echo "selected='selected'";?>
value="<?php echo htmlspecialchars($row['projectname']);?>"
>
<?php echo htmlspecialchars($row['projectname']); ?>
</option>
<?php } }?>
</select>
</td>
</tr>
depends...
what do your query look like?
one query? two querys? one on the userid, name and stuff and another on the projects...
or are all project in the same table as the fetched username?
and how are thy stored (blob, json, simple list)?
And last: mixing php inline with html is a very bad habbit ;)
say you have two querys
//query SELECT projectname FROM whatevertable WHERE userid = 'users-id'
$options = "<option value="">-- select --</option>".PHP_EOL;
while($row = mysql_fetch_array($result))
{
if(htmlspecialchars($row['projectname']) == $_SESSION['preselected']) //whatever
$options. = "<option selected="selected" value=".htmlspecialchars($row['projectname']).">".htmlspecialchars($row['projectname'])."</option>".PHP_EOL;
else
$options. = "<option value=".htmlspecialchars($row['projectname']).">".htmlspecialchars($row['projectname'])."</option>".PHP_EOL;
}
and after all the php code you can
echo "<td>Select Project</td>
<td>
<select id=/"project/" name=/"project/">
{$options}
</select>
</td>";
But without knowing any of your code but that little php-mixed-html it's hard to answer precisely
i want to control the drop down box to control show or hide statement. I do like this but it seems it doesn't work, i have it working if im using radio button.
can help me with the code? which part am i wrong?
thank you.
$dbcnx = mysql_connect('localhost', 'root', '');
mysql_select_db('dbase');
if($_POST['gred'])$gred=$_POST['gred'];else $gred="";
<script language="JavaScript">
function funcHide(elemHide1,elemHide2,elemHide3)
{
document.getElementById(elemHide1).style.display = 'none';
document.getElementById(elemHide2).style.display = 'none';
document.getElementById(elemHide3).style.display = 'none';
document.getElementById(elemShow).style.visibility = 'visible';
}
function funcShow(elemShow1,elemShow2,elemShow3)
{
document.getElementById(elemShow1).style.display = 'block';
document.getElementById(elemShow2).style.display = 'block';
document.getElementById(elemShow3).style.display = 'block';
document.getElementById(elemShow1).style.visibility = 'visible';
document.getElementById(elemShow2).style.visibility = 'visible';
document.getElementById(elemShow3).style.visibility = 'visible';
}
</script>
<table>
<tr>
<td>Gred </td>
<td>:</td>
<td><select name="gred" id="gred">
<option value=""> </option>
<option value="A17" <?php if($gred=='A17')echo "selected";?> onClick="funcShow('box1', 'box2', 'box3');">A17</option>
<option value="A22" <?php if($gred=='A22')echo "selected";?>>A22</option>
<option value="A27" <?php if($gred=='A27')echo "selected";?>>A27</option>
</select>
</td>
</tr>
<tr>
<td>TK</td>
<td>:</td>
<td>
<select name="tk" id="tk">
<option value=""> </option>
<option value="01" <?php if($tk=='01')echo "selected";?>>01</option>
<option value="02" <?php if($tk=='02')echo "selected";?>>02</option>
<option value="03" <?php if($tk=='03')echo "selected";?>>03</option>
<option value="04" <?php if($tk=='04')echo "selected";?>>04</option>
<option value="05" <?php if($tk=='05')echo "selected";?>>05</option>
<option value="06" <?php if($tk=='06')echo "selected";?>>06</option>
</select>
<?} ?>
</td>
</tr>
<tr>
<td colspan="2" valign="top">Status</td>
<td valign="top">:</td>
<td>
<?php
$qry = "SELECT * from dtable where userid='".$USER->id."'";
$sql = mysql_query($qry);
$row = mysql_num_rows($sql);
if($row==0)
{
?>
<input type=radio name="status" <?php if($status=='retake') {?>checked="checked"<?php } ?> value="retake" onClick="funcShow('box1', 'box2', 'box3');">Retake<br /></tr> <tr>
<td colspan='2'>
<div id="box1" style="display: none;">Last Date <br> Latest Date<br>
</div></td>
<td><div id="box2" style="display: none;">: <br> : <br></div></td>
<td>
<div id="box3" style="display: none;">
<?php $rsu[lastdate] ?> <br> <?php $rsu[latestdate] ?>
</div>
</td>
don't put the onClick attribute onto the option tag. You should use onChange on the select tag and then pass in this.value. Then based on that value you can decide which "box" to show/hide. Just a simple example:
<script type="text/javascript">
function showHide(selValue){
switch(selValue){
case "A17":
//show/hide whatever box you want.
alert(selValue);
break;
default:
//do nothing
alert('nothing');
}
}
</script>
<select name="gred" id="gred" onChange="showHide(this.value);">
<option value=""> </option>
<option value="A17"<?php if($gred=='A17')echo "selected";?>>A17</option>
<option value="A22" <?php if($gred=='A22')echo "selected";?>>A22</option>
<option value="A27" <?php if($gred=='A27')echo "selected";?>>A27</option>
</select>
for now, as you can tell, it just alerts some values. But it is easy to change.
Use onChange to trigger JS when you change something in the dropdown menu, instead of onClick. With selectedIndex you can determine what has been selected (the option 'show' or 'hide') and go from there.
Tip: get rid of all the junk code and limit it to just the elements that you need. Try to make it work in its most basic form, then build it out to what you're actually trying to do. That way it's a lot easier to track bugs and find out why it's not working.
Also, you might want to look into jQuery. You can do pretty much the same in plain old JavaScript, but jQuery makes things like this a lot easier.