get parameters from URL to html form - javascript

I created a button in html file that move to page with form:
<a class="btn goToProfile" href="{{ url_for('new_meeting', dog = dog[0] ) }}">Click here to schedule a meeting</a>
I add the parameter dog to the GET request
http://127.0.0.1:5000/new_meeting?dog=123
When I move to the new page I have a form:
<form action="/favorites/add_meeting" method="post">
<label for="time">Time:</label><br>
<input type="datetime-local" id="time" name="time">
<br>
<br>
<label for="place">Place:</label><br>
<input type="text" id="place" name="place"><br>
<br>
<label for="place">Dog ID:</label><br>
<input type="text" id="dog" name="dog" value="1" readonly><br><br>
<br>
<input type="submit" value="Submit">
</form>
How can I extract the parameter from the URL and add this to the value in the input tag?

You can find here about how to get query parameters with JavaScript
When you have parameters let's say inside dogName variable. You can change form input field with id dog like this:
document.getElementById("dog").value = dogName

Related

On change attribut is not initialised in thymeleaf

I have the following problem with javascript integrated into thymeleaf template.
I have the following table with different input fields which are filled up with data from a model.
Furthermore i have for each of this input field a hidden additional input field.
Idea is that when I change not hidden input tag, the hidden ones will receive the new value
<tr th:each=" item : ${products}">
<td>
<input class="form-control" type="text" th:value="${item.product.name}" id="productName" name="productName" />
</td>
<td>
<input class="form-control" type="text" th:value="${{item.product.price}}" id="productPrice" name="productPrice" />
</td>
<td>
<input class="form-control" type="text" th:onchange="substituteOnClick()" th:value="${item.quantity}" id="quantityItem" name="quantityItem" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.MinQuantity}" th:onchange="substituteOnClick()" id="minquantityItem" name="minquantityItem" />
</td>
<td>
<form th:action="#{/deleteWarehouseItem/{id_del}/(id_del=${item.id})}" method="post" role="form" class="ui form">
<button type="submit" class="ui form" name = "itemDel" th:value="${item.id}">Löschen</button>
</form>
</td>
<td>
<form class="ui form" method="post" th:action="#{/updateItem}" >
<input type="hidden" name="productName_mvc" id="productName_mvc" value="0" th:value="${item.product.name}"/>
<input type="hidden" name="productPrice_mvc" id="productPrice_mvc" value="0" th:value="${{item.product.price}}"/>
<input type="hidden" name="quantityItem_mvc" id="quantityItem_mvc" value="0" th:value="${item.quantity}"/>
<input name="minquantityItem_mvc" id="minquantityItem_mvc" value="0" />
<input type="hidden" name="inventoryIdentifier_mvc" id="inventoryIdentifier_mvc" th:value="${item.id}"/>
<button type="submit" class="ui labeled icon button" name = "updateItem" th:text="#{management.updateItem}">
</button>
</form>
</td>
</tr>
Here is my javascript file
function substituteOnClick() {
var pN=document.getElementById("productName").value;
var pP=document.getElementById("productPrice").value;
var qI=document.getElementById("quantityItem").value;
var MqI=document.getElementById("minquantityItem").value;
document.getElementById("productName_mvc").value=pN;
document.getElementById("productPrice_mvc").value=pP;
document.getElementById("quantityItem_mvc").value=qI;
document.getElementById("minquantityItem_mvc").value=MqI;
}
As you see my problem is that for each row I need 2 buttons for different actions, since that I decided that It would be useful just to change values of hidden inputs. It is possible because I use hidden inputs only if I need to update an object and it happens mainly on change event. But my problem is that even if I write anything new in not hidden inputs, it does not influence my hidden ones. I have cheked it by not hidding one of them.
How is it possible to change values in other inputs by changing them firstly in other ones.
Best wishes
first of all, you need to remove these extra brackets from this line
<input class="form-control" type="text" th:value="${{item.product.price}}" id="productPrice" name="productPrice" />
and then in your javascript use console.log() to check whether on onchange event its fetching the change value or not like
console.log("In js function");
console.log(qI);
if you are not getting these values then Instead of onchange event add a button for each input or for all input use a single button inside form and then use the onClick function or onsubmit like this:
<form onsubmit="return substituteOnClick()">
<td>
<input class="form-control" type="text" th:value="${item.product.name}" id="productName" name="productName" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.product.price}" id="productPrice" name="productPrice" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.quantity}" id="quantityItem" name="quantityItem" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.MinQuantity}" id="minquantityItem" name="minquantityItem" />
</td>
<input type="submit" name="Submit" value="Submit"/>
</form>
Let me know if this helps

Vue.js submit text button

I have a noob question.
i have a form with a text field. If i type something in, and push enter, no result. If i type something in, and push the button, i get the result i want. Can someone help me fix this - this is written in vue.js
<div class ="well">
<form class="form-inline" onsubmit="searchName">
<h1><label>Enter Search</label></h1>
<input type="text" name="name" class="form-control" v-model="search">
</form>
</div>
<input id="clickMe" type="button" value="clickme" v-on:click="searchName" />
You may add an event in your text field.
<input
type="text"
name="name"
class="form-control"
v-model="search"
v-on:keyup.enter="searchName"
/>
Or add a submit event in your form
<form
class="form-inline"
v-on:submit.prevent="searchName"
>
put your button inside the <form> tag and change the button type to submit:
<div class ="well">
<form class="form-inline" #submit.prevent="searchName">
<h1><label>Enter Search</label></h1>
<input type="text" name="name" class="form-control" v-model="search">
<input id="clickMe" type="submit" value="clickme"/>
</form>
</div>
EDIT
instead of onclick event in the button, use #submit.prevent in the form.

HTML code- the output is not what i want

<body>
<form action= "http://norton.open.ac.uk/reflect.php" method= "post" name= "form1">
<h1> Book Details: </h1>
<select name= "dropdown">
<option value= "Books" selected>Books</option>
<option value= "Magazines">Magazines</option>
<option value= "Journals">Journals</option>
<option value= "Newspapers">Newspapers</option>
<p>
<b>Book name:</b> <input type = "text" name = "book_name" /> </p>
<p>
<b>Author:</b> <input type = "text" name = "author" /> </p>
<p>
<b>Publication Year:</b> <input type = "date" name = "publication_year" /> </p>
<p>
<b>ISBN Number:</b> <input type = "number" name = "isbn_number" /> </p>
<input type= "submit" name= "submit" value= "Submit" />
</form>
I'm pretty sure something is wrong with the code, I just can't pinpoint what exactly it is. The output is not what I want.
So there are a couple of things that you will need to do to get this to work...
<input type="submit" />
you could use an id if you need to set some sort of submit in JS.
not sure 100% what you have going here but your form could look something like this:
<form action="http://norton.open.ac.uk/reflect.php" id="form1">
<div>
<label for="book">Book Name:</label>
<input type="text">
</div>
<div>
<label for="author">Author:</label>
<input type="text">
</div>
<div>
<label for="publication">Publication Year:</label>
<input type="text">
</div>
<div>
<label for="isbn">ISBN Number:</label>
<input type="number">
</div>
<div>
<input type="submit">
</div>
</form>
Make sure your values inside HTML element tags are: type="" vs. what is in your current state type= "". no need for spaces in between.
Also, for future reference please state a little more about what you are trying to accomplish. If you are submitting a form like this and you are using JS please specify what the current output is and what your desired output would be. It could also help if you supplied some research you have done to solve the problem.
Hope this helps you out!

How to assign GET parameter to Value attribute of HTML Input Text?

I have following HTML form in which I am getting name from get parameter. I am unable to assign the value of get parameter to the value attribute of form. In the text field "$_GET['name']" gets printed instead of its value. What I am doing wrong here in this code?
<!DOCTYPE html>
<html>
<body>
<form action="demo_form.asp">
Name: <input type="text" name="name" value=$_GET['name']><br>
Sex : <input type="text" name="sex" value="M"><br>
<input type="submit" value="Submit form">
</form>
</body>
</html>
Your code should be this:
Name: <input type="text" name="name" value="<?php echo $_GET['name'];?>"/>
^^^ you need to echo it
thats because u have not specified php tags and the browser wont be able to fetch your result.
if you are using asp than please use asp tags instead of php tags.
<form action="demo_form.asp">
Name: <input type="text" name="name" value="<?php echo $_GET['name'] ?>"><br>
Sex : <input type="text" name="sex" value="M"><br>
<input type="submit" value="Submit form">
</form>

Convert / Change Div element with Form element

I'm using panelbar, hence form tag is disturbing it's open/close animation
I found that form tag is creating issue, so I want div tag to convert to form tag when I click submit button.
Eg:
<div class="myForm">
<div id="detail">
Name: <input type="text" name="text_name" value="Some text here to edit"/>
</div>
<div id="income">
Income: <input type="text" name="text_income" value="your income"/>
</div>
<input type="submit" value="Submit" />
</div>
Convert to:
<form name="input" id="" action="html_form_action.php" method="post">
<div id="detail">
Name: <input type="text" name="text_name" value="Some text here to edit"/>
</div>
<div id="income">
Income: <input type="text" name="text_income" value="your income"/>
</div>
<input type="submit" value="Submit" />
</form>
So all I want is change the div with class ".myForm" to Form element
and closing div with closing form tag.
Is there any way to do this?
Use Javascript + jQuery:
$('.myForm').children().unwrap().wrapAll("<form name='input' id='' action='html_form_action.php' method='post'></form>");
This removes the wrapping div ".myForm" with the unwrap method. Then wraps it's children with the wrapAll method.
You can done this work simply using Jquery -
$(document).ready(function(){
$('form').html($('.myForm').html());
});
But if you want to do this, this is not correct because you use assign id of some element. So after append whole HTML into <form> you should remove .myForm.
Try This
For remove div with class .myForm after append innerhtml into form, you can simply use .remove.
$(document).ready(function(){
$('form').html($('.myForm').html());
$('.myForm').remove();
});
Try This
If you do like html5, you can check out html5 "form Attribute", which allows you to use the form elements wherever you like, and you don't need to wrap the contents inside form tags.
Check this out
<form name="input" id="myform" action="html_form_action.php" method="post"></form>
<div class="myForm">
<div id="detail">
Name: <input type="text" name="text_name" value="Some text here to edit" form="myform" />
</div>
<div id="income">
Income: <input type="text" name="text_income" value="your income" form="myform" />
</div>
<input type="submit" value="Submit" form="myform" />
</div>

Categories