Can't post value to next HTML-document with hidden input - javascript

I'm trying to post a value to another HTML-document by a hidden input. Then I want to recieve the value with location.search and do something in the new HTML-document. The problem is that the doucment is opening, but the value isn't sent. Here is the HTML:
<form action="britzcwka.html" method="post" id="formID">
<input id="inpt" type="hidden" name="myInput" value="">
</form>
And here is the JavaScript-code:
document.getElementById("inpt").value = someValue;
alert(document.getElementById("inpt").value);
document.getElementById("theForm").submit();
The alert shows the correct value, but the address url is just britzcwka.html when on the other page.
Hank

You're using POST method, so your variables will not be included in the browser's address bar. Change form's method to GET:
<form method="GET" action="britzcwka.html" id="formID">
<!-- form's content -->
</form>

Related

Why is my search query returning undefined, while my page populates with the term's results? Using business catalyst

I'm trying to get the search term to appear in the URL. What am I doing wrong?
<form name="catsearchform74255" method="post" onsubmit="processSearch(this)" action="/Default.aspx?SiteSearchID=2248&ID=/search-results&keywords=">
<div class="input-field search-box">
<input id="CAT_Search" type="search" name="CAT_Search" placeholder="What are you looking for?" class="white" required="true">
<label class="label-icon" for="CAT_Search"><i class="material-icons">search</i></label>
<i class="material-icons">close</i>
</div>
<script type="text/javascript">
function processSearch(form) {
form.action = form.action + CAT_Search.value;
}
</script>
</form>
Change method="post" to method="get".
Update any server-side code referencing post variables to reference get variables. For example, in PHP code, change $_POST["CAT_Search"] to $_GET["CAT_Search"].
Also, the correct format for the required HTML attribute is either required="" or required="required.
Code
This can be done with JS, you can't edit the method as Business Catalyst expects a post for this form.
If you change the form to the following:
<form name="catsearchform74255" id="searchForm" method="post" action="/Default.aspx?SiteSearchID=2248&ID=/search-results&keywords=">
<div class="search-box">
<input class="cat_textbox_small" type="text" name="CAT_Search" id="CAT_Search">
<input id="submitForm" onclick="submitFormScript()" type="button" class="cat_button" value="Search">
</div>
</form>
and then add the following jQuery:
function submitFormScript() {
var searchAction = $("#searchForm").attr("action");
searchAction = searchAction + $("#CAT_Search").val();
$("#searchForm").attr("action", searchAction);
$("#searchForm").submit();
}
Explanation
By adding the ID's to the fields on the form and then taking type="submit" off the input button we can edit the form action before we submit the form.
In the JS we are getting the form action, adding on the value of the search box (users input) and then setting that back to the form action attribute. After that we have the URL that we want to send to the next page so we can then submit the form.

Angular submit post data to Iframe from controller programmatically

what i am trying to achieve is that i want to load an iframe and send some data using the post method.
I want it to happen programmatically so i don't want a user click.
here is the code that i have come up so far.
In my view i have the following:
<form id="submitValues" target="siteFrame"
enctype="application/x-www-form-urlencoded"
method="POST" action="http://localhost/PHP/dataSender.php">
<input type="text" name="jwt" ng-model="jwtData"/>
<input type="hidden" name="name" ng-model="Hiddenname"/>
<input type="hidden" name="type" ng-model="typeData"/>
</form>
<iframe name="siteFrame" id="siteFrame"
seamless="seamless" onLoad="iframeLoadDone()"></iframe>
Note that the target of the form is the iframe.
In my controller i am doing this :
$scope.jwtData="someData";
$scope.Hiddenname="someData";
$scope.typeData="someData";
document.getElementById('submitValues').submit();
The value in the ng-model are not getting sent, they do show up in the view if i remove the "hidden" value form the inputs.
How to send the values?

Unable to send parameters to servlet on form submit through javascript

I have a HTML form as follows:-
<form name="login" id="login" action="<%=application.getContextPath()%>/GoogleLogin" method="get">
<input type=hidden id="firstName"/>
</form>
I am setting the values of this hidden input type in javascript and submitting the form to servlet as follows:-
<script>
document.getElementById("firstName").value="XYZ";
document.getElementById("login").submit();
<script>
My form is getting submitted but I am not able to get the request parameter "firstName".
http://localhost:8080/Login/GoogleLogin?
Thank you for your time and consideration.
Add a name attribute to the input field -
<input type="hidden" id="firstName" name="firstName" />
then you'll be able to get the request parameter using firstName.

How can I submit div data to MySQL

I was wondering how can I submit div data to MySQL. Im not used to javascript so I dont really know whats happening on the javascript part but how can I get or input the action="" part and method="" part and can I or should I add value="" to the hidden input???
Form html code:
<form onsubmit="document.getElementById('hidden_data').value=document.getElementById('showing_data').innerHTML;">
<input id="hidden_data" name="data" type="hidden"/>
<div id="showing_data" class="commenttext" contenteditable="true"></div>
<input type="submit" Value="Enter" id="submitthis">
</form>
Use the hidden field inside the form tag and use the JavaScript to put the value inside it. You can get the hidden field in the $_POST['hydName'].Put the data on the click of the submit button into the hidden field. Keep your action and method of the form same as required. After the click event is fired, it will submit the form to its action URL
<input type="submit" onclick="document.getElememtById('hidden').value = document.getElementById('div').innerHtml;" />

Pass parameter from a text field contained in another form

I have two forms in a JSP page, one contains a text field. I need to make sure that whenever the other form is submitted, it copies the value contained in that text field, and writes it's text value in an hidden parameter.
More clearly:
One form is named "search";
Another form is named "changeInitialForm";
The text field is contained in the "search" form, and it's name is "searchString";
The "changeInitialForm" has an hidden field, also this named "searchString";
The purpose is to make sure that whether the user submits one or another form, a "searchString" parameter is passed, with the value of the text field.
I tried to include an action in javascript, executed when the "changeInitialForm" is submitted, that reads the text field value and writes it into the hidden parameter:
function searchContacts(form)
{
var searchString= document.search.searchString.value;
form.searchString.value= searchString;
}
...
<form name="search" >
<input type="text" name="searchString">
<button name="search"> Cerca </button>
</form>
...
<form name="changeInitialForm" method="post" action="AddressBookView.jsp" onSubmit="searchContacts(this.form);">
<input type="hidden" name="selectedInitial"/>
<input type="hidden" name="status" value="view"/>
<input type="hidden" name="searchString" />
</form>
But after the "changeInitialForm" is submitted, regardless of the text field value, and empty parameter is passed, I am seeing this with firebug:
I would also appreciate an alternative solution, because I know what I am doing is tricky, but I don't find another method to do that. "search" and "changeInitialForm" cannot be joined in a single form, because they do very different things.
The following seems to work
function searchContacts(form)
{
var searchString= document.search.searchString.value;
form.searchString.value= searchString;
}
...
Form 1:
<form name="search" >
<input type="text" name="searchString">
<button name="search"> Cerca </button>
</form>
...
Form 2:
<form name="changeInitialForm" method="post" action="AddressBookView.jsp" onSubmit="searchContacts(this);">
<input type="hidden" name="selectedInitial"/>
<input type="hidden" name="status" value="view"/>
<input type="hidden" name="searchString" />
</form>
Notice that searchContacts(this.form) was replaced with searchContacts(this).
UPDATE after some precisions by the author of the question:
The onsubmit event is not triggered when form.submit() is called by some javascript code. Thus, what you need when you submit the form is to call searchContacts separately, for example using
searchContacts(document.changeInitialForm);
document.changeInitialForm.submit();

Categories