HTML Get method - html

I have create a form with one text field and one select field and using GET method. Is it possible to make the parameter combine to one when submit the form? Example: test.html?domain=test.com
<form action = "test.html" method = "GET">
Name: <input type = "text" name = "domain" />
<select name="domain_ext" class="inputAuto">
<option value=".com">.com</option>
</select>
<input type = "submit" />
</form>

You can use XHR https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
Here's a very basic example:
var getData = function () {
var xhr = new XMLHttpRequest();
var domain = document.querySelector('.domainInput').value;
var ext = document.querySelector('.extInput').value;
xhr.responseType = 'text';
xhr.onload = function () {
if (xhr.readyState === xhr.DONE && xhr.status === 200) {
document.querySelector('.output').innerText = xhr.responseText;
}
};
xhr.open('GET', domain + ext, true);
xhr.send(null);
};
document.querySelector('.getButton').addEventListener('click', getData);
<form>
Name: <input type="text" value="https://output.jsbin.com/juwuy" class="domainInput" />
<select class="extInput">
<option value=".js" default>.js</option>
<option value=".com">.com</option>
</select>
<input type="button" value="Get" class="getButton"/>
</form>
<p class="output"></p>

Related

show selected option value info to other select tag [duplicate]

i have the following problem:
I started to create a form with HTML an JS and there are two Dropdowns (Country and City). now i want to make these two dynamic with JQuery so that only the cities of the selected countries are visible.
I've started with some basic JS which worked fine but makes some trouble in IE. Now i'm trying to convert my JS to JQuery for a better compatibility.
My original JS looks like this:
function populate(s1, s2) {
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
s2.innerHTML = "";
if (s1.value == "Germany") {
var optionArray = ["|", "magdeburg|Magdeburg", "duesseldorf|Duesseldorf", "leinfelden-echterdingen|Leinfelden-Echterdingen", "eschborn|Eschborn"];
} else if (s1.value == "Hungary") {
var optionArray = ["|", "pecs|Pecs", "budapest|Budapest", "debrecen|Debrecen"];
} else if (s1.value == "Russia") {
var optionArray = ["|", "st. petersburg|St. Petersburg"];
} else if (s1.value == "South Africa") {
var optionArray = ["|", "midrand|Midrand"];
} else if (s1.value == "USA") {
var optionArray = ["|", "downers grove|Downers Grove"];
} else if (s1.value == "Mexico") {
var optionArray = ["|", "puebla|Puebla"];
} else if (s1.value == "China") {
var optionArray = ["|", "beijing|Beijing"];
} else if (s1.value == "Spain") {
var optionArray = ["|", "barcelona|Barcelona"];
}
for (var option in optionArray) {
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s2.options.add(newOption);
}
};
and here my Jquery:
http://jsfiddle.net/HvXSz/
i know it is very simple but i can't see the wood for the trees.
It should as simple as
jQuery(function($) {
var locations = {
'Germany': ['Duesseldorf', 'Leinfelden-Echterdingen', 'Eschborn'],
'Spain': ['Barcelona'],
'Hungary': ['Pecs'],
'USA': ['Downers Grove'],
'Mexico': ['Puebla'],
'South Africa': ['Midrand'],
'China': ['Beijing'],
'Russia': ['St. Petersburg'],
}
var $locations = $('#location');
$('#country').change(function () {
var country = $(this).val(), lcns = locations[country] || [];
var html = $.map(lcns, function(lcn){
return '<option value="' + lcn + '">' + lcn + '</option>'
}).join('');
$locations.html(html)
});
});
Demo: Fiddle
I'm going to provide a second solution, as this post is still up in Google search for 'jquery cascade select'.
This is the first select:
<select class="select" id="province" onchange="filterCity();">
<option value="1">RM</option>
<option value="2">FI</option>
</select>
and this is the second, disabled until the first is selected:
<select class="select" id="city" disabled>
<option data-province="RM" value="1">ROMA</option>
<option data-province="RM" value="2">ANGUILLARA SABAZIA</option>
<option data-province="FI" value="3">FIRENZE</option>
<option data-province="FI" value="4">PONTASSIEVE</option>
</select>
this one is not visible, and acts as a container for all the elements filtered out by the selection:
<span id="option-container" style="visibility: hidden; position:absolute;"></span>
Finally, the script that filters:
<script>
function filterCity(){
var province = $("#province").find('option:selected').text(); // stores province
$("#option-container").children().appendTo("#city"); // moves <option> contained in #option-container back to their <select>
var toMove = $("#city").children("[data-province!='"+province+"']"); // selects city elements to move out
toMove.appendTo("#option-container"); // moves city elements in #option-container
$("#city").removeAttr("disabled"); // enables select
};
</script>
I have created cascading Dropdown for Country, State, City and Zip
It may helpful to someone. Here only some portion of code are posted you can see full working example on jsfiddle.
//Get html elements
var countySel = document.getElementById("countySel");
var stateSel = document.getElementById("stateSel");
var citySel = document.getElementById("citySel");
var zipSel = document.getElementById("zipSel");
//Load countries
for (var country in countryStateInfo) {
countySel.options[countySel.options.length] = new Option(country, country);
}
//County Changed
countySel.onchange = function () {
stateSel.length = 1; // remove all options bar first
citySel.length = 1; // remove all options bar first
zipSel.length = 1; // remove all options bar first
if (this.selectedIndex < 1)
return; // done
for (var state in countryStateInfo[this.value]) {
stateSel.options[stateSel.options.length] = new Option(state, state);
}
}
Fiddle Demo
I have a handy code. you can just copy it:
Same as above
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
jQuery(function($) {
var locations = {
'Germany': ['Duesseldorf', 'Leinfelden-Echterdingen', 'Eschborn', 'asdasdasd'],
'Spain': ['Barcelona'],
'Hungary': ['Pecs'],
'USA': ['Downers Grove'],
'Mexico': ['Puebla'],
'South Africa': ['Midrand'],
'China': ['Beijing'],
'Japn': ['tokyo'],
'Shuidong': ['shuidongjie','maomingjie'],
'Russia': ['St. Petersburg'],
}
var $locations = $('#location');
$('#country').change(function () {
var country = $(this).val(), lcns = locations[country] || [];
var html = $.map(lcns, function(lcn){
return '<option value="' + lcn + '">' + lcn + '</option>'
}).join('');
$locations.html(html)
});
});
</script>
</head>
<body>1
<label class="page1">Country</label>
<div class="tooltips" title="Please select the country that the customer will primarily be served from">
<select id="country" name="country" placeholder="Phantasyland">
<option></option>
<option>Germany</option>
<option>Spain</option>
<option>Hungary</option>
<option>USA</option>
<option>Mexico</option>
<option>South Africa</option>
<option>China</option>
<option>Japn</option>
<option>Shuidong</option>
<option>Russia</option>
</select>
</div>
<br />
<br />
<label class="page1">Location</label>
<div class="tooltips" title="Please select the city that the customer is primarily to be served from.">
<select id="location" name="location" placeholder="Anycity"></select>
</div>
</body>
</html>
This is an example that I've done. I wish that will be useful for you.
$(document).ready(function(){
var ListNiveauCycle = [{"idNiveau":1,"libelleNiveau":"CL1","idCycle":1},{"idNiveau":26,"libelleNiveau":"Niveau 22","idCycle":24},{"idNiveau":34,"libelleNiveau":"CL3","idCycle":1},{"idNiveau":35,"libelleNiveau":"DAlf3","idCycle":1}];
console.log(ListNiveauCycle);
function remplirListNiveau(idCycle){
console.log('remplirListNiveau');
var $niveauSelect = $("#niveau");
// vider la liste
$niveauSelect.empty();
for (var i = 0; i < ListNiveauCycle.length; i++) {
if(ListNiveauCycle[i].idCycle==idCycle){
var opt1 = document.createElement('option');
opt1.innerHTML = ListNiveauCycle[i].libelleNiveau;
opt1.value = ListNiveauCycle[i].idNiveau;
$niveauSelect.append(opt1);
}
}
}
$("#cycles").change(function(){
remplirListNiveau(this.value)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group row">
<label class="col-sm-3 col-form-label">Cycle</label>
<div class="col-sm-9">
<select class="form-control" id="cycles" required="">
<option value="">-----------</option>
<option value="1">Cycle1</option>
<option value="24">Cycle2</option>
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group row">
<label class="col-sm-3 col-form-label">Niveau</label>
<div class="col-sm-9">
<select id="niveau" class="form-control" required="" name="niveau.id">
</select>
</div>
</div>
</div>

I am not able to paste chinese content in textbox using angularjs

<input type="text" class="form-control name" name="name" id="focus_me" required maxlength="50" letters-with-space="" ng-trim="false" tabindex="1" ng-model="vm.detail.name" ng-paste="paste($event.originalEvent)" ng-init="vm.detail.name = null">
$scope.paste = function (event,field) {
var item = event.clipboardData.items[0];
item.getAsString(function (data) {
$scope.pastedData = data;
$scope.$apply();
});
}
Input : 继续
here is input , i am not able to paste it into textbox. how to enable it?
checkout this https://jsfiddle.net/geekcode/s91t2ryg/11/
I'm able to paste the Chinese content, just pass $event instead of $event.originalEvent.
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<input ng-model="mod" type="text" ng-paste="paste($event)" ng-init="mod = null">
{{mod}}
<input ng-model="mod1" type="text">
</div>
</div>
<script>
var app = angular.module("app", []);
app.controller("ctrl", function($scope){
$scope.paste = function (event,field) {
var item = event.clipboardData.items[0];
item.getAsString(function (data) {
$scope.pastedData = data;
$scope.$apply();
});
}
});
</script>

Can't login into a site using Google Apps Script and receive error 422

I need to scrape private data from a Japanese site.
But I can't login into the site and receive error code 422.
How can I login?
I need to log in into this website:
https://moneyforward.com/users/sign_in
The form on this site is:
<form class="form-horizontal mf-mb40" id="new_sign_in_session_service" action="/session" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓" />
<input type="hidden" name="authenticity_token" value="OGuijdFq6M1xngenCHi0BgZh9x0Nniw2HxiRhC9H2T0vbgWcWNRz+fmi5wxEdk4ua5TL9/UF7BapR2Af8CdILQ==" />
<div class="a-text--f14 mb3">e-mail</div><div class="a-text-box">
<input placeholder="entry" class="js-focus-form" type="email" name="sign_in_session_service[email]" id="sign_in_session_service_email" />
</div>
<div class="a-text--f14 mb3 mt20">password</div><div class="a-text-box">
<input placeholder="entry" type="password" name="sign_in_session_service[password]" id="sign_in_session_service_password" />
</div>
<div class="m-password-support">
<div class="a-checkbox password-show">
<label><input class="checkbox" id="show-ps" name="new1" type="checkbox" value="1" />
<span class="checkbox-icon"></span>
<span class="checkbox-txt">password</span></label>
</div>
<div class="password-forget">
forgot password
</div>
</div>
<div class="a-button primary">
<input type="submit" name="commit" value="login" id="login-btn-sumit" data-disable-with="login" />
</div>
</form>
I receive the error on last row "var response = UrlFetchApp.fetch(url, options)" below code.
function Login() {
var account ='***';
var password = '***';
var response = UrlFetchApp.fetch("https://moneyforward.com/users/sign_in");
var regexp = /<input type=\"hidden\" name=\"authenticity_token\" value=\"(.*?)\" \/>/;
var elements = response.getContentText().match(regexp);
var headers = response.getAllHeaders();
var cookies = [];
if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
for (var i = 0; i < cookies.length; i++) {
cookies[i] = cookies[i].split( ';' )[0];
};
};
var payload = {
utf8: "✓",
authenticity_token : elements[1],
email : "account",
password : password
};
var headers = { 'Cookie' : cookies };
options = {
method : "post",
headers : headers,
followRedirects: true,
contentType: "application/x-www-form-urlencoded",
//muteHttpExceptions : true,
payload : payload,
};
var url = "https://moneyforward.com/session";
var response = UrlFetchApp.fetch(url, options);
}

How to pass hidden values with ajax upload form

I'm busying with passing some values of hidden input with an ajax form. I've tried may ways but it looks like the values didn't go through.
HTML
<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="hidden" name="sid" value="$rec_sStdi[std_sid]" />
<input type="hidden" name="stid" value="$rec_sStdi[stdi_sid]" />
<input type="text" name="title" class="form-control" size="50" maxlength="128" autocomplete="off" placeholder="ชื่อไฟล์ เช่น แบบประเมินครู เป็นต้น"/>
<progress id="progressBar" value="0" max="100" style="width:100%;"></progress>
<button class="btn btn-primary btn-xs pull-right" type="button" value="Upload File" onclick="uploadFile()"><i class="glyphicon glyphicon-upload"></i> อัพโหลด</button>
<input type="file" name="file1" id="file1" class="btn btn-danger btn-xs pull-right">
<b id="status"></b>
<p id="loaded_n_total"></p>
</form>
Javascript
function _(el){
return document.getElementById(el);
}
function uploadFile(){
var file = _("file1").files[0];
//alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "inc/eval_file_upload.php");
ajax.send(formdata);
}
function progressHandler(event){
_("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
}
function errorHandler(event){
_("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
_("status").innerHTML = "Upload Aborted";
}
PHP (eval_file_upload.php)
<?
$sid=$_POST['sid'];
$stid=$_POST['stid'];
$title=$_POST['title'];
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(move_uploaded_file($fileTmpLoc, "../files/$fileName")){
echo "\"$fileName\" upload is complete (sid:$sid , stid:$stid, title:$title)";
} else {
echo "move_uploaded_file function failed";
}
?>
I tried to test the passing by echoing the hidden values. But nothing shows up.
ref : http://www.developphp.com/view.php?tid=1351
Try to add the params in your ajax call:
sid = document.getElementById("sid").value;
stid = document.getElementById("stid").value;
formdata.append("sid", sid);
formdata.append("stid", stid);
And adds an id to your input type hidden.
<input type="hidden" id="sid" name="sid" value="$rec_sStdi[std_sid]" />
<input type="hidden" id="stid" name="stid" value="$rec_sStdi[stdi_sid]" />

How to store value in localstorage on button click using knockout js?

This is my code but when i type text into input box after that i click on save button then text is saving into dropdown list but this text is't storing in local storage and for this i m using knockout.js
<form style="margin-top: -25px;">
<button id="buttonSave" type="submit" style="margin-left: 1156px;">Save</button>
</form>
<div id="labelList" class="btn-group" style="margin-top: -595px;margin-left: 3px;">
<input id="editExistannotation" data-bind="value: annotationList" class="editAnnotationList textArea" type="text" placeholder="Edit existing annotation"/>
<select data-bind="options: area"></select>
</div>
----------------------------------------------------
var addHandle = function () {
this.items = ko.observableArray();
this.add = function (item) { this.items.push(item); }
this.remove = function (item) { this.items.remove(item); }
this.clear = function () { this.items.removeAll(); }
}
var addHandler = new addHandle();
ko.applyBindings(addHandler, document.getElementById("slider"));
$("#buttonSave").click(function () {
var label_object;
var labelText = document.getElementById("textarealabel");
var labelObject = new Object();
labelObject.textarealabel = labelText.value;
localStorage.setItem('label_object', JSON.stringify(labelObject));
$("#ddlList").prepend("<option value='0'>" + localStorage.getItem(label_object) + "</option>");
return false;
})
var existAnnotationmodel = new function () {
var labelObject = $('#textarealabel').val();
this.annotationList = ko.observable();
this.area = ko.observableArray();
this.append = ko.computed(function () {
this.area.push(this.annotationList());
localStorage.setItem('labelObject', JSON.stringify(labelObject));
}, this);
}
ko.applyBindings(existAnnotationmodel);
Now when you type text into the input type="text" and you click on the button. The text is added to the options and to the local storage.
I also remove non relevant code.
<form >
<button id="buttonSave" type="submit" data-bind="click:append" >Save</button>
</form>
<div id="labelList" class="btn-group" >
<input id="editExistannotation" data-bind="value: annotationList" type="text" />
<select data-bind="options: area"></select>
</div>
var VM = function () {
this.annotationList = ko.observable();
this.area = ko.observableArray();
this.append = function () {
this.area.push(this.annotationList());
localStorage.setItem('labelObject',this.annotationList());
localStorage.setItem('labelObjectList',this.area());
};
};
var existAnnotationmodel = new VM();
ko.applyBindings(existAnnotationmodel);
See fiddle