I asked this question previously but i think i made it too complex, I want to assign a query if a value from a list is selected.
<form id="filter" name="filter" method="post" action="">
<select>
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
<p><input name="filter" type="button" value="Filter" /></p>
</form>
What i want to achieve is if user selects petrol the run the following query
$query = mysql_query("SELECT fuel_type from car WHERE fuel_type = 'petrol'");
<form id="filter" name="filter" method="post" action="">
<select id="college" name="fuel" onchange="changeValue();">
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
<p><input name="filter" type="button" value="Filter" /></p>
</form>
If you want to post the value in form submit use like this
$ctext = $_POST['fuel'];
$list = mysql_query("SELECT fuel_type from car WHERE fuel_type ='$ctext'");
$row = mysql_fetch_array($list);
//process $row
If you don't want to submit the form, you can use jquery in onchange event...
js code
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function changeValue() {
var ctext = $("#college option:selected").val();
$.ajax({ url: "yourfile.php",
data: {"ctext":ctext},
type: 'post',
success: function(output) {
//display output here
}
});
}
yourfile.php
<?php
//connect to db
$ctext = $_POST['ctext'];
$list = mysql_query("SELECT fuel_type from car WHERE fuel_type ='$ctext'");
$row = mysql_fetch_array($list);
echo $row['fuel_type'];
?>
Give your select tag a name:
<select name="fuel">
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
Then in your PHP you can do something simple like:
<?php
if(isset($_POST['cmdFilter']))
{
echo "You selected the fuel type: " . $_POST['fuel'];
}
?>
You can also set the select to have an id instead and use JQuery to send an ajax request passing it the value by giving selecting it by $("#myIdforSelect").val()
P.S
Be sure to sanitise your input ;)
Although I would also suggest the AJAX method, I just want to make it clear that you do not HAVE to use AJAX to accomplish this.
If your filter options are not too complex or many, then you can simply do a pull for all available options for any level of the filter tree on load and hide them initially. Then, as the user drills down into each section, you show them the next one accordingly (via javscript). In the end you can just submit a regular form with all the selected form fields via GET or POST.
This can effectively save bandwidth by running less calls to the server also :)
BUT if your choice tree gets too complicated, it might be better to just run AJAX request on user interaction.
Related
In my code new value through textbox is getting added to dropdown and stored in database but the problem is that, previous dropdwn list like here say rohit,viraj etc is not getting displyed along with new inserted value. how to show all option list along with new added one? is there any perfect way to do this using array? please help...
htmlcode
`html code:
<select class="deligates1" id="deligates1" name="deligates1[]" size="1" multiple>
<option value="">--select--</option>
<option value="rohit">ROHIT</option>
<option value="viraj">VIRAJ</option>
<option value="sachin">SACHIN</option>
</select>
<span style="cursor:pointer;background-color:lightgoldenrodyellow;">Add delegates here</span><span><input type="text" id="write_dele" name="write_dele" >
<input type="button" id="add_dele" value="add"></span>
`
script code
:<script>
$(document).ready(function(){
$("#add_dele").click(function(){
var delegate = $("#write_dele").val();
$.ajax({
type:'POST',
data:'q='+delegate,
url:'add1.php',
success:function(new_delegate){
$("#deligates1").html(new_delegate);
}
});
});
});
</script>
add1.php:
<?php
$new_delegate=$_POST['q'];
$con=mysql_connect("localhost","root","");
mysql_select_db("task",$con);
echo '<option value="'.$new_delegate.'" >'.$new_delegate.'</option>';
?>
.html() Replace the entire select option with your new option
Try with this
$("#deligates1").append(new_delegate); // it will add your new option in select box
I have made a connection between the drop down menu and the database which shows the current usernames in the rows. I then need to be able to click a box next to the menu that says go and then for it to redirect or refresh to a page with the selected usernames details.
I really need your help anyone i have been searching the net for weeks trying to find a solution.
How about this.
In the head tag:
<script type="text/javascript">
function showUser(){
var username = document.getElementById('username').value;
window.location = '?username='+username;
}
</script>
Then your html dropdown and button:
<select id="username">
<option value="user">user</option>
<option value="name">name</option>
<option value="list">list</option>
</select>
<input type="button" value="go" onclick="showUser()" />
With your data coming through from your database, your select field might look like:
<select id="username">
<?php
$sql = mysql_query("SELECT * FROM staff");
while($user = mysql_fetch_object($sql)){
echo '<option value="'.$user->username.'">'.$user->username.'</option>';
}
?>
</select>
Use a form
<form action="/yourpage" method="post">
<select>
<option value="<?=$id; ?>"><?=$name; $></option>
</select>
<input type='submit'/>
</form>
In yourpage.php
<?php
if(isset($_POST)){
// load user data
// or redirect with header() to preexisting page
}
// load view with user data
?>
this weekend i've been trying to use this script To create dependable menus.
It consists of an sql table with three rows: "ID, Master, Name" It later grabs the entries that contain 0 as the "master" and will use the resulting data to populate the first option list
To populate the next selection lists from the database, it uses a combination of the following JS and php:
and the rest of the select lists will populate accordinly.
The problem that i'm having is that After it populates the select lists I would like to have the visitors of the website hit a seach button to perform a search based on the data collected. The problem is that when I submit the form it sends the info stored in the "master" row of the database instead of the info on "name"
I'm Getting
index.php?genre=1&fruit=37&colour=39
Instead of
index.php?genre=Male&fruit=Strawberry&colour=Red
I tried to switch '.$row['name'].' to '.$row['id'].
But that was a no go, I also tried to only use '.$row['id'].' and it just messed up with the forms. Is there anyway I can accomplish what i'm looking for so that i can send the values selected on the fields to the url?
Thanks in advanced for any help on this one.
The behavior that you mentioned is normal as submitting a form automatically sends the value, instead of the text, of the selected option. The switch that you mentioned ('.$row['name'].' to '.$row['id'].)should work fine. If it is messing up the forms, please provide more information on what you mean by messing up the forms.
Otherwise, here is a possible solution. It's not the most elegant solution and is probably best suited for simple forms that do not require further complexities but basically, generate the querystring and redirect manually. This is based on the original example that you linked to at http://www.ssdtutorials.com/tutorials/series/dependable-dropdown.html.
JS:
var formObject = {
run: function (obj) {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
var id = obj.attr('id');
var v = obj.val();
jQuery.getJSON('http://jquery-dependable-dropdown.ssdtutorials.com/mod/update.php', {
id: id,
value: v
}, function (data) {
if (!data.error) {
obj.next('.update').html(data.list).removeAttr('disabled');
} else {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
}
});
}
};
$(function () {
$('.update').live('change', function () {
formObject.run($(this));
});
$('#submitButton').click(function () {
window.location.href = 'test.php?gender=' + $('#gender').find(':selected').text() + '&category=' + $('#category').find(':selected').text() + '&colour=' + $('#colour').find(':selected').text();
});
});
HTML:
<div id="wrapper">
<form id="theForm" action="" method="post">
<select name="gender" id="gender" class="update">
<option value="">Select one</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
<select name="category" id="category" class="update" disabled="disabled">
<option value="">----</option>
</select>
<select name="colour" id="colour" class="update" disabled="disabled">
<option value="">----</option>
</select>
<input type="button" id="submitButton" value="submit">
</form>
http://jsfiddle.net/BUJnf/1/
Hope that helps a bit!
I'm implimenting a user registrations form for a college..There I need to calculate the fees and show it to the user and the same time need to insert to a database..
I have used a list box for like this
<select name="subject" id="select2"> ***(value=fees for the subject)
<option value="100">Arts</option>
<option value="150">English</option>
calculation is OK with the values wich are given(I used javascript calculation for that)
Now I want to insert the subject to database
ex:
INSERT INTO user(name, email, subject)
VALUES ('$_POST[name]','$_POST[email]','$_POST[subject]')");
I want to add the subject which selected to be add to database subject field as English not the fees.
I hope you can understand what I'm telling.Please Help
Thank You
Ideally you should use the labels as values like:
<select name="subject">
<option value="Arts">Arts</option>
<option value="English">English</option>
If for any reason you want to post both label and fee you can use hidden variables and JavaScript like:
<select name="subject_fee" id="subject_fee" onchange="populate_subject_name();">
<option value="100">Arts</option>
<option value="150">English</option>
</select>
<input type="hidden" name="subject_name" id="subject_name" value="">
<script type="text/javascript">
function populate_subject_name() {
var f = document.getElementById("subject_fee");
var n = document.getElementById("subject_name");
n.value = f.options[f.selectedIndex].text;
}
</script>
By the way I'd still recommend the first approach. There are other ways to calculate the fee -- just improvise. See this and this for one possibility.
This would require the server-side knowing the text of the SELECT element, as only the selected value is posted back.
It's possible in ASP.NET, as the SELECT element is know server-side. However I'm not sure if this in the case in PHP.
You have to add handle onsubmit event of form and a hidden field to preserve the selected text of list item.
<script type="text/javascript">
function submitIt()
{
var list=document.getElementById("select2");
var item=list.item(list.selectedIndex);
document.getElementById("subjectTest").value=item.text;
return true;
}
</script>
<form method="post" action="your_page.php" onsubmit="return submitIt()">
<input type="hidden" id="subjectTest" name="subjectText" />
<select name="subject" id="select2"> ***(value=fees for the subject)
<option value="100">Arts</option>
<option value="150">English</option>
<input type="submit" name="cmd" value="Submit"/>
</form>
In php, when you populate your list, keep in a map the value=>label mapping and get back the right value like :
$arr = array(100 => "Arts", 150 => "English");
echo $arr[$_POST[subject]]; //if $_POST[subject]==100, return "Arts"
Otherwise, in javascript, you can get it with
var index=document.getElementById("select2").selectedIndex;
document.getElementById("select2").options[index].text;
Then you can store it in an hidden input text...
Warning :
Don't insert directly the values in the sql request !
Be carful with injection SQL
Hope this help you...
I am building an invoice system for a client and would like to give them the ability to change the order status and payment status of the order by selecting them from a select dropdown.
The forms
<form action="changestatus.php" method="post">
<input type="hidden" name="status" value="order"/>
<input type="hidden" name="orderid" value="33"/>
<select name="orderstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
</form>
<form action="changestatus.php" method="post">
<input type="hidden" name="status" value="payment"/>
<input type="hidden" name="orderid" value="33"/>
<select name="paymentstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
</form>
changestatus.php
<?php
switch($_POST['status'])
{
case"payment":
mysql_query("UPDATE orders SET payment_status = '$_POST[paymentstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
case"order":
mysql_query("UPDATE orders SET order_status = '$_POST[orderstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
}
?>
I think I can use jquery.post() (http://api.jquery.com/jQuery.post/) but I have no idea how to implement it.
/////EDIT
OK i found a solution to this.
I changed "changestatus.php" to accept GET variables
<?php
include"../inc/config.php";
switch($_GET['status'])
{
case"payment":
$query = mysql_query("UPDATE orders SET payment_status = '$_GET[paymentstatus]' WHERE ID = '$_GET[ID]'")or die(mysql_error());
if($query){echo"Saved";}else{echo"Not Saved";};
break;
case"order":
$query = mysql_query("UPDATE orders SET orderStatus = '$_GET[orderstatus]' wHERE ID = '$_GET[ID]'")or die(mysql_error());
if($query){echo"Saved";}else{echo"Not Saved";};
break;
}
?>
Then changed the forms to
<select name="orderstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
<select name="paymentstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
and finally the javascript
<script>
$(document).ready(function() {
$('#paymentstatus').change(function() {
url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=payment&paymentstatus="+$('#paymentstatus').val();
$("#payment_status_result").load(url)
});
$('#orderstatus').change(function() {
url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=order&orderstatus="+$('#orderstatus').val();
$("#order_status_result").load(url)
});
});
</script>
See http://api.jquery.com/change/ to find out how to trigger javascript when a drop-down menu is changed.
As for the jquery post, the primary reason for using this is for making an AJAX call. Whatever you do, you don't want to ever ignore the result. Your script should always return an error or success message.
First give your forms and selects ids like so:
<form action="changestatus.php" method="post" id="orderStatusForm">
<form action="changestatus.php" method="post" id="paymentStatusForm">
<select name="orderstatus" id="orderstatus">
<select name="paymentstatus" id="paymentstatus">
Then add a change observer in your script:
var changeOrderStatus = function(){
$.post("changestatus.php", $("#orderStatusForm").serialize());
}
var changePaymentStatus = function(){
$.post("changestatus.php", $("#paymentStatusForm").serialize());
}
$('#orderstatus').change(changeOrderStatus);
$('#paymentstatus').change(changePaymentStatus);
That should get you started, then read a bit more about the jQuery AJAX functions so you can handle the response from the server.
sure, use change() like this:
$("#orderStatus").change(function() { /* post() your form here */ });
and add id="orderStatus" to the order status dropdown