Submit selection from Bootstrap dropdown menu to $_POST - php

My goal is to submit the users selection from a bootstrap dropdown menu to $_POST. As I was researching this question I came across this (bootstrap input field and dropdown button submit)
My question is very similar to this one. However, when I tried what the answer to that question recommended it did not work for me for several reasons.
My Code
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
Programming Cards
<!--Creats button for navigation when window gets too small-->
<button class = "navbar-toggle" data-toggle = "collapse" data-target= ".navHeaderCollapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class="nav navbar-nav navbar-right">
<li>Upload</li>
<li>
<form action="index.php" method="POST" id="my_form">
<input type="hidden" name="topic" id="topic">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Topic Select<b class="caret"></b>
<ul class="dropdown-menu">
<li>Introduction</li>
<li>Methods</li>
<li>Loops</li>
<li>Conditional Logic</li>
</ul>
</a>
</li>
</form>
<script>
$(function()
{
$('.dropdown-menu li').click(function()
{
$('#my_topic').val($(this).html());
$('#my_form').submit();
});
});
</script>
</li>
</ul>
</div>
</div>
</div>
Aspects of code that are not working
When the li is nested inside of the form element the formatting of the dropdown menu changes. I would like the dropdown to be a form but I want it to look like the default bootstrap dropdown.
Attaching an onclick event handler to the li elements simply is not working. Even after the li is clicked, the PHP does not detect any value for $_POST['topic']
Summary of Goal
I would like the default looking bootstrap drop down menu. I would like the user to be able to click on any item in the drop down menu. After the click the selection should be added to the $_POST array at index "topic"

There is a syntax error in your click() code. You missed 2 semi-colons at the end.
$(function()
{
$('.dropdown-menu li').click(function()
{
$('#my_topic').val($(this).html());
$('#my_form').submit();
}); // <-- need a semi-colon here.
}); // <-- and here.
Other than the above:
Your click handler is clearly not being called.
You're using the jquery API, without first linking to the jquery script file.
Do point 2, then remove the outer $(function()...) and instead, use
$(document).ready(function(){
$('.dropdown-menu li').click(function()
{
$('#my_topic').val($(this).html());
$('#my_form').submit();
});
});
And, Check this demo .

Try this
<form action="index.php" method="POST" id="my_form">
<input type="hidden" name="topic" id="topic">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Topic Select<b class="caret"></b>
<ul class="dropdown-menu">
<li>Introduction</li>
<li>Methods</li>
<li>Loops</li>
<li></li>
</ul>
</a>
</li>
</form>
<script>
$(function()
{
$('.dropdown-menu li').click(function()
{
$('#my_topic').val($(this).html());
$('#my_form').submit();
});
});
</script>
if you want to send all navbar, remove hidden field named topic and use next script
<script>
$(function()
{
$('.dropdown-menu li').click(function()
{
$('.dropdown-menu li').each(function()
{
$('#my_form').append('<input type="hidden" name="topic[]" value="'+$(this).html()+'">');
});
$('#my_form').submit();
});
});
</script>

Related

Selecting First Tab - Laravel

i want to have my first tab selected when page loads or refreshes. In my code, the content of the first tab shows alright but the tab doesn't show it is selected although its content is showing.
Why is this happening please?
<input type="hidden" name="currentTab" value="0"/>
<div class="row">
<div class="col-md-9">
<div class="nav-tabs-custom" id="tabs">
<ul class="nav nav-tabs">
#foreach($students as $student)
<li ><a href="#tab_{{ $student->id }}" data-toggle="tab" >{!!$student->name!!}</a></li>
#endforeach
</ul>
<div class="tab-content">
#foreach($stduents as $key => $student)
<div class="tab-pane" id="tab_{{ $student->id }}">
//content
</div>
#endforeach
</div>
</div>
</div>
</div>
<script>
$( document ).ready(function() {
$("#tabs").tabs({active: document.tabTest.currentTab.value});
$('#tabs a').click(function(e) {
var curTab = $('.ui-tabs-active');
curTabIndex = curTab.index();
document.tabTest.currentTab.value = curTabIndex;
});
})
</script>
Here is a jQuery solution for that:
$(document).ready(function(){
$('.nav-tabs').find('li > a:first').click(); // **
})
** This will activate the first tab when page loads or refreshes.
If you want to do a quick test, just select any other tab than 1st tab, and execute this line in your browser's console $('.nav-tabs').find('li > a:first').click();
I see that an active class is not being set on page load to the first nav
list element.
This should set the active class to first list element.
#foreach($students as $key => $student)
<li {{ $key == 0 ? 'class="active"' : '' }}><a href="#tab_{{ $student->id }}" data-toggle="tab" >
{!!$student->name!!}</a></li>
#endforeach

Bootstrap dropdown POST

I have a bootstrap dropdown which is fill with Jquery function.
But when i submit the form:
<div class="dropdown">
<div class="btn-group">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" >
Choose basket
<span class="caret"></span>
</button>
<ul class="dropdown-menu" name="basket" role="menu" id="groupe" aria-labelledby="dropdownMenu1">
</ul>
</div>
</div>
In my action.php $_POST['basket'] is empty.
This form mustn't be sent with ajax, but just with html "form".
Your sample code shows no <form> nor any form elements. Giving a name attribute to a ul doesn't magically make it work as a form element - you'd have to use JavaScript to either fill and submit an invisible form or send the data via AJAX.

Bootstrap add active class to nav bar when nav is a PHP include

I'm currently building a site with bootstrap and would like to know if it's possible to, and how to add the class="active" attribute to a link on the nav menu. I saw another post similar to this on this very site, however it didn't work on the page using the Bootstrap carousel. I removed all links to jQuery and other javascript on the page but it still wasn't working.
On each page the nav is called with a php include, as is the code for the link state. So picture the markup like so:
<head>
<?php include_once('head.php'); ?>
</head>
<body>
<?php include_once('nav.php'); ?>
</body>
in the head.php is the following code, which I got from another post on this site:
<script>
/*menu handler*/
$(function(){
function stripTrailingSlash(str) {
if(str.substr(-1) == '/') {
return str.substr(0, str.length - 1);
}
return str;
}
var url = window.location.pathname;
var activePage = stripTrailingSlash(url);
$('.nav li a').each(function(){
var currentPage = stripTrailingSlash($(this).attr('href'));
if (activePage == currentPage) {
$(this).parent().addClass('active');
}
});
});
</script>
and in the nav.php I've got:
<nav class="navbar navbar-fixed-top navbar-default" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php"><img src="assets/img/logo.png" /></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li>About</li>
<li>Services</li>
<li>Home </li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
Each page is named as per the href attr.
This works on a duplicate of template.php, however it doesn't work on index.php, which is making me think it's something to do with the carousel...
I've also tried using the data-toggle="pill" as recommended somewhere but that doesn't work.
Any assistance will be appreciated. I'ts on a local vps at the moment, however I can assign it a public IP.
Thanks in advance!
Ash
Because the script is being included in the head.php before the navbar has been loaded, it has nothing to apply the 'active' class to. Perhaps place the JavaScript in a separate include after the navbar.
If that doesn't work, I used this script to apply the active class to a specific menu item, just place it after the navbar include and change the href to the desired 'active' button
<script>
$(document).ready(function(){
$('a[href^="home.html"]').addClass('active');
});
</script>

Error with these bootstrap tabs?

Okay, I am trying to add these tabs from twitter bootstrap, I'm not sure what I am doing wrong, but I can't get these things to work! Some could please help me out?
My tab code..
<ul class="nav nav-tabs" id="tabs" data-tabs="tabs">
<li class="active">Home</li>
<li>Profile</li>
<li>Messages</li>
<li>Settings</li>
</ul>
<div class="tab-content" id="my-tab-content">
<div class="tab-pane active" id="home">asdf</div>
<div class="tab-pane" id="profile">fasd</div>
<div class="tab-pane" id="messages">.xzcv</div>
<div class="tab-pane" id="settings">ert.</div>
</div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('#tabs').tab();
});
</script>
<script type="text/javascript" src="../bootstrap/js/bootstrap.js"></script>
</html>
You can activate individual tabs in several ways.
$('#myTab a[href="#profile"]').tab('show'); // Select tab by name
$('#myTab a:first').tab('show'); // Select first tab
$('#myTab a:last').tab('show'); // Select last tab
$('#myTab li:eq(2) a').tab('show'); // Select third tab (0-indexed)
Try this:
jQuery(document).ready(function ($) {
$('#tabs').tab('show');
});
You may also want to double check that you've got jQuery included in the header...

Why is PJAX stopping Nestable working?

I have 3 columns in my app. Each column has an unordered list. I am using Nestable to drag and drop list items between lists. The mark up is as follows:
<div class="row-fluid span12">
<div class="cf nestable-lists">
<div id="pjax-content">
<div class="span4">
<div class="dd" id="nestable1">
<ul class="dd-list">
<li class="dd-item dd3-item" data-id="123">
<div class="dd-handle dd3-handle">Drag</div>
<div class="dd3-content">
// list content
</div>
</li>
<li class="dd-item dd3-item" data-id="456">
<div class="dd-handle dd3-handle">Drag</div>
<div class="dd3-content">
// list content
</div>
</li>
</ul>
</div>
</div>
<div class="span4">
<div class="dd" id="nestable2">
<ul class="dd-list">
<li class="dd-item dd3-item" data-id="789">
<div class="dd-handle dd3-handle">Drag</div>
<div class="dd3-content">
// list content
</div>
</li>
<li class="dd-item dd3-item" data-id="1011">
<div class="dd-handle dd3-handle">Drag</div>
<div class="dd3-content">
// list content
</div>
</li>
</ul>
</div>
</div>
<div class="span4">
<div class="dd" id="nestable3">
<ul class="dd-list">
<li class="dd-item dd3-item" data-id="1213">
<div class="dd-handle dd3-handle">Drag</div>
<div class="dd3-content">
// list content
</div>
</li>
<li class="dd-item dd3-item" data-id="1415">
<div class="dd-handle dd3-handle">Drag</div>
<div class="dd3-content">
// list content
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
This works and I can drag and drop list items between each list. The problem is when I implement PJAX.
I have a few links that use PJAX to change the url i.e. each link will update the data or order of the data in each list based on the URL. The data updates within id="pjax-content" accordingly, so it works. This is a snippet of the server side code (using CI):
if (isset($_SERVER["HTTP_X_PJAX"])) {
echo $data['contents'];
} else {
// construct views when not PJAX
}
$data['contents'] contains the html as a string.
I have the following JS libraries too (I have tried removing some of these and the problem still exists):
<script type="text/javascript" src="jquery-1.9.0.js"></script>
<script type="text/javascript" src="bootstrap.js"></script>
<script type="text/javascript" src="nestable.js"></script>
<script type="text/javascript" src="nestable-settings.js"></script>
<script type="text/javascript" src="bootstrap-editable.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript" src='jquery.pjax.js'></script>
<script type="text/javascript">
$(document).pjax('[data-pjax] a, a[data-pjax]', '#pjax-content');
$(document).on('pjax:send', function() {
console.log("before send");
});
$(document).on('pjax:complete', function() {
console.log("done!");
});
</script>
The Problem
When I click on one of the links PJAX works, the data is updated, the page doesn't reload and the console shows before send and done - so all is well. However, the nestable items are no longer selectable so a I can't drag and drop them. When I do a full page refresh it works and I can drag and drop.
I have compared the markup before and after (as that was a previous issue) and everything is the same.
Any suggestions on where I am going wrong? Or how to best debug this?
call nestable after your ajax complete function
$(document).on('pjax:complete', function() {
$('#nestable1,#nestable2,#nestable3').nestable();
});

Categories