I have a simple html with jQuery form , and i want to:
1) get the value into a js var .
2)send it by JSON to a server .
here is the form :
<div data-role="main" class="ui-content">
<data=role "form" name="seekerForm" id="jobSeekerForm" >
<label for="basic">First name :</label>
<input type="text" name="seekerName" id="WfirstName" data-mini="true" />
<label for="basic">Last name :</label>
<input type="text" name="seekerLast" id="WlastName" data-mini="true" />
<label for="basic" id="WEmail">E-mail:</label>
<input type="text" name="seekerEmail" id="WEmail" data-mini="true" />
<div data-role="fieldcontain" >
<label for="select-choice-1" class="select" >Choose a field:</label>
<select name="select-choice-1" id="selectField" >
<option value="HI-TEC">Software Programming</option>
<option value="webPro">Web Programming</option>
<option value="QA">QA</option>
<option value="systemInfo">System Information</option>
<option value="DB">DBA</option>
<option value="java">JAVA</option>
<option value="c">c++</option>
<option value="pyt">pyton</option>
</select>
</div>
<div data-role="fieldcontain" >
<label for="select-choice-1" class="select" >Choose Experience level:</label>
<select name="select-choice-1" id="selectPro" >
<option value="junior">junior(0-2)</option>
<option value="senior">senior(2-5)</option>
<option value="exp">Expert(5-10)</option>
</select>
</div>
</form>
3) same thing to the option value .
Here is the way im sending :
// the object i want to send :
var jobsData = {
name: $name.val(),
lname: $WlastName.val(),
mail: $WEmail.val(),
userlocation: location,
field : $selectField.val(),
Pro: $selectPro.val(),
range: $sliderRange.val()
};
// ajax - data: how i want to send that
$.ajax({
type: 'GET',
dataType: 'JSON',
url:'http://localhost:8888',
data: jobsData,
success: function(jobs){
$.each(jobs,function(i,job){
$('.jobRes').html(jobs);
});
You can use .serialize() to get all the form values and send them to the server via ajax. Something like this would do:
$(function() {
$('form').on('submit', function( e ) {
e.preventDefault();
$.ajax( url:'http://localhost:8888/', data: $(this).serialize(), ..... );
});
});
Your page must be running on the same localhost same port 8888 for this to work.
You might want to checkout the jQuery .serialize() method for getting the form values.
http://api.jquery.com/serialize/
Related
I have modal popup which opens on button click. I have a select dropdown which has user type, according to that user type , it fetches respective data from mysql table and populates in another select drop down. I am using .on(change) function , here is my modal popup's select dropdown:
<label for="inputEmail3" class="col-sm-4 col-form-label">User Type</label>
<select name="user_type" id="user_type" class="form-control form-control-sm" style="width: 100%;" required="">
<option selected="selected" value="">Select user type </option>
<option value="admin">Admin </option>
<option value="manager">Manager </option>
<option value="executive">Executive </option>
</select>
<label for="inputEmail3" class="col-sm-4 col-form-label">Employee/User</label>
<select name="user" id="user" class="form-control form-control-sm" style="width: 100%;" required="" disabled>
<option selected="selected" value="">Select Employee </option>
</select>
My Javascipt originally I had:
<script type="text/javascript">
$(document).ready(function(){
$('#user_type').on(function() {
var usertype = $(this).val();
$('#user').prop( "disabled", false );
$.ajax({
type:'POST',
url:'T_userData.php',
data:'usertype='+usertype,
success:function(html){
//alert(html);
$('#sub_prod').html(html);
//$('#city').html('<option value="">Select state first</option>');
}
});
});
});
</script>
then I changed it to
<script type="text/javascript">
$(document).ready(function(){
$('#trgtmodal').on("change","#user_type",function() {
var usertype = $(this).val();
$('#user').prop( "disabled", false );
$.ajax({
type:'POST',
url:'T_userData.php',
data:'usertype='+usertype,
success:function(html){
//alert(html);
$('#sub_prod').html(html);
//$('#city').html('<option value="">Select state first</option>');
}
});
});
});
</script>
still not working. If needed I can post the whole Modal Popup HTML markup. Please advise
I have two selects, the second one is hidden and it should be shown if the first select has been changed, I checked it through on('change', function(){ ... and it is working so far. I've checked and also showed a console.log when the first select box is changed.
The main problem here is when I picked a option from the first select box, it should send some request using AJAX. I've sent some request to fetch_sections.php and it should append or add data in my second select box.
This is my updated script:
<script>
$(document).ready(function() {
$('select').material_select();
$('#school-list').on('change', function() {
$.ajax({
url: 'fetch_sections.php',
type: 'post',
dataType: 'html',
data: {parent: $(this).val() },
success: function(data)
{
$('#section-list').empty();
$('#section-list').html(data);
console.log(data);
}
});
$('#hideThis').show("slow");
});
});
</script>
This is my fetch_sections.php (working, checked with PostMan):
<?php
require '../assets/php/init.php';
if ($_POST)
{
$schoolID = Essentials::sanitize($_POST['parent']);
$fetchSQL = "SELECT `sectionid`, `name` FROM `sections` WHERE schoolid = ?";
$fetch = $conn->prepare($fetchSQL);
$fetch->execute([$schoolID]);
$fetchRes = $fetch->fetchAll(PDO::FETCH_ASSOC);
if(!$fetchRes) {
echo '
<option value="" disabled selected>Unfortunately, there is no section here.</option>
';
} else {
foreach ($fetchRes as $section)
{
echo '
<option value="'.$section['sectionid'].'"">'.$section['name'].'</option>
';
}
}
}
This is my select HTML also updated:
<div class="row margin">
<div class="input-field col s12">
<select id="school-list" name="school-list">
<option value="" disabled selected>Choose your school</option>
...
</select>
<label>School</label>
</div>
</div>
<div class="row margin" id="hideThis" style="display:none">
<div class="input-field col s12">
<select id="section-list" name="section-list">
<option value="" disabled selected>Choose your section</option>
</select>
<label>Section</label>
</div>
</div>
Update [as suggested by ccKep]:
I can now see the data that I want through console.log(data), the thing that it outputs is:
<option value="9-1">9-A...</option>, now I want to add it to my main select list but it doesn't let me.
The output should be:
<select id="section-list" name="section-list">
<option value="" disabled selected>Choose your section</option>
<option value="9-1">9-A...</option>
</select>
I am having problem passing two select to $_POST
i have two select and when i post from the first the value is not been saved ,so when i post from the second select doesn't remember the first select value ,I want to pass the first select and save the value to select.
<form method="post" action="index.php">
<select id="city" name="city" class="styled-select">
<option value="all" selected="selected">all</option>
<option value="Van">Vancouver</option>
<option value="vic">Victoria</option>
</form>
<form method="post" action="index.php">
<select id="dept" name="dept" class="styled-select">
<option value="all" selected="selected">all</option>
<option value="1">First</option>
<option value="2">Second</option>
</form>
and i am using jquery to submit the form in change
when first form submitted select does not save the value of the selected option
$(document).ready(function() {
$('#city').change(function() {
$(this).find("option[selected='true']").removeAttr('selected');
$(this).find('option:selected').attr('selected', 'true');
this.form.submit();
});
$('#dept').change(function() {
$(this).find("option[selected='true']").removeAttr('selected');
$(this).find('option:selected').attr('selected', 'true');
this.form.submit();
});
});
I noticed you were missing some commas and that you didn't pass any data. Try this instead.
$("#city").change(function()
{
var selected = $("#city").find(':selected').val();
$.ajax({
url: "index.php",
type: 'POST',
data: {
"city": selected
},
success: function(data) {
console.log("success");
}
});
});
Also, you should be able to get the value of a select via val() e.g. $("#city").val().
For normal submit:
<form method="post" action="index.php">
<select id="city" name="city" class="styled-select">
<option value="all" <?php echo $_POST['city']==="all" ? "selected" : "" ?>>all</option>
<option value="Van" <?php echo $_POST['city']==="Van" ? "selected" : "" ?>>Vancouver</option>
<option value="vic" <?php echo $_POST['city']==="vic" ? "selected" : "" ?>>Victoria</option>
</form>
I have a form that queries a library database on another site. It works perfectly if I only include input fields for the "format" and "keyword" because the resulting parameters passed to the url come out in the correct order and the destination site recognizes it.
However, I also need my form to have an input field to select to search by "Author", "Title", "Subject" etc. This is what causes the problem. The destination site only recognizes this parameter if it is in the middle of the url (inside the keyword parameter).
My current resulting url:
http://alpha2.suffolk.lib.ny.us/search~S50/X?SEARCH=harry&searchscope=50&SORT=D&m=b
What I need the url to look like:
http://alpha2.suffolk.lib.ny.us/search~S50/X?SEARCH=t:(harry)&searchscope=50&SORT=D&m=b
If you compare the two you will notice a couple differences. First, ignore the parentheses around harry. That doesn't make a difference. The real issue is how do I get the "t:" to be inserted into my url? The "t:" comes from selecting "Title" as the thing to search by.
Here is my current form HTML (If you remove the "searchtype" select box at the bottom the form will execute without errors, but I need it to execute with it.)
<form class="form-inline" role="search" method="get" name="searchform" id="searchform" action="http://alpha2.suffolk.lib.ny.us/search~S50/X">
<div class="form-group" style="float: left; margin-top: 6px;">
<label class="form-title">Search Collection</label>
</div>
<div class="form-group">
<input type="text" value="" name="SEARCH" id="SEARCH" placeholder="Enter Search Terms..." />
<input type="hidden" value="50" name="searchscope" />
<input type="hidden" value="D" name="SORT" />
</div>
<div class="form-group" style="float: left; margin-top: 3px;">
<label for="searchformat">For:</label>
<select name="m" id="m">
<option value="">ANY</option>
<option value="a">BOOK</option>
<option value="e">EBOOK DOWNLOAD</option>
<option value="l">LARGE PRINT BOOK</option>
<option value="b">BLU-RAY</option>
<option value="g">DVD</option>
<option value="i">AUDIO BOOK CD</option>
<option value="h">AUDIO BOOK MP3CD</option>
<option value="x">EAUDIOBOOK DOWNLOAD</option>
<option value="q">PLAYAWAY</option>
<option value="j">MUSIC CD</option>
<option value="p">MAGAZINE/NEWSPAPER</option>
<option value="n">EMAGAZINE DOWNLOADS</option>
<option value="v">PLAYAWAY VIEW</option>
<option value="s">VIDEO GAME</option>
<option value="r">CD-ROM</option>
<option value="d">VHS</option>
<option value="t">GAMES/PUZZLES</option>
<option value="f">DIGITAL IMAGE</option>
<option value="z">EVIDEO DOWNLOADS</option>
<option value="y">EMUSIC DOWNLOADS</option>
<option value="c">SHEET MUSIC</option>
<option value="m">MAP</option>
<option value="w">ONLINE RESOURCE</option>
<option value="o">OTHER MATERIALS</option>
</select>
</div>
<div class="form-group" style="float: left; margin-top: 3px;">
<label for="searchtype">By:</label>
<select name="" id="searchtype">
<option value="a:"> Author</option>
<option value="t:"> Title</option>
<option value="d:"> Subject</option>
<option value="N:"> Note</option>
<option value="" selected="selected"> Keyword</option>
</select>
</div>
<button class="btn btn-primary" id="searchsubmit" type="Submit">GO</button>
</form>
EDIT:
The attempted Javascript (as requested in the comments):
<script>
function searchSubmit() {
m = document.getElementById("m").value;
t = document.getElementById("searchtype").value;
a = document.getElementById("searcharg").value;
var newurl = "alpha2.suffolk.lib.ny.us/search/X~S22?SEARCH="; + t + a + "&searchscope=50&SORT=D&m=" + m;
var searchform = document.getElementById("searchform");
searchform.action = newurl; searchform.submit();
}
</script>
What I would do is get the searchtype-value and add it to the input-search before submitting. Here is a JS-fiddle example:
$(document).ready(function(){
$("#searchform").on("submit", function(e){
var keyWord = $("#SEARCH").val();
var searchtype = $("#searchtype").val();
$("#SEARCH").val(searchtype + keyWord);
});
});
If you want to use the JavaScript approach, you will have to use AJAX and prevent the default behavior of the button.
document.getElementById("searchsubmit").addEventListener("click", function(event){
event.preventDefault();
var xhttp = new XMLHttpRequest();
//here you set up your variables. One example is
var mysearch = "SEARCH=" + document.getElementById("searchtype").value + document.getElementById("SEARCH").value;
xhttp.open("GET", "yourURL?" + yourVariables, true);
xhttp.send();
});
I am trying to pass the html variable from the list menu to the perl script via POST but what happens is that the jQuery ajax() was able to execute the perl without including the value got from the document.getElementById. Here is my HTML code:
<form id="form">
<p id="survey">
<label for="list"><b>Ran survey</b></label>
</p>
<p>
<label for="division">Please select division:</label>
<select type="list" id="list" name="division">
<option>--Select One--</option>
<option value="DENVER">Denver</option>
<option value="PHOENIX_SOUTHWEST">Phoenix-Southwest</option>
<option value="PORTLAND">Portland</option>
<option value="SOUTHERN_HOUSTON">Southern-Houston</option>
<option value="NORCAL">Norcal</option>
<option value="SEATTLE">Seattle</option>
<option value="VONS_SoCal">Vons-Socal</option>
<option value="EASTERN">Eastern</option>
</select>
</p>
<p id="survey">
<input type="submit" value="Ran Survey" onclick="surveyFunction();" />
</p>
</form>
function surveyFunction() {
var div = document.getElementById("list").value;
$.ajax({
type: "POST",
url: "possurv.pl",
data: div,
success: function(result){
$('#survfeedback').html(result);
}
});
}
You need to include the parameter name in the querystring to assign the value to. I assume this should follow the standard practice of being the name of the control. In this case you can either build the querystring yourself:
data: 'division=' + encodeURIComponent(div),
Or you can send an object and let jQuery serialise it to a querystring for you:
data: { division: div },