I have a drop down menu with dynamically set values from the database:
When a value is selected how could I make it load a specific view dependent on what the selected option is?
I now have it loading a specific view dependent on what option is selected:
Code:
if (isset($_REQUEST['general_options'])) {
$page = strtolower(str_replace(" ", "", $_REQUEST['general_options']));
$this->load->view( $page, $data, FALSE);
}
How could I dynamically produce a form with specific questions dependent on what question is selected?
HTML:
<label for="general_options">Quote Type: </label>
<select name="general_options" id="general_options">
<option value="Graphic Design">Graphic Design</option>
<option value="Content Management System">Content Management System</option>
<option value="Shopping Cart">Shopping Cart</option>
<option value="E-Mail Marketing">E-Mail Marketing</option>
</select>
I think php will be overkill for this. I have used jquery for this. you can find the jsfiddle here
<label for="general_options">Quote Type: </label>
<select name="general_options" id="general_options">
<option value="graphic">Graphic Design</option>
<option value="content">Content Management System</option>
<option value="shopping">Shopping Cart</option>
<option value="marketing">E-Mail Marketing</option>
</select><br/>
<div id="graphic" style="display:none;" class="questions">
<p>Graphic content 1</p>
<p>Graphic content 2</p>
<p>Graphic content 3</p>
</div>
<div id="content" style="display:none;" class="questions">
<p>CMS content 1</p>
<p>CMS content 2</p>
<p>CMS content 3</p>
</div>
<div id="shopping" style="display:none;" class="questions">
<p>shopping content 1</p>
<p>shopping content 2</p>
<p>shopping content 3</p>
</div>
<div id="marketing" style="display:none;" class="questions">
<p>marketing content 1</p>
<p>marketing content 2</p>
<p>marketing content 3</p>
</div>
and the jquery Code
$(document).ready(function() {
$('#general_options').change(function() {
theVal = $("#general_options").val();
$('div.questions').each(function(){
if(this.id == theVal){
$('div.questions').hide();
$('#'+this.id).show();
}
});
});
});
For the values of option tags, instead of the text, you could use the ids. Then, you could write code(server side) to query the database with the selected id, when the user had submitted the form. If you need it an interactive manner, you could use the AJAX.
In jQuery, there is get(), post() and ajax() methods. You could use one of them.
Related
I have 3 PHP Variable values with me which i want to display depending on the HTML option selected from the drop down menu without refreshing the page.
<p>
<label for="package" class="formlbl">I want to Enroll In:</label>
<select name="package" id="package">
<option selected="selected" value=""></option>
<option value="full_package">Complete Package</option>
<option value="training">Only Training</option>
<option value="counselling">Only Counselling</option>
</select>
</p>
What i want is when he selects full package it should echo variable no 1, when he selects training it should echo variable no 2 and so on without refreshing the page.
Please tell me what i should do. The values and variable are working in the code.
Since PHP is only a server-side language once the response is sent to the client, php can no longer change anything on the page without a refresh.
You will need to use javascript in order to have content that changes without reloading a new page (also called dynamic content). You can do this really easily with a framework called jquery.
Add jquery to your webpage with this html line:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
and then use this html template for your dropdown:
<!-- make each item in your dropdown an <a> with an ID -->
<ul class="dropdown">
<li onclick="toggleResponse('comp')">Complete </li>
<li onclick="toggleResponse('pack')">Package Only </li>
<li onclick="toggleResponse('train')">Training Only </li>
<li onclick="toggleResponse('couns')">Counselling </li>
<ul>
<!-- give every response the same class, and individual IDs -->
<span class="response" id="comp"><?php echo var1 ?></span>
<span class="response" id="pack"><?php echo var2 ?></span>
<span class="response" id="train"><?php echo var3 ?></span>
<span class="response" id="couns"><?php echo var4 ?></span>
<style>
// let all the responses be hidden initially
.response {
display: none;
}
</style>
<script type="text/javascript">
function toggleResponse (id) {
// hide any responses that might be showing
$('.response').hide();
// show the response for this option
$('#' + id).show();
}
</script>
This is probably the quickest and easiest way to do it.
Fiddle example: https://jsfiddle.net/te36zy18/
HTML
<p>
<label for="package" class="formlbl">I want to Enroll In:</label>
<select name="package" id="package">
<option selected="selected" value=""></option>
<option value="full_package">Complete Package</option>
<option value="training">Only Training</option>
<option value="counselling">Only Counselling</option>
</select>
</p>
<div id="full_package" class="package"><?php echo full_package;?></div>
<div id="training" class="package"><?php echo training;?></div>
<div id="counselling" class="package"><?php echo counselling;?></div>
jQuery
$("#package").on("change", function(){
var package = $(this).val();
$(".package").hide()
$("#" + package).show();
});
CSS
.package {display: none'}
I load my content from my DB with PHP, into my div.
I have a dropdown above it, where i should be able to sort out the content, meaning that when dropdown is chosen, the old content in the div is removed, and the new appears.
So how is it possible to "remove" the old content in the div, and get the new content based on the dropdown selection ?
This is what i got so far:
<div class="col-md-4">
<ul class="nav nav-tabs top_nav">
<li role="presentation" class="active">Edit</li>
<li role="presentation" class="inactive">
<!-- THE DROPDOWN THAT SHOULD SORT THE CONTENT BELOW -->
<!-- REMOVE OLD, AND GET NEW AFTER SELECTED -->
<select id="filter_type">
<option selected value="Alle">Alle</option>
<option value="Privat_Tale">Privat Tale</option>
<option value="Erhverv_Tale">Erhverv Tale</option>
<option value="Gamle">Gamle</option>
<option value="Privat_MBB">Privat MBB</option>
<option value="Erhverv_MBB">Erhverv MBB</option>
<option value="Familietele">Familietele</option>
<option value="Retention">Retention</option>
</select>
</li>
</ul>
<!-- LOAD THE CONTENT FROM DB, AND SHOW IT -->
<div class="abo_load" id="abo_load">
<?php
if (mysql_num_rows($sql_select_edit) > 0) {
while ($row = mysql_fetch_assoc($sql_select_edit)) {
?>
<div class="abo_box" id="abo_box">
<label class="abo_navn"><?php echo $row['abo_name']; ?></label>
<input type="hidden" value="<?php echo $row['category']; ?>" name="category" />
<form action="conn/delete.php" method="post">
<input type="hidden" value="<?php echo $row['id'];
?>" name="slet">
<input class="delete" value="Delete" type="submit" onclick="return confirm('Er du sikker??\nDet ville jo være ÆV med en Fejl 40!');">
</form>
<label class="edit">Edit</label>
<?php include("files/edit_list_content.php"); ?>
</div>
<?php
}
}
?>
</div>
</div>
EDIT:
I have already made it with JQuery, i load a php page, that gets the selected item, and then get the data from the DB. But the problem is, that when its loaded, i can't use any JQuery to manipulate with the new content. Like i have a edit label on the content with a JQuery hide function on. That function is working when i get the data though php from my DB, but not when i get the data though JQuery load. Thats why i want to load the data though php
You need to bind change event on your dropdown & based upon selection, fire an ajax request to get related content & then upon receiving update content inside DIV.
If you are using jQuery, Some useful functions would be:
Ajax: http://api.jquery.com/jquery.ajax/
Binding event: https://api.jquery.com/change/
A good example of implementation has been given here:
https://stackoverflow.com/a/4910803/2004910
Im trying to get some values of my dropdown in php. Its in div, so how can I do that?
<div class="ui dropdown selection" tabindex="0" style="right: 120px;">
<div class="default text" name="dif" id="dif">Difficulty</div>
<i class="dropdown icon"></i>
<div class="menu" id="difficulty" name="difficulty">
<div class="item" value='0' data-value="0">Easy</div>
<div class="item" value='1' data-value="1">Normal</div>
<div class="item" value='2' data-value="2">Hardcore</div>
</div>
</div>
For example, if user select "Easy", I will get with php the value 0.
I tried:
$difficulty = ($_POST['difficulty']) ? $_POST['difficulty'] : '0';
You can access them in jQuery or Javascript:
http://robertnyman.com/2010/04/29/using-html5-custom-data-attributes-to-store-data-on-html-elements/
You could store the value in a hidden form and field and use .submit()
http://api.jquery.com/submit/
And then check for your post value. Divs do not store values, they are containers so adding 'value=0' will not work. But you can access attributes using JS or jQuery.
Edit: IGNORE BELOW I just saw your edit.
If you're using PHP, you can POST the data to another page.
<form action="process.php" method='post'>
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Then on the process.php page you can access the value with the superglobal: $_POST['cars']
You can't get them from a div with just PHP. I'm guessing that there is some Javascript that is updating a hidden input somewhere on the page when the user selects an option from this stylized dropdown list. You'll need to find that hidden input, and that's where your data is coming from when the form is submitted.
I need help on the following. there are 3 pages 1st is static and second is dynamic based on that third is also dynamic.
When it navigates from 1 st to 2nd it get the content as displays it but when it navigates from 2nd to 3rd it unable to load the content. But when i refresh the page it loads the content. I am not able to get what's wrong in this. Could anyone help me on this?
Javascript:
<script type="text/javascript">
$(document).ready(function () {
var b_id = getUrlVars()["b_id"];
$.ajax({
url: 'http://localhost/ajax_practice/php/get_categories.php?b_id=' + b_id,
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function (data, status) {
$.each(data, function (i, item) {
var output = "";
$(".main_category").append(output);
$(".main_category").listview("refresh");
output += "<li>";
output += "<a href='directory.html?c_id=" + item.id + "&b_id=" + b_id + "' data-transition='slide' rel='external'>";
output += item.category_name;
output += "</a>";
output += "</li>";
$(".main_category").append(output);
$(".main_category").listview("refresh");
});
}
});
});
</script>
Html page
<div data-role="page" id="page">
<div data-role="header">
<ul class="header_box" data-theme="d">
<li>
<select data-native-menu="false" name="selectmenu" id="selectmenu" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select data-native-menu="false" name="selectmenu2" id="selectmenu2" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
</li>
<li>
<a>
<img src="images/logo1.png"><span>www.justclick.co</span>
</a>
</li>
<li>
<select data-native-menu="false" name="selectmenu2" id="selectmenu3" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select data-native-menu="false" name="selectmenu2" id="selectmenu4" data-theme="d">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
</li>
</ul>
<div class="header_search">
<div class="search_input">
<input type="text" placeholder="What ? e.g. Hotel" data-theme="d">
<input type="text" placeholder="Where? eg. Area" data-theme="d">
</div>
<div class="search_button">
<a>
<img src="images/search_button.png">
</a>
</div>
<div class="clear"></div>
</div>
</div>
<div data-role="content">
<div data-role="header">
<div class="service_heading">Business Information & Services</div>
</div>
<ul data-role="listview" class="main_category"></ul>
</div>
</div>
php code
<?php header( 'Content-type: application/json');
include 'connect.php';
$b_id=$_GET[ 'b_id'];
$sql_select=mysql_query( "SELECT * FROM business_category WHERE b_id=$b_id") or die(mysql_error());
$records=array();
if(mysql_num_rows($sql_select)>0) {
while($row=mysql_fetch_array($sql_select)) {
$records[] = $row;
}
echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');';
} else {
echo 'data not selected';
}
?>
I am not able to get the content of the page when i come from previous page but i get it when i refresh the page.Why is it so?
jQueryMobile uses by default an Ajax mechanism for transitions between pages. Note that when a new page is loaded through Ajax, the scripts and styles included inside the head tag are not loaded. jQueryMobile only loads the body element of the new pages and more specifically the data-role="page" element.
However when the page is loaded through HTTP then the scripts and styles inside the head tag are normally loaded. This explains why your page appears properly when opening the page directly.
In order to resolve your issue you have to perform the following actions:
Replace $(document).ready(function() { with $(document).on('pageinit', '#your-page-id', function() {
Move your second page's script from the head tag inside the <div data-role="page"> or place the script inside a common JS file and load it inside the first page's head tag
I hope this helps.
I am using jquery mobile's multiple select: custom to try and help filter data based on the user's selection(s).
I can see that jquery mobile already updates text ON SELECT because selecting an option automatically changes the text in the select menu.
Is there a way to tap into that, so that I can send the value of the selected options to a php page through AJAX and return results via JSON?
<form id="target" method="post">
<div data-role="fieldcontain">
<label for="select-choice-attunement" class="select ui-hidden-accessible">Filter Attunement</label>
<select data-mini="true" name="select-choice-attunement" id="select-choice-attunement" multiple="multiple" data-native-menu="false">
<option>Filter by Element</option>
<option value='1'>fire</option>
<option value='2'>water</option>
<option value='3'>earth</option>
<option value='4'>light</option>
<option value='5'>darkness</option>
</select>
</div>
</form>
To make it a bit more clear.
From the list above, you can see there are only 5 choices. The user might finish after 1, or all 5. The results need to mirror what they chose, preferably as fast as possible (which is why I would like to hook to jquery mobiles update). So if the choose 'fire' it will immediately filter via the called php function and returned json list, the necessary list items.
Lets imagine that I wanted to update the following with a new array of list items:
View:
<div id='change_with_ajax'>
<ul>
<?php
foreach ($ajaxretrievedarray as $array)
{
echo "<li>".$array['name']."</li>";
} ?>
</ul>
</div>
You may check the below example.
When the change event is triggered, the selected values are displayed inside an alert box.
<!DOCTYPE html>
<html>
<head>
<title>Select Choice</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
$(document).on('change', '#select-choice-attunement', function(e){
alert($(this).val());
});
</script>
</head>
<body>
<!-- /page -->
<div data-role="page">
<!-- /header -->
<div data-role="header" data-theme="b">
<h1>Select Choice</h1>
</div>
<!-- /content -->
<div data-role="content">
<div data-role="fieldcontain">
<label for="select-choice-attunement" class="select ui-hidden-accessible">Filter Attunement</label>
<select data-mini="true" name="select-choice-attunement" id="select-choice-attunement" multiple="multiple" data-native-menu="false">
<option>Filter by Element</option>
<option value='1'>fire</option>
<option value='2'>water</option>
<option value='3'>earth</option>
<option value='4'>light</option>
<option value='5'>darkness</option>
</select>
</div>
</div>
</div>
</body>
</html>
I hope this helps.