I have 2 functions in PHP, one of them displays the ISBN and the title of a book and the other displays the editions that exist in the database based on the ISBN selected on the previous selection.
Here are the 2 functions:
ISBN - Book dropdown list:
<?php include ("includes/connections.php");
function dropdown($intIdField, $strNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
echo "<select name=\"$strNameOrdinal\" onchange=\"selection($id)\">\n";
echo "<option value=\"NULL\">Select Value</option>\n";
$strQuery = "select $intIdField, $strNameField
from $strTableName
order by $strOrderField $strMethod";
$rsrcResult = mysql_query($strQuery);
while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
$strA = $arrayRow["$intIdField"];
$strB = $arrayRow["$intIdField"] . " - " . $arrayRow["$strNameField"];
echo "<option value=\"$strA\">$strB</option>\n";
}
echo "</select>";
}
?>
Edition dropdown list:
<?php include ("includes/connections.php");
function dropdownEdition($intId1Field, $intId2Field, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
$intId2Field = $GLOBALS['book'];
var_dump($intId2Field);
var_dump($_POST["book"]);
echo "<select name=\"$strNameOrdinal\">\n";
echo "<option value=\"NULL\">Select Value</option>\n";
$strQuery = "SELECT $intId1Field, $intId2Field
FROM $strTableName
ORDER BY $strOrderField $strMethod";
$rsrcResult = mysql_query($strQuery);
while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
$strA = $arrayRow["$intId1Field"];
echo "<option value=\"$strA\">$strA</option>\n";
}
echo "</select>";
}
?>
What I have been trying to do is pass the ISBN selected on the previous selection with a onchange function which would return the ISBN of the book but it failed a lot.
<?php
function selection($id){
echo $id;
}
?>
I know I'm terrible at this but I don't know what else to do if you could point me to a direction it would be much appreciated.
I would prefer a PHP solution rather than a JavaScript one if possible.
You are trying to call a PHP function via the onchange event.
You'll need to write a JavaScript function to make an AJAX call to the PHP file in order to get the result.
You can easily make the AJAX request with jQuery
$.get({
url: "/selection.php",
data: {
id: "ISBN HERE"
},
success: function(data) {
alert(data)
}
})
You would also have to add something like this to the PHP file to display the result
echo selection($_GET["id"]);
The first page, which has the list of books and a Go button
<form action="page2.php" method="get">
<!-- book selection list -->
<input type="submit" value="Go">
</form>
The second page could list all the editions for the book with the ID $_GET[$strNameOrdinal]
To do this on the same page, with a reload, you need to check if the form has been submitted
<?php
if (empty($_GET[$strNameOrdinal])) {
// List Books
} else {
// List editions
}
?>
Related
I'm pretty new to Php and web development. What I'm trying to do here is besides fetching the value of selected id pck which is rate_perhour, I also want to fetch the value of option pack_id to POST on an insert file.
<?php
include ("dbconn.php");
$data = mysqli_query($db,"SELECT * FROM packages");
$cek = mysqli_num_rows($data);
$select= '<select id="pck" name="pck" class="form-control">';
$select.='<option value="">-- Select Package --</option>';
while($rs=mysqli_fetch_array($data,MYSQLI_ASSOC))
{
$select.='<option value="'.$rs['rate_perhour'].'">'.$rs['pack_id'].'</option>';
}
$select.='</select>';
echo '<input type="hidden" name="packid" id="packid">';
echo "<span class=style7>".$select."</span>";
?>
I'd tried the script to pass value into the hidden input to hold the id but didn't work as well.
<script>
$(document).ready(function()
{
$("#pck").change(function()
{
$("#packid").val(("#pck").find(":selected").text());
});
});
</script>
Parts of the POST method. It can get the Selected Value(pck) but not Option(packid)
<php
include ("dbconn.php");
if(isset($_POST['cuid']))
{
//Insert into 'reservation' table
$pack =$_POST['packid']; // Option
$rate =$_POST['pck']; // Selected Value
}
else
{
} ?>
I need to make drop down list as a link to different pages. How do I do that using PHP, MySQL and HTML.
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
$sql="select first_name from users";
$result=mysql_query($sql);
echo "<select First_name=''>";
echo "<a href='index.html'>";
while($row=mysql_fetch_array($result)){
echo ":<option value='".$row['first_name']."'>".$row['first_name']."</option>";
}
echo"</a>";
echo"</select>";
?>
You can't use links on the option tag, in order to do that, you need to use javascript.
You can try to do something like this:
echo "<select name=\"First_name\" onchange=\"document.location='?'+this.value\">";
PHP is a server-side script and does not manipulate a page after a user has adjusted it. Like real time. Only javascript and others do that. PHP creates a page with what you want to see but if you need to change something bases on a dropdown use java. Here is a function that can do that. It unhides a div tag that can have your info you need.
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('dropdown');
var divtag1 = document.getElementById('divtag1');
var divtag2 = document.getElementById('divtag2');
eSelect.onchange = function() {
if(eSelect.selectedIndex === 1) {
divtag1.style.display = 'block';
}
if(eSelect.selectedIndex === 2) {
divtag2.style.display = 'block';
}//or if you want it to open a url
if(eSelect.selectedIndex === 3) {
window.open("https://yourwebsite.com", "_NEW");
}
}
}
</script>
echo "<div id=\"divtag1\" style=\"display:none;\">/*your code*/
</div>";
echo "<div id=\"divtag2\" style=\"display:none;\">/*your code*/
</div>";
I have combobox named catid, which is filled with categories. On change, I am filling the content on other
2 comboboxes and I am adding checkboxes in my form, which are all based on selected category.
So three comboboxes in total, categories (#catid), subcategories (#catid2) and manufacturers (#manufacturerid)
in this order.
When there are no subcategories, everything is nice and smooth. In the subcategory combobox, i get the message
there are no subcategories, i am filling the manufacturers combobox with manufacturers from that category,
and i am drawing the comboboxes again related to this category.
Problem arise when sub category exist. I am trying using on change event to fetch my checkboxes, related to that subcategory.
I get undefined value.
Here is my code:
Controller:
public function __construct() {
parent::__construct();
$this->load->model('addsCategoriesModel');
$this->load->model('addsTypesModel');
$this->load->model('colorsModel');
$this->load->model('geographicAreasModel');
$this->load->model('categoriesFiltersModel');
}
// function index has been ommited for the sake of simplicity
function get_dropdown_values($catid){
$this->load->model('manufacturersModel');
$this->db->select('manufacturerid,manufacturername');
$this->db->where("catid = $catid");
$result = $this->manufacturersModel->get();
//echo form_dropdown('manufactirerid', $result, NULL, 'id="manufacturerid"');
echo "<select name='manufacturerid' id='manufacturerid' class='styled_select'>";
//"<option value=".$result[]['manufacturerid'].">".$result[]['manufacturername']
foreach ($result as $key=>$value) {
echo "<option value=".$key['manufacturerid'].">".$value['manufacturername']."</option>";
}
echo "</select>";
}
function get_dropdown_values2($catid){
// create the second drop down, where parent id is the selected category
$this->db->select('catid,parentid,categoryname');
$this->db->where("parentid = $catid");
$result2 = $this->addsCategoriesModel->get();
if(count($result2) > 0){
echo "<select name='catid2' id='catid2' class='styled_select'>";
foreach ($result2 as $key=>$value) {
echo "<option value=".$key['catid'].">".$value['categoryname']."</option>";
}
echo "</select>";
} else {
echo "<select name='catid2' id='catid2' class='styled_select'>";
echo "<option value='0'>No subcategories</option>";
echo "</select>";
}
}
function get_categories_filters($catid3){
// create the checkboxes according to selected category
// case 1: if the category id is NOT 4
$this->db->where("catid = $catid3");
$result3 = $this->categoriesFiltersModel->get();
if(count($result3) > 0){
// start the foreach loop
foreach($result3 as $key=>$value){
echo "<div class='chb_group'>";
echo "<span class='custom_chb_wrapper'>";
echo "<input type='checkbox' name=" .$value['filterid'] ." class='zcheckbox'/>";
echo "<label>" .$value['filtername']. "</label>";
echo "</span>";
echo "</div>";
}
} else {
$result3 = "No checkboxes in this category";
echo $result3;
//$this->get_categories_filters2(22);
}
}
function get_categories_filters2($catid4){
// create the checkboxes according to selected category
$this->db->where("catid = $catid4");
$result4 = $this->categoriesFiltersModel->get();
if(count($result4) > 0){
// start the foreach loop
foreach($result4 as $key=>$value){
echo "<div class='chb_group'>";
echo "<span class='custom_chb_wrapper'>";
echo "<input type='checkbox' name=" .$value['filterid'] ." class='zcheckbox'/>";
echo "<label>" .$value['filtername']. "</label>";
echo "</span>";
echo "</div>";
}
} else {
//$result4 = "No checkboxes in this category";
$result4 = "Are you in filters2??";
echo $result4;
}
}
}
Here is my view:
<select name="catid" id="catid" class="styled_select" onchange="load_dropdown_content($('#manufacturerid'), this.value);load_dropdown_content2($('#catid2'), this.value);load_dropdown_content3($('#catid3'), this.value);">
<option value="0">Please select</option>
<?php
// foreach loop to fetch the categories
foreach ($adds_categories as $category){
echo "<option value=".$category['catid'].">".$category['categoryname']."</option>";
}
?>
</select>
</div>
<div class="select_wrapper">
<label><strong>Subcategory: </strong></label>
<select name="catid2" id="catid2" class="styled_select" onchange="load_dropdown_content4($('#catid3'), this.value);">
<option value="0">Please select</option>
</select>
</div>
For the sake of simplicity, I am not showing here the manufactirer combobox, with which i have no problem whatsoever and the checkboxes
And the javascript:
<script type="text/javascript">
function load_dropdown_content(field_dropdown, selected_value){
var result = $.ajax({
'url' : '<?php echo site_url('submit/get_dropdown_values'); ?>/' + selected_value,
'async' : false
}).responseText;
field_dropdown.replaceWith(result);
}
function load_dropdown_content2(field_dropdown, selected_value){
var result2 = $.ajax({
'url' : '<?php echo site_url('submit/get_dropdown_values2'); ?>/' + selected_value,
'async' : false
}).responseText;
field_dropdown.replaceWith(result2);
}
function load_dropdown_content3(field_dropdown, selected_value){
if(selected_value == 4){
load_dropdown_content4();
}
var result3 = $.ajax({
'url' : '<?php echo site_url('submit/get_categories_filters'); ?>/' + selected_value,
'async' : false
}).responseText;
$('#catid3').empty(); // DIV CATID3 IS THE DIV WHERE CHECKBOXES ARE CREATED
$("#catid3").append(result3);
}
function load_dropdown_content4(field_dropdown, selected_value){
var result4 = $.ajax({
'url' : '<?php echo site_url('submit/get_categories_filters2'); ?>/' + selected_value,
'async' : false
}).responseText;
$('#catid3').empty(); // DIV CATID3 IS THE DIV WHERE CHECKBOXES ARE CREATED
$("#catid3").append(result4);
alert(result4);
}
</script>
If anyone can help me to overcome that undefined error which I am getting from subcategories combobox, I will deeply appreciated.
Regards...
Instead of $("$cat3"), you can do like below also.
onchange = 'load_dropdown_content4("cat3",this.value)'
In load_dropdown_content4
function load_dropdown_content4(field_dropdown, selected_value)
{
//replace field_dropdown.replaceWith(result2);
$("#"+field_dropdown).replaceWith(result2);
}
Try this code.
I am pulling out record from the database and inserting them inside a dropdown like this:
echo "<select>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option>" . $drow['id'] . "</option>";
}
echo "</select>";
It works but I need to be able to click on an option on the dropdown and make it link just like:
Record1Here
Record2Here
Record3Here
UPDATE: Latest code:
<script>
function doSomething() {
var currentval = this.options[this.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
</script>
...
echo "<select onchange='doSomething();'>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option value=\"view.php\">" . $drow['id'] . "</option>";
}
echo "</select>";
You can't place anchors on an option within a select list.
What you can do is use JavaScript and then do something on the change event of the select list :
echo "<select onchange='doSomething(this);>';
then in JavaScript do something based on the selected value :
function doSomething(elem) {
var currentval = elem.options[elem.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
Example here
you could update your PHP code to include a value in each option :
echo "<option value=\"urlhere.php\">" . $drow['id'] . "</option>";
I am still a beginner with PHP. I am having a problem with drop downs and links. I want to be able to choose an option in the drop down menu and it go to the specified page. For some reason only the links for the second drop down menu. (MUNICIPALITIES) will work correctly by linking to the specific municipality page. The crime drop down menu will not link to the specific crime page. I have included the code for the crime drop down and municipality drop down. I have also included the script below.
Ultimately my question is why aren't both drop downs functioning correctly with the link?
<script language="JavaScript" type="text/javascript">
function gopage(theLink) {
if (document.dropdown.theLink.value != "") {
location.href = document.dropdown.theLink.value;
}
}
</script>
<form name="dropdown">
<select name="theLink" onchange="gopage(theLink)">
<option value="ALL">Choose Crime associated with the Gang</option>
<?php
//echo 'NOWWWWWWWWWWWWWWWWWWWWWW';
if ($length3 <> 0) {
for ($m = 0; $m < $length3; $m++) {
$rows = $resultset3[$m][crime_name];
$trackchoices = $rows;
$options2 = "<option value=\"crimesmain.php?crime=$trackchoices\">$trackchoices</option>";
echo "$options2";
}
}
else if ($length3 == 0) {
$trackanswer = "NO CRIMES";
$options5 = "<option value=\"$trackanswer\">$trackanswer</option>";
echo "$options5";
}
?>
</select>
</form>
<br>
<br>
<form name="dropdown">
<select name="theLink" onchange="gopage(this)">
<option value="ALL">Choose Municipality associated with the Gang</option>
<?php
if ($length12 <> 0) {
for ($q = 0; $q < $length12; $q++) {
$rows2 = $resultset6[$q][municipality_name];
$trackchoices2 = $rows2;
//try
$options3 = "<option value=\"municipalitymain.php?mun=$trackchoices2\">$trackchoices2</option>";
//echo "<a href='municipalitymain.php?mun=$options3'>";
echo "$options3";
}
}
else if ($length12 == 0) {
$trackanswer = "NO MUNICIPALITY";
$options6 = "<option value=\"$trackanswer\">$trackanswer</option>";
echo "$options6";
}
?>
</select>
</form>
I think a better solution would be to pass the id of the select element to gopage so your function looks like this
function gopage(elId)
{
if (document.getElementById(elId).value != "") {
location.href = document.getElementById(elId).value;
}
}
First Dropdown
<select name="theLink" id="crime" onchange="gopage('crime')">
Second Dropdown
<select name="theLink2" id="muni" onchange="gopage('muni')">