Fetch Bootstrap Dropdown Value OnSubmit - php

Dropdown made using Bootstrap
<form id="search" action="" method="post" >
<div class="dropdown">
<button data-toggle="dropdown" id="dropdownMenu1" type="button" class="btn btn-default dropdown-toggle">Fixed <span class="caret"></span></button>
<ul aria-labelledby="dropdownMenu1" role="menu" class="dropdown-menu">
<li role="presentation">Florida</li>
<li role="presentation">Texas</li>
<li role="presentation">Washington</li>
<li role="presentation">New York</li>
<li role="presentation">Ohio</li>
</ul>
</div>
</form>
jQuery to keep dropdown option selected
var jQee = jQuery.noConflict();
jQee(".dropdown-menu li").click(function(){
var selText = jQee(this).text();
jQee(this).parents('.dropdown').find('.dropdown-toggle').html(selText+' <span class="caret"></span>');
//jQee('#search').submit();
});
HTML Dropdown
<select onchange="form.submit()" name="template">
<option value="F" selected="true">Florida</option>
<option value="T">Texas</option>
<option value="W">Washington</option>
<option value="N">New York</option>
<option value="O">Ohio</option>
</select>
For the above dropdown, I am fetching the option value based on user selection.
While doing it with Bootstrap, I am able to keep the the dropdown value selected, but as I am using it with PHP, I also need the value in $_POST variable in order to execute SQL query and display results based on what value is selected from dropdown.
How can I achieve it?

Use a hidden field and populate it when the user selects a state.
HTML
<form id="search" action="" method="post" >
<input type="hidden" name="selection">
<div class="dropdown">
<button data-toggle="dropdown" id="dropdownMenu1" type="button" class="btn btn-default dropdown-toggle">Fixed <span class="caret"></span></button>
<ul aria-labelledby="dropdownMenu1" role="menu" class="dropdown-menu">
<li role="presentation">Florida</li>
<li role="presentation">Texas</li>
<li role="presentation">Washington</li>
<li role="presentation">New York</li>
<li role="presentation">Ohio</li>
</ul>
</div>
</form>
JavaScript
$(document).ready(function() {
$("ul li a").click(function() {
text = $(this).closest("li").text();
$("input[name='selection']").val(text);
$(this).parents('.dropdown').find('.dropdown-toggle').html(text+' <span class="caret"></span>');
$("#search").submit();
});
});
See jsfiddle Use inspect element to check the value of the hidden field.

Related

Cannot get value of bootstrap dropdown on Submit

I have the following code of which i want to get the value of the text displayed on the bootstrap dropdown button when i click on the submit button, and also the i want the POST to be sent to a codeigniter controller to perform certain operations on it.
I need help please.
<form method="post" action="#" id="user-order-form">
<div class="users-order col-md-5">
<label class=""> <strong>ORDER BY </strong></label>
<div class="dropdown form-control" style="padding:0px; margin-left: 20px; margin-right:10px">
<button class="btn btn-default dropdown-toggle form-control" type="button" id="user-order-dropdown" name="user-order-dropdown" data-toggle="dropdown">
Surname
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelled-by="user-order-dropdown" id="capton">
<li>Surname </li>
<li>First Name </li>
<li>Company Name </li>
</ul>
</div>
<label class="radio-inline"><input type="radio" name="order-radio" checked>ASC</label>
<label class="radio-inline"><input type="radio" name="order-radio">DESC</label>
<button type="submit" class="btn" name="order-submit-btn" id="order-submit-btn">Order</button>
</div>
</form>
This is the jquery code that displays the selected dropdown item:
$('.dropdown').each(function (key, dropdown) {
var $dropdown = $(dropdown);
$dropdown.find('.dropdown-menu a').on('click', function () {
$dropdown.find('button').text($(this).text()).append(' <span class="caret"></span>');
});
});
To get the value of dropdown: You need to create a hidden input field and every time the value of dropdown changes, you need to update
it.
To get the POST data in the controller: You need to create a controller(obviously) and a function within that controller. Then
give that URL to the form action attribute.
I've written a reference code for you with necessary explaining in the comments, see if it helps you.
View
<form method="post" action="<?php echo base_url('some_controller/some_function');?>" id="user-order-form">
<!-- give the action where controller name(some_controller) and function name(some_function) -->
<div class="users-order col-md-5">
<label class=""> <strong>ORDER BY </strong></label>
<div class="dropdown form-control" style="padding:0px; margin-left: 20px; margin-right:10px">
<button class="btn btn-default dropdown-toggle form-control" type="button" id="user-order-dropdown" name="user-order-dropdown" data-toggle="dropdown">
Surname
<span class="caret"></span>
</button>
<!-- make a hidden field, give it a default value(which is initially selected), give it a name(it will be used to get the post data)-->
<input type="hidden" name="dropdown" value="Surname"id="dropdown_input"/>
<ul class="dropdown-menu" aria-labelled-by="user-order-dropdown" id="capton">
<li>Surname </li>
<li>First Name </li>
<li>Company Name </li>
</ul>
</div>
<label class="radio-inline"><input type="radio" name="order-radio" checked>ASC</label>
<label class="radio-inline"><input type="radio" name="order-radio">DESC</label>
<button type="submit" class="btn" name="order-submit-btn" id="order-submit-btn">Order</button>
</div>
</form>
JQuery
$('.dropdown').each(function (key, dropdown) {
var $dropdown = $(dropdown);
$dropdown.find('.dropdown-menu a').on('click', function () {
$dropdown.find('button').text($(this).text()).append(' <span class="caret"></span>');
$('#dropdown_input').val($(this).text()); // change the value of hidden input field
});
});
Controller(Some_controller.php)
function some_function(){
$dropdown = $this->input->post('dropdown'); // get dropdown value
$order_radio = $this->input->post('order-radio'); // get radio value
/* Do whatever you want to with that data here(Eg - Save in DB) -- your logic */
}

How use JQuery closest() in Post and comment system

I have a post and comment system. Something like facebook post and comment system. The post display properly but the comments only display for the first post i.e the post displayed at the top. Am on able to submit comment on all other posts except the first post.
The problem on which I need your assistance is as follows:
-The comments for each of the posts should display correspondently.
-To be able to submit comments for the other posts.
These what I have done.
View:
<div class="box-footer" style="display: block;">
<form id="com" class="com" method="post">
<div class="img-push">
<input type="hidden" class="status_id" id="status_id" name="status_id" value="<?php echo $post['spid']; ?>">
<textarea name="comment" id="comment" class="form-control input-sm comment" placeholder="Press enter to post comment"></textarea>
<div class="box-footer box-form">
<btn class="btn btn-azure btn-sm pull-right commentbt" id="commentbt">Comment</btn>
<ul class="nav nav-pills">
<li><i class="fa fa-bullhorn"></i></li>
</ul>
</div>
</div>
</form>
</div>
<?php endforeach;?>
Jquery:
$(document).ready(function(){
$(".commentbt").click(function(){
var comment = $(this).closest("div.img-push").find("input[name='comment']").val();
alert(comment);
});
});
All am getting is "Undefined"
I need to get the value of the input field and the textarea. Thanks
I can notice, initially, the next errors on your code:
1) You forgot the close div for <div class="box-footer" style="display:block;">
2) The attribute name of the input element is status_id, not comment
3) The usage of the same ID attribute for multiple elements.
4) On JQuery, I will replace the search of the input by matching by class. Also, I added the search of textarea value too.
So, try next modifications:
PHP
<?php foreach ($statuspost as $post): ?>
<div class="box-footer" style="display:block;">
<form class="com" method="post">
<div class="img-push">
<input type="hidden" class="status_id" name="status_id" value="<?php echo $post['spid'];?>">
<textarea name="comment" class="form-control input-sm comment" placeholder="Press enter to post comment"></textarea>
<div class="box-footer box-form">
<btn class="btn btn-azure btn-sm pull-right commentbt">Comment</btn>
<ul class="nav nav-pills">
<li><i class="fa fa-bullhorn"></i></li>
</ul>
</div>
</div>
</form>
</div>
<?php endforeach:?>
JQuery
$(document).ready(function()
{
$(".commentbt").click(function()
{
var statusID = $(this).closest("div.img-push").find("input.status_id").val();
alert(statusID);
var comment = $(this).closest("div.img-push").find("textarea.comment").val();
alert(comment);
});
});

Pass selected li element with form

How can I pass with form what is selected from dropdown field? So that url goes to ?s=sdf&tag=bookor ?s=sdf&tag=newspaper in this example
http://codepen.io/filaret/pen/yazXKQ
I couldn't create my design goal with <select><option>... inside input element so I used Bootstrap dropdown with .input-group-btn.
<form action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="input-group">
<input type="text">
// SELECT OPTIONS
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><i class="fa fa-book"></i></a></button>
<ul class="dropdown-menu">
<li><i class="fa fa-book"></i></li>
<li><i class="fa fa-newspaper-o"></i></li>
</ul>
</div>
// SUBMIT BUTTON
<div class="input-group-btn">
<button type="submit" class="btn btn-default" data-toggle="dropdown">Submit</button>
</div>
</div>
</form>
I managed to make it work with this
<input id="search-hidden" type="hidden" value="">
$selectWrapper.find(".dropdown-menu li").click(function() {
var $selectedIcon = $(this).find('.fa');
$selectWrapper.find("#search-hidden").val( $selectedIcon.data('value') );
});

Using jQuery inside a PHP page while using a HTML form

Basically, what I'm trying to do is use jQuery UI widgets such as datepicker and selectable inside my page book.php. Inside the page I'm using a HTML form. For some reason, when I try to use datepicker and selectable, it's just not working?? On previous pages I have gotten jQuery to work, but for some reason it won't work on this page!
This is my HTML form:
<form method="post" action="homepage_loggedin_book_andrea.php">
Your appointment ID is:
<?php
$test = mysql_result(mysql_query("SELECT MAX(id) FROM appointments"), 0);
echo $test;
?>
<br>
<p>Please enter the appointment ID: <input type="text" value="<?php echo $result['id']; ?>" name="id"></p>
<p>Date: <input type="text" id="#datepicker" name="slot_date"></p>
<br>
Blue dots are lines when Andrea is unavailable.
<img src="Timetable_andrea.jpg" width="400" height="251" alt=""/><br>
<label for="slot_line" name="slot_line" id="selectable">Select a line</label>
<ol id="selectable" name="slot_line">
<li class="ui-widget-content">Line 1</li>
<li class="ui-widget-content">Line 2</li>
<li class="ui-widget-content">Line 3</li>
<li class="ui-widget-content">Line 4</li>
<li class="ui-widget-content">Line 5</li>
<li class="ui-widget-content">Line 6</li>
<li class="ui-widget-content">Line 7</li>
</ol>
<br>
<br>
<p>Reason for appointment: <input type="text" value="<?php echo $result['reason']; ?>" name="reason" style="width: 300px;"></p>
<div class="form-group">
<button type="submit" class="btn btn-block btn-primary" name="btn-book">Book</button>
</div>
</form>
As you can see, I just the ID datepicker and selectable where I want the jQuery to work. I have the jQuery code on a seperate page (which I have targeted in my this HTML page)
jQuery code:
$(document).ready(function(){
$( "#datepicker" ).datepicker();
$( "#menu" ).selectmenu();
$( "#selectable" ).selectable();
});
All I need to work is the datepicker and selectable in my PHP code? I'm not sure as to why it's not working.. Someone please help!

Submit button that goes to a url based on user form input PHP

I have a dropdown search box and I want it so that when the user clicks "Check", it will go to http://example.com/check/UserInput. Is this possible to do with php/html?
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="glyphicon glyphicon-search"></i>
</a>
<ul class="dropdown-menu" style="padding:12px;">
<form class="form-inline">
<input type="text"
class="searchAddress2 form-control pull-left"
placeholder="Address">
<button type="submit"
class="searchAddress btn btn-default pull-right">Check</button>
</form>
</ul>
</li>
For example, the user clicks the search icon, the dropdown appears, they enter "alksndlkasn" in the input form, and they click check. They are now sent to http://example.com/check/alksndlkasn.
I tried this but it isn't working.
<form class="form-inline" action="https://example.com/check/text">
<input id="text"
type="text"
class="searchAddress2 form-control pull-left"
placeholder="Address">
<button type="submit"
class="searchAddress btn btn-default pull-right">Check</button>
</form>
well you can post user input and then if it is not empty go to a link with your prefix .
simple code
$add = !empty( $_POST['address']) ? $_POST['address'] : "--";
if($add !== "--" ) {
$go = "http://example.com/check/$add";
header("Location: $go");
}
This can be done with HTML itself.
There is an attribute named action for the form tag.
The action attribute specifies where to send the form-data when a form
is submitted.
As an example, see this link
Thus, For the submit button to redirect to a page, you should specify an action for the form tag
So, Replace your form as below
<form class="form-inline" action="your_file.php">
<input type="text" class="searchAddress2 form-control pull-left" placeholder="Address">
<button type="submit" class="searchAddress btn btn-default pull-right">Check</button>
</form>
where your_file.php is the page to which it has to be redirected.
Read more about HTML form in the docs
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="glyphicon glyphicon-search"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" style="padding:12px;">
<input type="text" class="searchAddress2 form-control pull-left" placeholder="Address" id="text">
<input type="hidden" name="url" id="url" value="http://example.com/check/">
<button type="submit" class="searchAddress btn btn-default pull-right"
onclick="window.location.replace($('#url').val()+$('#text').val())">Check</button>
</ul>
</div>
You dont need php to do that, look at this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="glyphicon glyphicon-search"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" style="padding:12px;">
<input type="text" class="searchAddress2 form-control pull-left" placeholder="Address" id="text">
<input type="hidden" name="url" id="url" value="http://example.com/check/">
<button type="submit" class="searchAddress btn btn-default pull-right"
onclick="window.location.replace($('#url').val()+$('#text').val())">Check</button>
</ul>
</div>
I'm not sure exactly how you want it to look, but here is a simple example of a little HTML and Javascript that lets the user select something in a form and then redirects the user to the selection. Another let's the user type in something then click a button to be redirected.
It may not do exactly what you're describing, but the javascript demonstrates how to redirect a user based on a selection in a form. You can change the form controls as you like but the concept is still the same.
<HTML>
<HEAD>
<script language="javascript" type="text/javascript">
function changeFunc()
{
var tmp = document.getElementById("myList").value;
window.location = tmp;
}
function goFunc()
{
var tmp1 = "http://www.example.com/check/";
tmp1 += document.getElementById("myinput").value;
window.location = tmp1;
}
</SCRIPT>
</HEAD>
<BODY>
<form>
<select id='myList' onchange='changeFunc()'>
<option value="default">Default</option>
<option value="http://cnn.com">CNN</option>
<option value="http://www.foxnews.com">Fox News</option>
</select>
</form>
<form>
<input type="text" id="myinput"></input>
</form>
<button onclick="goFunc()">Go!</button>
</BODY>
</HTML>

Categories