I have created a drop down menu in bootstrap. I have set the links using php code to site my path. (I tried it without php and I still get the same result)
When I select the item and click the submit button to pull the categories nothing happens.
I created a link menu using the same paths and it works, as soon as I put it in a drop down menu with a button it does not work.
<div class="container-fluid">
<div class="row justify-content-center text-center">
<div class="col-md-5">
<select class="form-control selecter">
<option selected="selected" value="">All Categories</option>
<option value="<?php echo base_url();?>categories/posts/154">Auto</option>
<option value="<?php echo base_url();?>categories/posts/154">House</option>
<option value="<?php echo base_url();?>categories/posts/154">Lawn</option>
</select>
<br>
<button class="btn btn-block btn-primary btn-gradient">Search</button>
</div>
</div>
</div>
You can not navigate using option, If you want to navigate on the option change or the button click, you should use JavaScript addEventListener method to listen for the events. Ex: click in button or change in select drop-down. By using window.location property you can navigate to page
<div class="container-fluid">
<div class="row justify-content-center text-center">
<div class="col-md-5">
<select id="dropdown" class="form-control selecter">
<option selected="selected" value="">All Categories</option>
<option value="<?php echo base_url();?>categories/posts/154">Auto</option>
<option value="<?php echo base_url();?>categories/posts/154">House</option>
<option value="<?php echo base_url();?>categories/posts/154">Lawn</option>
</select>
<br>
<button id="search" class="btn btn-block btn-primary btn-gradient">Search</button>
</div>
</div>
</div>
VANILLA JAVASCRIPT
<script>
let dropdown = document.getElementById('dropdown');
let searchBtn = document.getElementById('search');
searchBtn.addEventListener('click', function(e) {
window.location = dropdown.value;
}
// If you want to navigate when the selection option gets changed
dropdown.addEventListener('change', function(e) {
window.location = dropdown.value;
} </script>
JQUERY
<script>
$(function(){
// On option select navigation
$('#dropdown').on('change', function () {
var url = $(this).val();
if (url) {
window.location = url;
}
});
// On Search button click - navigation
$('#search').on('click', function () {
var url = $('#dropdown').val();
if (url) {
window.location = url;
}
});
});
</script>
This is a select element no a link or a form action. The default behaviour of the select will be to select the option and not link to the option value.
You will you need to fire a JavaScript when search button is clicked and then redirect the user to the selected value.
function search()
{
var id = document.getElementById("selecter");
window.location = id.options[id.selectedIndex].value;
}
<div class="container-fluid">
<div class="row justify-content-center text-center">
<div class="col-md-5">
<select class="form-control selecter" id="selecter">
<option selected="selected" value="">All Categories</option>
<option value="<?php echo base_url();?>categories/posts/154">Auto</option>
<option value="<?php echo base_url();?>categories/posts/154">House</option>
<option value="<?php echo base_url();?>categories/posts/154">Lawn</option>
</select>
<br>
<button class="btn btn-block btn-primary btn-gradient" onclick="javascript:search()">Search</button>
</div>
</div>
</div>
Related
I have created the form below to ask what the visitor wants so I can indicate in a modal window how to get there. So I need to get the visitor's choices in the modal window without refreshing the page.
Here are my codes below.
<form id="wantedForm" method="POST">
<div class="wanted-opt">
<div class="wantoptiontitle">Je veux</div>
<div class="wanted-options input-field">
<select id="wanted-1" name="wanted-1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</div>
</div>
<div class="wanted-opt">
<div class="wantoptiontitle">comme ça je peux</div>
<div class="wanted-options input-field">
<select id="wanted-2" name="wanted-2">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</div>
</div>
<button class="btn-default btn modal-trigger" type="submit">Comment faire ?
</button>
</form>
<div id="wantedModal" class="modal">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
<p><?php var_dump($_POST) ?></p>
</div>
</div>
Le script
$('#wantedForm').on('submit', function(e){
e.preventDefault();
$('#wantedModal').modal('open');
});
When I do a var_dump($_POST) in the modal, it returns a null result. Can someone please tell me what I need to correct? Thank you for your help.
Please tell me what I need to change. Thank you.
EDIT
I had tried to do with ajax also with the code below. It hadn't worked either.
$('#wantedSubmit').click(function(e){
e.preventDefault();
var wanted_1 = $('#wanted-1').val();
var wanted_2 = $('#wanted-2').val();
$.post(location.href, {wanted_1: wanted_1});
$.post(location.href, {wanted_2: wanted_2});
$('#wantedModal').modal('open');
});
Here is a solution which gets your form data directly in your modal on submit:
$('#wantedForm').on('submit', function(e){
e.preventDefault();
var wanted_1 = $('#wanted-1 option:selected').text();
var wanted_2 = $('#wanted-2 option:selected').text();
$('#choices')
.empty()
.append('<p>wanted 1: '+wanted_1+'</p>')
.append('<p>wanted 2: '+wanted_2+'</p>');
$('#wantedModal').modal('show');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<form id="wantedForm" method="POST">
<div class="wanted-opt">
<div class="wantoptiontitle">Je veux</div>
<div class="wanted-options input-field">
<select id="wanted-1" name="wanted-1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</div>
</div>
<div class="wanted-opt">
<div class="wantoptiontitle">comme ça je peux</div>
<div class="wanted-options input-field">
<select id="wanted-2" name="wanted-2">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</div>
</div>
<button class="btn-default btn modal-trigger" type="submit">Comment faire ?
</button>
</form>
<div id="wantedModal" class="modal">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
<div id="choices"></div>
</div>
</div>
Your are not posting the request by using e.preventDefault(); that`s why $_POST is empty. You should collect submited values in JS like this.
var x = $('#wanted-1').val();
var y = $('#wanted-2').val();
and then you can open insert this values in modal elements like
$("#modal-element1").html(x);
<form class="form-inline" action="./test.php?id=1">
<li class="nav-item">
<div class="form-group">
<select class="custom-select custom-select-lg mb-3" id="objectType">
<option selected>Object Type</option>
<option value="Toy">Toy</option>
<option value="Furniture">Furniture</option>
<option value="Gift">Gift</option>
<option value="Household">Household</option>
<option value="Instrument">Instrument</option>
</select>
</div>
</li>
<li class="nav-item">
<div class="form-group">
<select class="custom-select custom-select-lg mb-3" id="materialType">
<option selected>Material Type</option>
<option value="Mud">Mud</option>
<option value="Cloth">Cloth</option>
<option value="Thread">Thread</option>
<option value="Jute">Jute</option>
<option value="Cotton">Cotton</option>
<option value="Can">Can</option>
<option value="Bamboo">Bamboo</option>
</select>
</div>
</li>
<li class="nav-item">
<div class="form-group">
<div class="input-group">
<input class="form-control" type="search" placeholder="Search">
<span class="input-group-btn">
<button class="btn btn-outline-secondary" type="submit" id="searchSubmit"><i class="fa fa-search"></i>
</button>
</span>
</div>
</div>
</li>
</form>
I have this form. When the submit button is called test.php page is called with request id value 1. In test.php form field's value will be used for making query.
test.php code snippet of a portion
$id=$_REQUEST['id'];
if ($id==1)
{
$result = search($_POST['imageName'],$_POST['objectType'],$_POST['materialType']);
echo $_POST['imageName'];
}
else
{
$result = home_page_image();
}
When I click the submit button after fill the field the test.php?id=1 is not appear in the browser address bar that confirms me that the page is not called.
Edit 1
I have added method(POST) as #PL200 mentioned in his answer but nothing is echo after search() function in test.php. I also put a alert but not shown. alert is work before search function and post value is neither echo before nor after of search function. That indicates the form values are not posted in test.php.
Edit 2
Actually I call the same page when click the submit button is clicked.
You've included no method for your form. Change the first line of your html to:
<form class="form-inline" method="POST" action="./test.php?id=1">
That should fix the problem that you're having.
I understand what you were trying to do, but note that some browsers will ignore the query arguments passed with the form action attribute. So whenever the form has been submitted, on some browsers the $_GET['id'] might not be defined, just inconsistent behavior.
Also,you didn't specify any method attribute for your form.
What you should have done instead
Use a hidden input form element to pass your value, this is the recommended way to do it.
<form class="form-inline" action="./test.php" method="post">
<!-- put values to pass here -->
<input type="hidden" value="1" name="id" />
...
</form>
Furthermore, you could also place the hidden input field outside the form element and use the form id to reference it with the input field form attribute. See example below:
<form id="form-id" class="form-inline" action="./test.php" method="post">
<!-- put values to pass here -->
...
</form>
<!-- Bind this hidden input field to your form -->
<input type="hidden" value="1" name="id" form="form-id" />
Note that there's no limitation to the numbers of hidden input field you can use in a single form, howbeit, make it simple.
There is no name of form input.
<form class="form-inline" action="./test.php?id=1" method="POST">
<li class="nav-item">
<div class="form-group">
<select class="custom-select custom-select-lg mb-3" name="objectType">
<option selected>Object Type</option>
<option value="Toy">Toy</option>
<option value="Furniture">Furniture</option>
<option value="Gift">Gift</option>
<option value="Household">Household</option>
<option value="Instrument">Instrument</option>
</select>
</div>
</li>
<li class="nav-item">
<div class="form-group">
<select class="custom-select custom-select-lg mb-3" name="materialType">
<option selected>Material Type</option>
<option value="Mud">Mud</option>
<option value="Cloth">Cloth</option>
<option value="Thread">Thread</option>
<option value="Jute">Jute</option>
<option value="Cotton">Cotton</option>
<option value="Can">Can</option>
<option value="Bamboo">Bamboo</option>
</select>
</div>
</li>
<li class="nav-item">
<div class="form-group">
<div class="input-group">
<input class="form-control" type="search" placeholder="Search" name="imageName">
<span class="input-group-btn">
<button class="btn btn-outline-secondary" type="submit" id="searchSubmit"><i class="fa fa-search"></i>
</button>
</span>
</div>
</div>
</li>
</form>
And alert message is not shown after search() method because in search() method something is wrong. I debug and correct the error in search() method. Now all works.
How can I validate cloned drop downs wherein a drop down will be selected, the option will be disabled to other drop down lists?
Html Code
<form id="surveyForm" method="post" class="form-horizontal">
<div class="form-group" id="surveyTemplate">
<label class="col-xs-3 control-label">Options</label>
<div class="col-xs-5">
<select class="form-control user" name="employee[]">
<option></option>
<option value="1">Martin Luther</option>
<option value="2">Mark Glovo</option>
<option value="3">Michael Sei</option>
</select>
</div>
<div class="col-xs-4">
<button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i>
</button>
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-xs-offset-3">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
Jquery Code
$('#surveyForm').on('click', '.addButton', function() {
var template = $('#surveyTemplate'),
clone = template
.clone()
.removeAttr('id')
.insertBefore($template),
$employee = clone.find('[name="employee[]"]');
$('#surveyForm').formValidation('addField', employee);
});
$("#surveyForm").on('change', '.user', function() {
var value = $(this).val();
$(this).find("option[value =" + value + "]").attr('disabled', false);
$("select").not(this).find("option[value =" + value + "]").attr('disabled', true);
});
Note that when I click the add button, it adds a clone of this form.
I would like to disabled a button that will submit my form if the fields aren't changed.
So if a change a field, I would like to be able to submit the form by enabling the button, but only if a change a field (minimum).
<?php $listeToitures = $db->query("SELECT * FROM i10_toit where i10_toit.N_NUME_IMME = $choixImmeuble");
$toiture = $listeToitures->fetch() ?>
<br><br><button class="btn btn-primary" type="button" data-target="#toitures" data-toggle="collapse" aria-expanded="false" aria-controls="MonCollapse"><i class="glyphicon glyphicon-chevron-right"></i> Toitures</button>
<div id="toitures" class="collapse">
<h3 class="page-header">Toitures <button type="submit" name="enregistrerParkingAmenagement" class="btn btn-default">Enregistrer</button></h3>
<button class="btn btn-primary" type="button" data-target="#parking" data-toggle="collapse" aria-expanded="false" aria-controls="MonCollapse"><i class="glyphicon glyphicon-chevron-right"></i> Parking</button>
<div id="parking" class="collapse">
<div class="col-xl-6 col-lg-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="toiture_c_etat_ferb">Etat ferblanterie :</label><br>
<select name="toiture_c_etat_ferb" id="toiture_c_etat_ferb" class="form-control">
<?php
$cherche = chercheViews('VZ1085');
foreach ($cherche as $resultat){
$code = $resultat['C_CODE_SEQU'];?>
<option value="<?php echo $code ?>"<?php if($toiture['C_ETAT_FERB']==$code) echo 'selected="selected"'; ?>><?php echo $resultat['L_DESC_CODE']; ?></option>
<?php }?>
</select>
</div>
<div class="form-group">
<label for="toiture_c_type_ferb">Type de ferblanterie :</label><br>
<select name="toiture_c_type_ferb" id="toiture_c_type_ferb" class="form-control">
<?php
$cherche = chercheViews('VZ1143');
foreach ($cherche as $resultat){
$code = $resultat['C_CODE_SEQU'];?>
<option value="<?php echo $code ?>"<?php if($toiture['C_TYPE_FERB']==$code) echo 'selected="selected"'; ?>><?php echo $resultat['L_DESC_CODE']; ?></option>
<?php }?>
</select>
<br><br><br><br><br><br><br><br>
</div>
</div>
<label for="chaufferie_l_toit_comm">Commentaire :</label><br>
<textarea name="chaufferie_l_toit_comm" class="form-control" id="chaufferie_l_faca_comm"><?php echo $toiture['L_TOIT_COMM'];?></textarea>
</div>
</div>
</div>
</form>
<script>
$(function() {
var form_original_data = $("#myform").serialize();
$("#enregistrerFacadesToitures").click(function() {
if ($("#myform").serialize() != form_original_data) {
<button type="submit" name="enregistrerFacadesToitures" class="btn btn-default">Enregistrer</button>
}
});
});
</script>
</div>
</section>
Sorry for my english.
you can use prop('disabled' , true) and prop('disabled' , false) for button .. like so
$("#enregistrerFacadesToitures").click(function() {
if ($("#myform").serialize() !== form_original_data) {
$('button[type="submit"][name="enregistrerFacadesToitures"]').prop('disabled' , true);
}else{
$('button[type="submit"][name="enregistrerFacadesToitures"]').prop('disabled' , false);
}
});
give a default disabled property to the submit button DOM, then use jQuery to check if any changes happened in the form , if yes, then delete the disabled property of this submit button DOM, if not, then just keep it as disabled.
I have a PHP-page which displays a number of records.
On the page I have two submit buttons: one for the next page and one (within a bootstrap modal) to export the data to another database.
The HTML of the next page is:
<form method="post" action="mypage.php" />
Jump to Page:
<select name="page"">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="Go" />
</form>
This is a part of the code from within the (bootstrap) modal:
<div class="modal-body">
<form data-async data-target="#modal" action="import.php" method="POST">
<div id="modal" class="alert alert-success" style="font-size:13px;">Please enter the requested data!</div>
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active">Project</li>
<li>Summary</li>
<li>Personal</li>
<li>Details</li>
<li>Notification</li>
</ul>
<div class="tab-content">
<div id="pane1" class="tab-pane active">
<input type="hidden" name="ticketId" id="ticketId" value=""/>
<p>Please select the project you want to file the report under.</p>
<select style="width:350px;" class="chzn-select" name="project">
<option value=""></option>
<option>Project 1</option>
<option selected>Project 2</option>
<option>Project 3</option>
<option>Project 4</option>
</select>
<p class="note">Note: You can change this later.</p>
[... content skipped ...]
</div><!-- /.tab-content -->
</div><!-- /.tabbable -->
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Create Report" />
Close
</div>
</div>
</form>
And in the header of the modal window page I have this javascript:
<script>
jQuery(function($) {
$('body').on('submit','form[data-async]', function(event) {
alert('submit Event');
var $form = $(this);
var $target = $($form.attr('data-target'));
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
success: function(data, status) {
$target.html(data);
}
});
event.preventDefault();
$('#addBookDialog').delay(2000).fadeOut('slow');
location.reload(true);
});
});
</script>
BTW the next page is called with simple PHP that queries the database for new records.
The problem is this: when I click on the submit button within the modal the data gets exported to the other database using import.php. Great. But when I use the first button (next page, or another one) the request is also sent to import.php, while it just should display another page.
So my question is: how can I use the Jquery function for exporting the info from the modal window and how can the first (page) submit button just call the next or other page.
If you have any question, please let me know.
Cheers!
Why you use submit for going to next page if you don't want to submit your form? To call next page use simple link.