How to pass html select option value through url in php - php

I'm trying to pass select option value through url.selection value come from mysql table.i want to pass that driver name to another page.
<?php
//another select query goes here.
$query1= "SELECT * FROM driver WHERE status='Available'" ;
echo '<td>'.'<select name="driver">';
$result1= mysql_query($query1);
while($row1 = mysql_fetch_assoc($result1))
{
echo '<option value="'.$row1["name"].'">'.$row1["name"].'</option>';
}
echo '</select>'.'</td>';
echo'<a rel="facebox" href=db_confirm_booking.php?id='.$row["id"].'&driver='.$_POST['driver'].'>' . 'Confirm' . '</a>';
?>

Use jQuery.
Call change event of the drop down.
$(function(){
$("[name=driver]").die('change').live('change', function(){
var driver = $(this).val();
if (typeof driver !== 'undefined') {
window.location.href = 'YOUR_FILE.php?driver='+driver;
}
});
});
And
in your YOUR_FILE.php, get driver using $_GET['driver']
Or if you want the form to be more secure, take a hidden variable.
On change of driver, assign it the value of driver drop down.
And post the form.
In your PHP file, get it as $_POST['hid_driver'].

U can add onclick function for the select field, try this way
<?php
//another select query goes here.
$query1= "SELECT * FROM driver WHERE status='Available'" ;
echo '<td>'.'<select name="driver" onchange="window.location.href=\'db_confirm_booking.php?driver=' . $_POST['driver'] . '&id=\'+this.value">';
$result1= mysql_query($query1);
while($row1 = mysql_fetch_assoc($result1))
{
echo '<option value="'.$row1["name"].'">'.$row1["name"].'</option>';
}
echo '</select>'.'</td>';
echo'<a rel="facebox" href=db_confirm_booking.php?id='.$row["id"].'&driver='.$_POST['driver'].'>' . 'Confirm' . '</a>';
?>

try this code
<?php
//another select query goes here.
$query1= "SELECT * FROM driver WHERE status='Available'" ;
echo '<td>'.'<select name="driver"
onChange="window.location.href=this.value">';
$result1= mysql_query($query1);
while($row1 = mysql_fetch_assoc($result1))
{
echo '<option value="db_confirm_booking.php?id='.$row["id"].'&driver='.$_POST['driver'].'">"'.$row1["name"].'">'.$row1["name"].'</option>';
}
echo '</select>'.'</td>';
echo'<a rel="facebox" href=db_confirm_booking.php?id='.$row["id"].'&driver='.$_POST['driver'].'>
' . 'Confirm' . '</a>';
?>
example code
<select onChange="window.location.href=this.value">
<option value="www.google.com">A</option>
<option value="www.aol.com">B</option>
</select>

<select name="sel" id="sel" onchange="seturl(this.value)">
<option value=""></option>
</select>
<script>
function seturl(id)
{
document.forms[0].action="test.php?driver="+id;
document.forms[0].submit();
}
</script>
It will reload the page with driver name in url.

Related

Ajax POST to get items for a select

I have some names of places inside a select. Some of this names have apostrophe.
When user makes his choice with that select, Ajax makes a query to a table to find occurrencies of shops that match that location. My code is this:
<?php
echo "<select id='location' name='location'>";
echo "<option value='' selected>Select a location</option>";
$sql = "SELECT * FROM locations_list ORDER BY location_name;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['location_name'] . "'>". $row['location_name'] . "</option>";
}
}
echo "</select>";
echo "<select id='shop' name='shop'>";
echo "<option value='' selected='shop'>Select location first</option>";
?>
<script>
$(document).ready(function(){
$('#location').on('change',function(){
var locationID = $(this).val();
if(locationID){
$.ajax({
type: 'POST',
url: 'extract_shops.php',
data: {'location': locationID},
success:function(html){
$("#shop").html(html);
}
});
}else{
$('#shop').html('<option value="">Select a location first!</option>');
}
});
});
</script>
<?php
echo "</select>";
Things run smoothly when the locations don't have apostrophe in their name. In such cases, Ajax passes location name to extract_shops.php (which has a simple "SELECT * FROM shops WHERE location = '$passedlocationname';") and gets back a list of shops.
But when location has an apostrophed name, Ajax does not pass anything to extract_shops.php. I have put an if inside extract_shops.php that returns this info inside the second select in case Ajax doesn't pass anything.
Where am I wrong?

PHP Drop Down On Change

I have a drop down list called courses. When the user chooses a course, I should show him information about the teacher that gives this course. So, on change I need to get the value selected, and show him the results generated from an sql query.
This is my php code:
$sql= "SELECT id, course_period_id from schedule WHERE STUDENT_ID='$_SESSION[student_id]'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$course_period_id=$row["course_period_id"];
$course= DBGet(DBQuery("SELECT title FROM course_periods WHERE course_period_id='$course_period_id'"));
$options.="<OPTION VALUE=\"$course[1]['TITLE']\">".$course[1]['TITLE'].'</option>';
}
echo '</TD></TR></TABLE>';
echo "<SELECT>
<OPTION VALUE=0>Choose
$options
</SELECT>";
echo '</TD></TR></TABLE>';
I want to use "href", as I created a php file "teachers_info.php" with the following code:
if(!empty($_GET['Course']))
{
$sql="SELECT teacher_id FROM course_periods where title= '$course'";
$teacher_id= DBGet(DBQuery($sql));
$result= DBGet(DBQyery(" SELECT first_name, last_name, phone, email FROM staff WHERE staff_id = '$teacher_id[1]['teacher_id']'"));
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Phone</th>
<th>E-mail</th>
</tr>";
echo "<tr>";
echo "<td>" . $result[1]['first_name'] . "</td>";
echo "<td>" . $result[1]['last_name'] . "</td>";
echo "<td>" . $result[1]['phone'] . "</td>";
echo "<td>" . $result[1]['email'] . "</td>";
echo "</tr>";
echo "</table>";
}
How can I do this?
Thanks :)
Ok this is a bad approach, for multiple reasons:
This should be done using ajax
Don't use $_SESSION for this
I'll help you out. The first thing you need is jquery. You can find it here: http://jquery.com .
Next take a look at this picture to understand how ajax works:
In short, ajax is used to make calls to the server and update the page with the response without reloading. In your case, you will make a call to the server, send course and receive the results.
Once you have jquery set up, you have to write this:
$(function(){
$("select").on("change", function(){
var value = $(this).value(); //get the selected id
$.get("requesturl.php", {course: value}, function(){
// do something with the response
}, "json");
})
})
The requesturl.php file would look as follows:
$course = $_GET["course"]
if($course){
//execute Database query and store it in $result
echo json_encode($result);
}
you can modify this code.
myform.php
<script type="text/javascript">
$(document).ready(function(){
//Below line will get value of Category and store in id
$("#Category").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "GET",
url: "Ajax_SubCategory.php",
data: dataString,
cache: false,
success: function(html)
{
//This will get values from Ajax_SubCategory.php and show in Subcategory Select option
$("#SubCategory").html(html);
}
});
});
});
</script>
<form>
<?php
mysql_connect("localhost","root","") or die("error connect");
mysql_select_db("test") or die(" error database");
echo "<select name='Category' id='Category'>";
echo "<option value='' disabled='' selected='' >--Select Category--</option>";
$q6=mysql_query("select DISTINCT Category from category");
while($r6=mysql_fetch_array($q6))
{
echo "<option value='$r6[0]' >$r6[0]</option>";
}
echo "</select>";
echo "<select name='SubCategory' id='SubCategory'>";
echo "<option value='' disabled='' selected='' >--Select Sub Category--</option>";
echo "</select>";
?>
</form>
Ajax_SubCategory.php
<?php
mysql_connect("localhost","root","") or die("error connect");
mysql_select_db("test") or die("error database");
if($_GET['id'])
{
$id=$_GET['id'];
$sql=mysql_query("select SubCategory from category where Category='$id'");
while($row=mysql_fetch_array($sql))
{
$data=$row['SubCategory'];
echo '<option value="'.$data.'">'.$data.'</option>';
//echo "<input type='checkbox' value='".$data."' >".$data.;
}
}
?>
Well if you want the PHP to get something you ned to post something. You either add a submit button or:
echo "<select name=\"course\" id=\"course\" onchange=\"this.form.submit()\">
<OPTION VALUE=0>Choose
$options
</SELECT>";
Then you can utilize if(!empty($_GET['Course'])) {
because $_GET[] need the form to be submited.

Dynamic drop down menus

I want to create a dynamic drop down where the options of the second drop down changes after the selection of the first drop down.
The test.php file
<?php
$con=mysqli_connect("localhost","******","****","******");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM countries");
?>
<select id="country" name='country' onchange="get_states();">
<option value=''>Select</option>
<?php
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['country_id'] . "'>" . $row['country_name'] . "</option>";
}
?>
</select>
<div id="get_state"></div> // Sub will be appended here using ajax
<script type="text/javascript">
function get_states() { // Call to ajax function
var country = $('#country').val();
var dataString = "country="+country;
$.ajax({
type: "POST",
url: "getstates.php", // Name of the php files
data: dataString,
success: function(html)
{
$("#get_state").html(html);
}
});
}
</script>
<?php
mysqli_close($con);
?>
and the gestates.php is:
<?php if ($_POST) {
$country = $_POST['country'];
if ($country != '') {
$sql1 = "SELECT * FROM states WHERE country_id=" . $country;
$result1 = mysql_query($sql1);
echo "<select name='state'>";
echo "<option value=''>Select</option>";
while ($row = mysql_fetch_array($result1)) {
echo "<option value='" . $row['id'] . "'>" . $row['state_name'] . "</option>";}
echo "</select>";
}
else
{
echo '';
}
}
?>
However the above code does not work!
on change on first drop-down you need to make an ajax call which will get the options and you can then populate the next drop-down
Try adding single quotes around $country
$sql1 = "SELECT * FROM states WHERE country_id='" . $country . "'";
or
$sql1 = "SELECT * FROM states WHERE country_id='$country'";
EDIT: Also, you can only echo one result. Your second echo will be ignored by Jquery as the first will be considered a success.
You should format your results differently.
Perhaps a json encoded array.
In your php:
while($row = mysql_fetch_array($result1))
{
$data[$row['id']] = $row['state_name'];
}
echo json_encode($data);
In your Jquery set dataType: 'json'
$.each(html,function(key,value){
$("#get_state").append($('<option>',{
value:key,
text: value
}));
});

Jquery input form will not pass variable when form submitted

I'm having trouble getting my variable to post in my form. I use jquery to load a box if the user wants to enter a new sub category.
Here is the select in the form
<select id="sub_cat1" name="sub_cat1">
<option value=""></option>
<option value="new">New Sub Cat</option>
<?php
$query = "SELECT sub_category FROM blog_posts GROUP BY sub_category";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) > 0) {
while ($row = mysqli_fetch_array($data)) {
$sub_category = $row['sub_category'];
echo '<option value="' . $sub_category . '">' . $sub_category . '</option>';
}
}
?>
</select>
<div id="load_sub_cat_box"></div>
This is the jquery
$(document).ready(function(){
$("#sub_cat1").on("keyup change", function() {
var prod = $("#sub_cat1").val();
if(prod == 'new'){
$("#load_sub_cat_box").append('<div style="padding-top:15px;">Enter New Sub Category</div><input type="text" name="sub_cat2" />');
}
});
});
And here is the form validate code
if($_POST['sub_cat1'] == "new"){
$sub_cat = mysqli_real_escape_string($dbc, trim($_POST['sub_cat2']));
}
else{
$sub_cat = mysqli_real_escape_string($dbc, trim($_POST['sub_cat1']));
}
If I select new, the box does load on the page but for some reason when form is submitted it will not load a variable for $sub_cat1. Is there something I don't know about jquery and forms?
You have two select elements with id="sub_cat1".
ids must be unique per page.
It is also invalid HTML, with a select inside a select.

Passing dropdown data which is gatherd from a mysql table

I need a script that loads data form mysql and show on a drop down list. From there, I need to pass the selected data to another page.
I have done the first step. I am now able to load data from mysql table and to show them on a drop down menu. The exact is given bellow.
<?php
include("config.php");
$result= mysql_query("SELECT folder_name FROM folders");
echo '<select name="directory">'; // Open your drop down box
while ($row = mysql_fetch_array($result)) {
//echo "<option>" . $row['folder_name'] . "</option>";
echo '<option value="'.$row['folder_name'].'">'.$row['folder_name'].'</option>';
}
echo '</select>';// Close your drop down box
?>
Now I need help to pass the selected data to another page. Any suggestion please?
Let me consider the form is posted to page2.php from page1.php
page1.php
<form method="post" action="page2.php">
//your dropdown code here
<?php
include("config.php");
$result= mysql_query("SELECT folder_name FROM folders");
$str = '';
$str .= '<select name="directory">'; // Open your drop down box
while ($row = mysql_fetch_array($result)) {
$str .= '<option value="'.$row['folder_name'].'">'.$row['folder_name'].'</option>';
}
$str .= '</select>';// Close your drop down box
echo $str;
?>
<input type="submit" value="submit" />
</form>
in page2.php you can access the dropdown selected value as
$selVal = '';
if(isset($_POST['directory']))
{
$selVal = $_POST['directory'];
}
create a javascript function to handle the redirect with the folder name data:
function changePage(folder){
window.location.href= 'http://www.yourdomain.com/page2.php?folder=' + folder;
}
onchange option, trigger changePage javascript function with folder name as input:
include("config.php");
$result= mysql_query("SELECT folder_name FROM folders");
echo '<select name="directory">'; // Open your drop down box
while ($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['folder_name'].'" onchange="changePage(\''.$row['folder_name'].'\')">'.$row['folder_name'].'</option>';
}
echo '</select>';// Close your drop down box
page2.php
$folder_name = strip_tags($_GET['folder']);

Categories