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);
Related
I want to take value from select in html and push it into action url.
So when users select something value of action buyoption will be the value of the selection
I would prefer to do it with php if there is no option to do it with html.
This is my test buy it doesn't change the buyoption to selection variable..
please help
<form action="<?php echo URLROOT; ?>/trades/trade/buyoption/selloption" method="POST">
<div class="row">
<div class="col-md-9 register-right" >
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">TRADE</h3>
<div class="row register-form">
<div class="col-md-12">
<label for="buy"><b>YOU SEND:</b></label>
<div class="form-group">
<select class="form-control" name="buyoption" id="buyoption">
<option class="hidden" selected disabled>Please select what you want to sell</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">LITECOIN (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div>
<label for="buy"><b>YOU RECEIVE:</b></label>
<div class="form-group">
<select class="form-control" name="selloption" id="buyoption">
<option class="hidden" selected disabled>Please select what you want receive</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">Litecoin (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div>
<input type="submit" class="btnRegister" value="Proceed"/>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
There must not be multiple elements in a document that have the same id value.
You can watch for change event of select element and on Change, manipulate the Action URL.
let buyoption = document.getElementById('buyoption');
let selloption = document.getElementById('selloption');
let postForm = document.getElementById('postForm');
let action = postForm.getAttribute('action');
let replacerFn = function() {
let temp = action;
if (buyoption.value) {
temp = temp.replace('buyoption', buyoption.value);
}
if (selloption.value) {
temp = temp.replace('selloption', selloption.value);
}
postForm.setAttribute('action', temp);
console.log('Action Attribute=> ', postForm.getAttribute('action'));
};
buyoption.addEventListener('change', function() {
replacerFn();
});
selloption.addEventListener('change', function() {
replacerFn();
});
<form action="/trades/trade/buyoption/selloption" method="POST" id="postForm">
<div class="row">
<div class="col-md-9 register-right">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">TRADE</h3>
<div class="row register-form">
<div class="col-md-12"> <label for="buy"><b>YOU SEND:</b></label>
<div class="form-group">
<select class="form-control" name="buyoption" id="buyoption">
<option class="hidden" selected disabled value="">Please select what you want to sell</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">LITECOIN (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div><label for="buy"><b>YOU RECEIVE:</b></label>
<div class="form-group">
<select class="form-control" name="selloption" id="selloption">
<option class="hidden" selected disabled value="">Please select what you want receive</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">Litecoin (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div><input type="submit" class="btnRegister" value="Proceed" /> </div>
</div>
</div>
</div>
</div>
</div>
</form>
I am trying to fetch Data when we select value.
Here Is Jquery and html code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#colorselector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
</script>
Now Here is Html.And This Code is working .
<select id="colorselector">
<option value="red">Red</option>
<option value="yellow">Yellow</option>
<option value="blue">Blue</option>
</Select>
<div id="red" class="colors" style="display:none"> red... </div>
<div id="yellow" class="colors" style="display:none"> yellow.. </div>
<div id="blue" class="colors" style="display:none"> blue.. </div>
But when I am trying to use this with dynamic PHP code this is not Working.
<div class="form-group">
<label for="address"> Select Product Kit</label>
<select class="form-group" name="product kit" id="colorselector">
#foreach($product as $productDeatails)
<option value='{{$productDeatails->id}}'>{{$productDeatails->title}}
</option>
#endforeach
</select>
<div id="{{$productDeatails->id}}" class="colors" style="display:none">{{$productDeatails->price}}
</div>
#endforeach
</select>
<div id="{{$productDeatails->id}}" class="colors" style="display:none">{{$productDeatails->price}}
As you can see, the foreach has ended before you could print the hidden div HTML. Due to this, $productDeatails is out of scope now and is no longer available.
A solution is to run the foreach again to print the hidden divs in HTML like below:
<div class="form-group">
<label for="address"> Select Product Kit</label>
<select class="form-group" name="product kit" id="colorselector">
#foreach($product as $productDetails)
<option value='{{ $productDetails->id }}'>{{ $productDetails->title}}</option>
#endforeach
</select>
#foreach($product as $productDetails)
<div id="{{ $productDetails->id}}" class="colors" style="display:none">{{ $productDetails->price}}
</div>
#endforeach
</div>
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>
I've been searching around the last few days looking for an answer but haven't found one. At this point I believe the problem to either be a result of the AJAX, or my actual form. Either way, any help would be appreciated.
So the Ajax I'm using has worked before, when using it on a much simpler form. For that reason, I simply brought it over and adapted it. The only things that I changed are the ID names and file it will load. With that said, it does work - to an extent. If the file being loaded has text, or a basic echo statement, then it will run fine and print as expected on the page.
When I start trying to have it set variables and what not, then it starts having problems. It simple spits out "Ajax failed to run". Ive tried various ways to pull an error message as well but those didn't seem to work either. There may be some small errors here and there because I've been greatly fiddling with the code for a day or two now.
HTML:
<!DOCTYPE html>
<?php session_start(); ?>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Standard Meta -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<!-- Site Properties -->
<title>Apollo Project | Conjugate</title>
<link rel="stylesheet" type="text/css" href="/css/semantic.css">
<link rel="stylesheet" type="text/css" href="components/dropdown.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="/components/dropdown.js"></script>
<script src="/scripts/semantic.js"></script>
<style type="text/css">
body {
background-color: #FFFFFF;
}
.ui.menu .item img.logo {
margin-right: 1.5em;
}
.main.container {
margin-top: 7em;
}
</style>
</head>
<body>
<div class="ui fixed inverted menu">
<div class="ui container">
<a href="http://semantic-ui.com/examples/fixed.html#" class="header item">
Apollo Project
</a>
Home
<div class="ui simple dropdown item">
Dropdown <i class="dropdown icon"></i>
<div class="menu">
<a class="item" href="http://semantic-ui.com/examples/fixed.html#">Link Item</a>
<a class="item" href="http://semantic-ui.com/examples/fixed.html#">Link Item</a>
<div class="divider"></div>
<div class="header">Header Item</div>
<a class="item" href="http://semantic-ui.com/examples/fixed.html#">Link Item</a>
</div>
</div>
</div>
</div>
<div class="ui main text container">
<h3 class="ui center aligned header">Verb Conjugating</h3>
<div id="form-content">
<form method="post" action="" id="mainForm" >
<!-- Selector for Chapter -->
Chapter:
<select name="chapter" class="ui fluid search dropdown" id="chapter">
<option value="chAll">Any Chapter</option>
<option disabled role="separator">Select a chapter:</option>
<option value="ch1">Chapter 1</option>
<option value="ch2">Chapter 2</option>
<option value="ch3">Chapter 3</option>
<option value="ch4">Chapter 4</option>
<option value="ch5">Chapter 5</option>
<option value="ch6">Chapter 6</option>
<option value="ch7">Chapter 7</option>
<option value="ch8">Chapter 8</option>
<option value="ch9">Chapter 9</option>
<option value="ch10">Chapter 10</option>
<option value="ch11">Chapter 11</option>
<option value="ch12">Chapter 12</option>
<option value="ch13">Chapter 13</option>
<option value="ch14">Chapter 14</option>
<option value="ch15">Chapter 15</option>
<option value="ch16">Chapter 16</option>
<option value="ch17">Chapter 17</option>
<option value="ch18">Chapter 18</option>
<option value="ch19">Chapter 19</option>
<option value="ch20">Chapter 20</option>
<option value="ch21">Chapter 21</option>
<option value="ch22">Chapter 22</option>
<option value="ch23">Chapter 23</option>
<option value="ch24">Chapter 24</option>
<option value="ch25">Chapter 25</option>
<option value="ch26">Chapter 26</option>
<option value="ch27">Chapter 27</option>
<option value="ch28">Chapter 28</option>
<option value="ch29">Chapter 29</option>
<option value="ch30">Chapter 30</option>
<option value="ch31">Chapter 31</option>
<option value="ch32">Chapter 32</option>
<option value="ch33">Chapter 33</option>
<option value="ch34">Chapter 34</option>
<option value="ch35">Chapter 35</option>
</select>
<br>
<div class="ui two column doubling stackable grid">
<!-- Selectors for Voice, Tense, Conjugation, and Mood -->
<div class="column"> Voice:
<select name="voice" class="ui fluid search dropdown" id="voice">
<option value="vocAll">Any Voice</option>
<option disabled role="separator">Select a voice:</option>
<option value="usrAct">Active</option>
<option value="usrPas">Passive</option>
</select>
</div>
<div class="column"> Tense:
<select name="tense" class="ui fluid search dropdown" id="tense">
<option value="tenAll">Any Tense</option>
<option disabled role="separator">Select a tense:</option>
<option value="usrPres">Present</option>
<option value="usrImp">Imperfect</option>
<option value="usrPerf">Perfect</option>
<option value="usrPluPerf">PluPerfect</option>
<option value="usrFut">Future</option>
<option value="usrFutPerf">Future Perfect</option>
</select>
</div>
<div class="column"> Conjugation:
<select name="conj" class="ui fluid search dropdown" id="conj">
<option value="conjAll">Any Conjugation</option>
<option disabled role="separator">Select a conjugation:</option>
<option value="usr1st">1st</option>
<option value="usr2nd">2nd</option>
<option value="usr3rd">3rd</option>
<option value="usr4th">4th</option>
</select>
</div>
<div class="column"> Mood:
<select name="mood" class="ui fluid search dropdown" id="mood">
<option value="moodAll">Any Mood</option>
<option disabled role="separator">Select a mood:</option>
<option value="usrInd">Indicative</option>
<option value="usrInf">Infinitive</option>
<option value="usrImp">Imperative</option>
</select>
</div>
</div>
<br>
<input type="submit" class="ui fluid button" name="submitBtn">
</form>
</div>
<!-- HTML to submit Ajax to another page -->
<script type="text/javascript">
$(document).ready(function() {
// submit form using $.ajax() method
$('#mainForm').submit(function(e){
e.preventDefault(); // Prevent Default Submission
$.ajax({
url: 'submitDecline.php',
type: 'POST',
data: $(this).serialize() // it will serialize the form data
})
.done(function(data){
$('#form-content').fadeOut('slow', function(){
$('#form-content').fadeIn('slow').html(data);
});
})
.fail(function(){
alert('Ajax Submit Failed ...');
});
});
});
</script>
<!--
<h3 class="ui center aligned header">Doubling Stackable Grid</h3>
<div class="ui stackable grid">
</div>
<div class="ui two column doubling stackable grid">
<div class="column">
<div class="ui segment">Content</div>
</div>
<div class="column">
<div class="ui segment">Content</div>
</div>
<div class="column">
<div class="ui segment">Content</div>
</div>
<div class="column">
<div class="ui segment">Content</div>
</div>
<div class="column">
<div class="ui segment">Content</div>
</div>
<div class="column">
<div class="ui segment">Content</div>
</div>
</div>
-->
</div>
<?php $conn->close();
?>
<script src="scripts/script.js"></script>
</body>
</html>
PHP file being loaded:
<p> This is a test </p>
<?php
session_start();
if( $_POST ){
$chaptSelect = $_POST['chapter'];
$tenseSelect = $_POST['tense'];
$voiseSelect = $_POST['voice'];
$moodSelect = $_POST['mood'];
$conjSelect = $_POST['conj'];
if( $chaptSelect == "Any Chapter" ) {
echo("You selected any chapter")
}
?>
<p> This is a test </p>
I have a problem with a form that I made, it is working in Firefox but in IE and Chrome it doesnt! when I press the Submit button in IE and Chrome nothing happens! I didnt check this before because I didnt thought that I will have problems like this! I dont know what I am missing here! is there any known problem (bug)
the form:
<div id="Formulari">
<div class="WraperForForm">
<form action="index.php?menu=rezervimet&submenu=rezervo" method="post">
<div class="elementsLabelBox">
Emri:
</div>
<div class="elementsBox">
<input type="text" id="emri" name="emri">
</div>
<div class="elementsLabelBox">
Mbiemri:
</div>
<div class="elementsBox">
<input type="text" id="mbiemri" name="mbiemri">
</div>
<div class="elementsLabelBox">
Prej:
</div>
<div class="elementsBox">
<select class="selectDest" name="Prej" onChange="getState(this.value)">
<option></option>
'.funksionet::all_directions().'
</select>
</div>
<div class="elementsLabelBox">
Deri:
</div>
<div class="elementsBox">
<div id="statediv"><select class="selectDest" name="deri">
<option></option>
</select></div>
</div>
<div class="elementsLabelBox">
<form name="Data1Drejtim">
<label for="data1drejtim">Data e nisjes:</label>
</div>
<div class="elementsBox">
<input type="text" id="data1drejtim" name="data1drejtim">
<script language="JavaScript">
// whole calendar template can be redefined per individual calendar
var A_CALTPL = {
\'months\' : [\'Janar\', \'Shkurt\', \'Mars\', \'Prill\', \'Maj\', \'Qershor\', \'Korrik\', \'Gusht\', \'Shtator\', \'Tetor\', \'Nentor\', \'Dhjetor\'],
\'weekdays\' : [\'Di\', \'He\', \'Ma\', \'Me\', \'Ej\', \'Pr\', \'Sh\'],
\'yearscroll\': true,
\'weekstart\': 0,
\'centyear\' : 70,
\'imgpath\' : \'images/\'
}
new tcal ({
// if referenced by ID then form name is not required
\'controlname\': \'data1drejtim\'
}, A_CALTPL);
</script>
</div>
<!-- ___________________ RETURN DATE _____________________________________ -->
<div id="hideThis">
<div class="elementsLabelBox">
<label for="dataKthyese">Data kthyese:</label>
</div>
<div class="elementsBox">
<input type="text" id="dataKthyese" name="dataKthyese">
<script language="JavaScript">
// whole calendar template can be redefined per individual calendar
var A_CALTPL = {
\'months\' : [\'Janar\', \'Shkurt\', \'Mars\', \'Prill\', \'Maj\', \'Qershor\', \'Korrik\', \'Gusht\', \'Shtator\', \'Tetor\', \'Nentor\', \'Dhjetor\'],
\'weekdays\' : [\'Di\', \'He\', \'Ma\', \'Me\', \'Ej\', \'Pr\', \'Sh\'],
\'yearscroll\': true,
\'weekstart\': 0,
\'centyear\' : 70,
\'imgpath\' : \'images/\'
}
new tcal ({
// if referenced by ID then form name is not required
\'controlname\': \'dataKthyese\'
}, A_CALTPL);
</script>
</form>
</div>
</div>
<div class="elementsLabelBox">
Persona:
</div>
<div class="elementsBox">
<select name="persona">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</div>
<!-- <tr>
<td width="30" >Fëmij:</td>
<td><input type="text" size="3" name="femij"></td>
</tr> -->
<div class="elementsBox">
</div>
<div class="elementsLabelBox">
</div>
<div class="elementsLabelBox">
<label for="1drejtim">Një drejtim</label>
<input type="radio" id="1drejtim" name="drejtimi" value="një drejtim" onclick="toggleVisibility(\'hideThis\',0)">
<br/>
<label for="1drejtim">Kthyese</label>
<input type="radio" id="kthyese" name="drejtimi" checked="checked" value="kthyese" onclick="toggleVisibility(\'hideThis\',1)">
</div>
<input style="float:right;margin:15px 49px 0 0;" type="submit" value="Rezervo" name="rezervo" />
</form><!-- end of the reservation form-->
</div>
</div><!-- end of Formulari-->
Thank you for your time.
You have two <form> tags in this document, and the one nested closest to the submit button has no action or method attributes. IE and Chrome are correct to do nothing since this form has no action to associate with submission.