HTML Form Data into Ruby Formula - javascript

I'm trying to create a page where I can type into a textbox, on that page, and then create a variable using the data.
So far, I have:
<FORM NAME = frmName>
First Nme: <INPUT TYPE = Text NAME = txtFirst value ="">
Last Nme: <INPUT TYPE = Text NAME = txtLast value ="">
<P>
Total: <INPUT TYPE = Text NAME = txtFullName value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Submit" onClick = calculate()>
</FORM>
The JavaScript is working that allows me to run the onClick command, but what I really need has to have a page where I can insert data, and then populate a variable with that data.
For example. Type my first name and last name into the textboxes on the page. Then a variable running in the background, with be created. (ex. variable = first + name). This variable can then be used in the code on the rest of the page. Please also keep in mind, I don't want to add the form data into a database.
Is it possible to do this in ruby only?

Related

How to set value of form input field?

This is my form input field (without classes and such):
<input id="input_partnerID" type="value" name="partner_id" value=""/>
I want to set the input value to the contact id of the logged in user. I can get that value with this Qweb code:
<p id="value_parterID" t-esc="user_id.partner_id.id"/>
And to get that value in my input form I use this javascript. The method is called when the "accept terms and conditions" button is clicked.
function getID() {
document.getElementById("input_partnerID").value = document.getElementById("value_parterID").innerHTML;
}
This works but probably isn't the most efficient way to do this.
How can I use Qweb to fill in the input value in 1 or 2 lines preferably without javascript?
You can set the attribute value using t-att-value="".
So in my case I should use this input field:
<input type="value" name="partner_id" t-att-value="user_id.partner_id.id"/>
Which does the same as the given example with the <p> and javascript.

display the value of <input type="search"> input field on form after the first query (javascript)

i am using an HTML5 input field on a get form to implement search on my site:
<input type="search" name="q" placeholder="search" value="">
everything works, but after the first search the search string the user entered is not saved in the search field, rather the placeholder attribute value is displayed again.
after the first search i would like to have the search string the user entered displayed in the search field on the form.
that means that value attribute of the input field needs to be "" before the first search (so that the placeholder attribute value will display) and then dynamically updated after the first search.
I understand from the Mozilla documentation on <input type="search">
that the value the user entered is stored in the DOMstring "searchTerms = mySearch.value;" but my programming skills and experience are limited.
i am not using php, thank-you in advance.
Try adding this event.preventDefault(); on your function, with that your event will be canceled but the propagation of it won't. If you want to learn more about it, here's the documentation: https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
solved the problem with:
DOMContentLoaded to trigger the javascript
(https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event)
URLSearchParams and URLSearchParams.get() to get query string
(https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
(https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/get)
getElementById to set value in input field
(https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById)
it is a common question-
(How can I get query string values in JavaScript?)
<input type="search" name="q" placeholder="search" id=mySearch required value="">
<script type="text/javascript">
window.addEventListener('DOMContentLoaded', (event) => {
let params = new URLSearchParams(document.location.search.substring(1));
let qstring = params.get("q"); // is the string for the query
document.getElementById("mySearch").value = qstring;
});
</script>

Copying the value of a text field to another file's text field

I have a pop-up window from file_A.asp that has an input of a text_field1, Once the user key in the value of text_field1 and click on Save button; it will redirect him to file_B.asp where the value written in text_field1 is copied to a text_field2 of File_B.asp
I know that it's impossible to copy a value of a text field from a file to another, that's why I need a JavaScript function to pass the value to the second file. but the thing is I don't know how to implement it.
Can some one please help me?
Make sure your form attributes are set correctly...
<form id="main" method="post" target="form_b.asp">
<input name="aField" type="text" />
...or...
<form id="main" method="get" target="form_b.asp">
<input name="aField" type="text" />
...and...
Dim aField
aField = Request.Form("aField")
...or...
Dim aField
aField = Rquest.QueryString("aField")
Note: corrected to name attribute from id.
Maybe you should try passing value of text_field1 using URL ?
Ex :
file_B.asp?val=VALUE_OF_TEXT_FIELD1
so you can get it like this ;
Request.QueryString("val")

Store data on page to variables using Javascript/JQuery

So I am trying to store data to variables on the page when a button is clicked, there are multiple buttons and it needs to store data for that specific button. Each one of these buttons is nested inside a <form onsubmit></form> all the data I need to extra is within this form in different <input> and <div> tags. So using javascript or jquery how can I select the value of a specific and tag. So when the button is clicked on it adds this product to the cart, I want to take the productNumber, price, and quantity and store them to my own variables. When the button is clicked the forum calls onsubmit="ShoppingCartAddAJAX( this ); so I want to store this data in the ShoppingCartAddAJAX function.
Here is what one of the forms on the page looks like.
<form method="post" name="addtocart-501" onsubmit="ShoppingCartAddAJAX( this ); return false;">
<input type="hidden" name="formName" value="ShoppingCartAddAJAX" />
<input type="hidden" name="productNumber" value="758201" />
<input id="qtyproduct" type="hidden" class="hiddenqty" name="dmst_Qty_2805" value="1" />
<div class="price">
<div class="pricenew singleprice">
$7.99
</div>
</div>
</a>
</div>
</div>
</li>
</form>
So I have been trying to get this data by doing something like this.
var pn = $('input[name$="productNumber"]').val();
var qty = $('input[id$="qtyproduct"]').val();
In my javascript file the function looks something like this:
function ShoppingCartAddAJAX(formElement, productNumber) {
var pn = $('formElement.input[name$="productNumber"]').val();
var aa = $('formElement[name$="productNumber"]').val();
var qty = $('input[id$="qtyproduct"]').val();
}
But with alot more code... just showing that the function is passing in formElement and a productNumber.
But this is giving me the product number and quantity of the first product on the page, I need the data for which ever button the user decides to click on and there are multiple ones on the page not just one. I hope that when the button is clicked and that function is fired there is a way to look what forum it came from and then extract that data. I would also like to be able to get the price but it is stored in <div class="pricenew singleprice"> $7.99</div>.
Any help is greatly appreciated.
You are getting the product number and quantity of the first record since you have multiple input fields in the form with the name of productNumber(according to your description). So what basically happens here is when you call $('input[name="productNumber"]').val() jquery returns you the value of the first input field. Instead of that, do something like this inside your ShoppingCartAddAJAX function.
function ShoppingCartAddAJAX(form)
{
// Find child an element inside our form with the id of "qtyproduct"
// I used "find("#qtyproduct")" method so it searches anything nested inside your specific form element
// You can use "children("#qtyproduct")" method also
var qty = $(form).find("#qtyproduct").val();
var pn = $(form).find("input[name='productNumber']").val();
var pp = $(form).find(".pricenew.singleprice").text();
}

show submit <a href> link onchange and pass the value of the input box changed

I am not a JavaScript person really, I write in ASP and use a SQL database in the backend. But our marketing director requested a change that will use JavaScript.
On our view cart page, we're currently displaying an input box with a modify button to allow customers to change the quantity of the listed item (in that row... there could be multiple products in their cart, each having their own quantity input).
<form method="post" action="updateitem.asp">
<input type="hidden" name="product_id" value="<%= PRODUCT_ID %>">
Quantity: <input type"text" name="quantity">
<input type="image" name="Submit" src="/graphics/modify.gif" align="middle" border="0" alt="Continue">
</form>
What I'd like to make work is something like this. Hrm, assuming I need to do my form/div name differently for each product? I can easily write the product_id into the id tags but then assuming I'd also need to loop through my function for each one. I've gotten this far in writing the replacement code:
Get Dataset from Database (items in cart) and loop through:
<form method="post" action="updateitem.asp" id="updateitems<%= PRODUCT_ID %>">
Quantity: <input type="text" name="qty<%= PRODUCT_ID %>" OnChange="Javascript:UpdateQty()")
<div id="showlink<%= PRODUCT_ID %>">
<br /><span class="BodyTiny">update</span>
</div>
</form>
END LOOP
So if the quantity changes, it displays the word "update" where they can click and it passes whatever quantity that is in the quantity field to the updateitem.asp (in a way I can then update it in the database in ASP/SQL). In the code above, if we could just insert the new # in the a href statement after quantity=, then I could fix it in the updateitems.asp page without a problem.
I'm not sure where to even begin honestly, I have this so far:
LOOP through dataset so each product has its own function
<script Language="JavaScript">
<!--
function UpdateQty(updateitems<%= PRODUCT_ID %>) {
Show div updateitems<%= PRODUCT_ID %>
Replace NEWQUANT within that div with the value in the input field qty<%= PRODUCT_ID %>
}
//-->
</script>
END LOOP
I'm having a few problems...
I am not sure how to write the function UpdateQty. It should A) display the stuff in div id=showlink, and B) add the # from the input named quantity quantity to the href so I can update it in the database on the next page
If they have JavaScript turned off, they should be able to enter a new quantity and just hit enter for it to submit the form. I believe this will work as its a form with 1 text input and 1 hidden one, so just hitting enter in the text input should submit it. But that should still work with whatever JavaScript is added to make the showlink div show if it changes.
Do I need to add a class to my CSS for showlink? If so, what do I need to put in it?
Any help would be greatly appreciated!
Mahalo!
so what you can do for updateQty is have one function that will be correct for all products in a list just by making a function that finds all the necessary elements by relative paths.
//in this function the context is the element, so the
//`this` variable can be used rather than a param
//for the product id
function updateQty() {
//show the update link
var parent = this.parentNode;
//assumes only the update text uses the class bodyTiny, and that there
//is only one element using it. obviously making this invariant true
//would be trivial by adding a different class name
var updateText = parent.getElementsByClassName("bodyTiny")[0];
updateText.style.display = 'block';
var updateLink = updateText.parentNode
, updateHref = updateLink.href;
//next we find the current quantity and replace it with the input
updateHref = updateHref.replace(/qty=(\d*)/, 'qty='+this.value);
//then we set the link element's attribute
updateLink.href = updateHref;
//now when you mouse over the link you should see the url has changed
}
You can also set your form to POST the equivalent data, and then I guess on the server side you will have your OnGetPage and OnPostback both delegate to the same method with the parameters either parsed out of the query string or out of the post data.
You can see it running on jsfiddle. Hopefully this helps!

Categories

Resources