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'}
Related
I have created a form with multiple fields like input type name, checkboxes and also a dropdown. My code for dropdown:
<div class="container">
<form action="selecttest.php" method="post">
<div class="dropdown">
<div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
<ul class="dropdown-menu myclass numberom1" aria-labelledby="menu1" name="thenumbers">
<li value="one">One</li>
<li value="two">Two</li>
<li value="three">Three</li>
<li value="four">Four</li>
<li value="five">Five</li>
</ul>
</div>
<input type="submit" name ="submit"/>
I want to get the value of the selected <li> in the selecttest.php. The following code is not working:
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
How can I get the value of the dropdown in the php page? There are other elements in the form as mentioned above so i cannot use jquery onclick event. And this is a non ajax form.
Thanks.
A dropdown menu is not the same as a select input.
Within a form, the syntax for a select input (like yours, within Bootstrap) would be:
<label for="select_1">Select list:</label>
<select class="form-control" id="select_1" name="thenumbers">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
So long as the select has an attribute name with value "thenumbers", and the select is within the form that is being submitted, you will be able to get the value of the selected element in PHP with:
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
To use the Bootstrap dropdown as a faux select, you can use JavaScript. Here is an example with jQuery:
HTML
<!-- Create dropdown and add specific class '.thenumbers' to ul element -->
<div class="dropdown">
<div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
<ul class="dropdown-menu myclass numberom1 thenumbers" aria-labelledby="menu1" name="thenumbers">
<li value="one">One</li>
<li value="two">Two</li>
<li value="three">Three</li>
<li value="four">Four</li>
<li value="five">Five</li>
</ul>
</div>
<!-- Create a hidden input -->
<input type='hidden' name='thenumbers'>
jQuery
$(function(){
//Listen for a click on any of the dropdown items
$(".thenumbers li").click(function(){
//Get the value
var value = $(this).attr("value");
//Put the retrieved value into the hidden input
$("input[name='thenumbers']").val(value);
});
});
That way (as long as the hidden input is within the form), when the form is submitted, the "thenumbers" input value will be the last selected dropdown option.
Your form is not submitting because there is no submit button.Try this code.
HTML CODE
<form action="selecttest.php" method="post">
<div class="dropdown" >
<select class="form-control" name="thenumbers">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
</div>
<input type="submit" name ="send"/>
</form>
PHP CODE
if(isset($_POST['send']))
{
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
}
When using Input group-bootstrap, you can get values in the PHP page by writing your code like this:
HTML/Bootstrap
<select name = "thenumbers" class="btn btn-outline-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<option value ="one" name ="one">One</option>
<option value ="two" name ="two">Two</option>
<option value ="three" name ="three">Three</option>
</select>
<input class="form-control border-secondary py-2" name="searchterm" type="text" >
<div class="input-group-append">
<button class="btn btn-primary" name="submit" type="submit" value ="su bmit">
<i class="fa fa-search"></i>
</button>
</div>
PHP code:
<?php
if(isset($_POST['submit']) && !empty($_POST['searchterm'])){
if(isset($_POST['thenumbers']){
$val = $_POST['thenumbers'];
echo $val;
}
}
You can remove the form element and use hyperlinks with url query string parameters. When a user clicks an option a GET request is made to the PHP file.
<ul class="dropdown-menu myclass numberom1" aria-labelledby="menu1">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
In your PHP script, use $_GET to retrieve the query string variable.
$val = $_GET["thenumbers"];
echo "the Value selected is ".$val;
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 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.