I have a simple product selection form where user gets to select how many products to buy. Based on his selection, I would need to append the URL with his selection. If he selects Product1 and Product2 the submit button should add those two products to the cart with the following parameters https://example.com?add-to-cart=10,20
Here is example of my form
<form action="/action_page.php">
<input type="checkbox" id="10" name="vehicle1" value="Bike">
<label for="vehicle1"> Product 1</label><br>
<input type="checkbox" id="20" name="vehicle2" value="Car">
<label for="vehicle2"> Product 2</label><br>
<input type="checkbox" id="30" name="vehicle3" value="Boat">
<label for="vehicle3"> Product 3</label><br><br>
<input type="submit" value="Submit">
</form>
How can I achieve this?
Found something similar here but don't know how to modify it per my example.
This will redirect to example.com?add-to-cart=10,20
<script>
$('form').on('submit', function(e){
e.preventDefault();
let params = ''
const redirect_url = 'axample.com?add-to-cart'
let count = $('input[name^="vehicle"]:checked').length
$('input[name^="vehicle"]:checked').each(function(index) {
params += $(this).attr('id')
if(index !== count - 1) {
params += ','
}
});
// Amend redirect url in form by adding fields from form
window.location.href = redirect_url + '=' + params
});
<script>
Related
I want to answer surveys on a form on a website by making an html parser on Flutter.
The html output is as follows. For example, how can I select the value of the poll with the name poll_13 as 120.
<div id="polls">
<form id="polls_form_1" class="wp-polls-form" action="/index.php" method="post">
<input type="radio" id="poll-answer-10" name="poll_13" value="117">
<input type="radio" id="poll-answer-11" name="poll_13" value="118">
<input type="radio" id="poll-answer-12" name="poll_13" value="119">
<input type="radio" id="poll-answer-13" name="poll_13" value="120">
<input type="button" name="vote" value="Vote" class="Buttons" onclick="poll_vote(13);">
</form>
</div>
I was able to select the poll-answer-13 element by importing the html and http packages, but I couldn't figure out how to click and submit the form.
void submitForm() async {
var response = await http.get(Uri.parse(
"https://xxxx.com/poll"));
var document = parse(response.body);
var pollAnswer118 = document.querySelector("#poll-answer-13");
}
I am trying to add the value of selected checkbox to anchor tag parameter "product-code" and "product-quantity". For example if someone click the first checkbox the product code parameter will be something like product-code="90;1" and product-quantity="1;1". And if someone also check the second checkbox the product code parameter will be like product-code="90;1;2" and product-quantity="1;1;1". Same for the third parameter. Can someone help me with this how I can get this done with the help of jquery?
<input type="checkbox" name="Apple" value="1">
<input type="checkbox" name="Banana" value="2">
<input type="checkbox" name="Orange" value="3">
Order Now!
This solution will work for you.
jQuery(document).ready(function($){
$('input[type=checkbox]').on('change',function(){
let newProductCode = defaultProductCode = $('.avangate_button').attr('product-code').split(';')[0];
let newProductQty = defaultProductQty = $('.avangate_button').attr('product-quantity').split(';')[0];
$('input[type=checkbox]').each(function( index ) {
if($(this).is(':checked')){
newProductQty = newProductQty + ";" + 1;
newProductCode = newProductCode + ";" + $(this).val();
}
});
$('.avangate_button').attr('product-code',newProductCode);
$('.avangate_button').attr('product-quantity',newProductQty);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="Apple" value="1">
<input type="checkbox" name="Banana" value="2">
<input type="checkbox" name="Orange" value="3">
Order Now!
its simple you just create two variables e.g productCode & productQuantity and place it anchor tag . Create a function to set upper variables value and execute this function on "onChange" event of checkbox .
here is the code :
<input type="checkbox" name="Apple" value="1">
<input type="checkbox" name="Banana" value="2">
<input type="checkbox" name="Orange" value="3">
<a href="#buy" class="avangate_button btn btn-primary" product-code="90"
product-
quantity="1">Order Now!</a>
var productCode = "90";
var productQuantity = "1";
$("input[type=checkbox]").change(function(val){
productCode = productCode + ";" + val;
productQuantity = productQuantity + ";" + val;
});
If you really want to work with the attribute product-code then the following snippet should do the trick:
const a=document.querySelector("a");
const cbs=[...document.querySelectorAll("input[type=checkbox]")];
cbs.forEach(cb=>cb.addEventListener("click",ev=>{
a.setAttribute("product-code",[90,...cbs.filter(c=>c.checked).map(c=>c.value)].join(";"));
console.log(a);
}));
<input type="checkbox" name="Apple" value="1">
<input type="checkbox" name="Banana" value="2">
<input type="checkbox" name="Orange" value="3">
<a href="#buy" class="avangate_button btn btn-primary" product-code="90"
product-
quantity="1">Order Now!</a>
Please note:
According to the MDM web docs the <a> element does not actually support an attribute product-code. You should therefore rather use something like data-product-code instead.
So I am building an ecommerce website and I am at the product page. I want a filter system where the filter values get appended to the existing URL instead of loading a new URL of the latest filter parameter.
For example, if I have two elements as such:
<input type="checkbox" name="checkbox1" value="test1">
and
<input type="button" name="button1" value="a button">
and each are from different forms.
Let's say I submit the first form. The url becomes:
.../?checkbox1=test1
When I also submit the second form (after submitting the first form), the url now becomes:
.../?button1=a button
But I want the value of the button to be appended to the first url like:
.../?checkbox1=test1&button1=a button
How do I go about this?
Thanks
Have ONE form or do this
document.querySelector("[name=button1]").addEventListener("click",function(e) {
const chk = document.querySelector("[name=checkbox1]")
const but = this.value
let url = "/?button1=a%20button";
if (chk.checked) url +=`&${chk.name}=${chk.value}`;
//location = url
console.log(url)
})
<input type="checkbox" name="checkbox1" value="test1">
<input type="button" name="button1" value="a button">
Why you don't go like this ? Only single form :
<html>
<body>
<form action="redirect.html">
<input type="checkbox" name="checkbox1" value="test1"> <br><br>
<input type="button" name="button1" value="a button"> <br><br>
<input type="submit" name="submit-btn" value="submit">
</form>
</body>
</html>
I'd like to add HTML to a Google Site that allows a user to press a button that displays a random letter of the alphabet. However, it should randomize only the letters that the user selects through checkboxes. Below is an image of what I'd like to achieve, and I'd like the result to display to the right of the checkbox array.
As to what I have tried so far, I have the following code that I modified from an open source online. I hope it is ok for my purpose.
<!DOCTYPE html>
<html>
<body>
<h1>Pick Letters To Randomize</h1>
<form action="/action_page.php">
<input type="checkbox" id="letter1" name="letter1" >
<label for="letter1"> A</label><br>
<input type="checkbox" id="letter2" name="letter2" >
<label for="letter2"> B</label><br>
<input type="checkbox" id="letter3" name="letter3" >
<label for="letter3"> C</label><br><br>
<input type="submit" value="Randomize">
</form>
</body>
</html>
But I am really at a loss for how to solve the rest of my problem.
Here is a working example for you. I have a few suggestions that I've implemented that will make this easier for you:
Add a value to the checkbox input. That way, you don't have to grab a child/sibling label.
I've added comments to show what I'm doing. Hope that helps!
document.addEventListener("DOMContentLoaded", function() {
const form = document.getElementById("randomLetterForm");
const submitBtn = document.getElementById("randomSubmit");
const textResult = document.getElementById("result");
// We check the values on the submit click
submitBtn.addEventListener("click", function(e) {
// Prevent it from *actually* submitting (e.g. refresh)
e.preventDefault();
// Grab *all* selected checkboxed into an array
const items = document.querySelectorAll("#randomLetterForm input:checked");
// Checking if it's not empty
if (items.length > 0) {
// Setting a random index from items[0] to items[items.length]
textResult.innerHTML = items[Math.floor(Math.random() * items.length)].value;
} else {
// If not, we alert
alert("Please choose at least 1 number");
}
});
});
<h1>Pick Letters To Randomize</h1>
<form id="randomLetterForm" action="/action_page.php">
<input type="checkbox" value="A" id="letter1" name="letter1" >
<label for="letter1"> A</label><br>
<input type="checkbox" value="B" id="letter2" name="letter2" >
<label for="letter2"> B</label><br>
<input type="checkbox" value= "C" id="letter3" name="letter3" >
<label for="letter3"> C</label><br><br>
<input id="randomSubmit" type="submit" value="Randomize">
</form>
<div>
<p id="result"></p>
</div>
The following form shows a list of data on submit. I am trying to show no results found when there is no data on submit. I tried like shown below,it shows and hides the div. But when there is no options selected on form and click submit button it shows the no result div.How to show the no results div only when form validation succeeds and there is no data to display.
HTML
<div class="form-group" >
<label class="radio-inline">
<input name="sampleinlineradio" value="3" type="radio" ng-model="radioption"
ng-required="!radioption"> MAIN 1</label>
<label class="radio-inline">
<input name="sampleinlineradio" value="1" type="radio" ng-model="radioption" >
Main 2</label>
<div ng-show="!radioption && buttonClicked">Please select one.</div>
</div>
<div class="form-group">
<label ng-repeat="branch in branches">
<input type="checkbox" name="selectedBranches[]" value="{{branch.value}}"
ng-model="branch.selected" ng-required="isOptionsRequired()" >
{{branch.name}}</label>
<div ng-show="isOptionsRequired() && buttonClicked">Please select one.</div>
</div>
<input type="button" ng-click="fetchresults()" value="submit" >
<div ng-show="buttonClicked">
<table> <thead>.....</thead>
<tbody>
<tr ng-show="results.length!=0" ng-repeat="r in results">
<td></td></tbody></table>
</div>
<div ng-show="buttonClicked">
<span ng-show="results.length==0 ">No results.</span>
</div>
Minimal Controller Code
$scope.fetchresults = function(){
$scope.results = Result.query({main: $scope.radioption, branch: $scope.selection[0], });
$scope.buttonClicked = true;
}
EDIT:
I used model to validate and $valid is also working, as suggested below.But Got a couple of glitches. If i click the button it does not show div. but after validation is over it shows automatically "no results" from the click before. How to stop this.And while it lists data when its available it shows "no results" for a second or so
Give your form a name
<form name="formName">
Then you can do
ng-show="results.length==0 && formName.$valid"
Some more information on angularJS form validation
You can check the below snippet, I have changed most of them in angular way with $invalid, $pristine, $submitted and $valid.
You can check the angular documentation to read about them.
Link 1, Link 2
Note: You can use a submit button and get rid of ng-click event and use ng-submit instead which can't be used in this snippet as form submit is not allowed. Comment the line form.$submitted = true; when you use a submit button.
var app = angular.module('app', []);
app.controller('TestController', function($scope){
$scope.isFieldInvalid = function(field) {
var form = $scope.testForm;
return form[field].$invalid && (!form[field].$pristine || form.$submitted);
};
$scope.fetchResults = function(){
var form = $scope.testForm;
form.$submitted = true; // comment this line if button type="submit"
if(form.$valid){
$scope.searching = true;
$scope.results = [];
var rnd = Math.random();
if(rnd >= 0.5) {
$scope.results = [{id: 1}, {id: 2}];
}
//$scope.results = Result.query({main: $scope.radioption, branch: $scope.selection[0], });
$scope.buttonClicked = true;
$scope.searching = false;
}
};
});
angular.bootstrap(document, ['app']);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="testForm" ng-controller="TestController" ng-submit="fetchResults()" role="form" novalidate="">
<div class="form-group" ng-controller="TestController">
<label class="radio-inline">
<input name="sampleinlineradio" value="3" type="radio" ng-model="radioption" ng-required="true"> MAIN 1</label>
<label class="radio-inline">
<input name="sampleinlineradio" value="1" type="radio" ng-model="radioption" ng-required="true" >
Main 2</label>
<div ng-show="isFieldInvalid('sampleinlineradio')">Please select one.</div>
</div>
<div class="form-group">
<label ng-repeat="branch in branches">
<input type="checkbox" name="selectedBranches[]" value="{{branch.value}}"
ng-model="branch.selected" ng-required="isOptionsRequired()" >
{{branch.name}}</label>
<div ng-show="isOptionsRequired() && buttonClicked">Please select one.</div>
</div>
<input type="button" value="submit" ng-click="fetchResults()" >
<div ng-show="buttonClicked && !searching">
<table> <thead></thead>
<tbody>
<tr ng-show="results.length!=0" ng-repeat="r in results">
<td>{{r.id}}</td></tbody></table>
</div>
<div ng-show="buttonClicked && !searching">
<span ng-show="results.length==0 ">No results.</span>
</div>
</form>