Need jsp form to submit query to specific path - html

I'm trying to setup a simple form in a jsp that will put a search query in the URL submitted.
This is what I have so far:
<form action="search/" method="get" onsubmit="this.action+=this.q.value.trim();return true">
<input type="text" id="q">
<input type="submit" value="go">
</form>
This is in a jsp that's included on each page of my web app and it works when I'm at the root of the app. For example, if I initially load "http://localhost:8080/MyApp/" then type "123456" in the search form, it takes me to "http://localhost:8080/MyApp/search/123456", which is what I want. However, from that page, if I search again (for "654321" for example) it goes to "http://localhost:8080/MyApp/search/search/654321", which doesn't work for me. I need search/{query} to be appended to the root of the app's path no matter what page I may be on at the time.
I would like to avoid javascript if possible, I'm fine with JSTL though.
Any suggestions would be much appreciated!

Simply add a full path to your action, start it with /. Note it will depend on your application context, you could use :
<form action="${pageContext.request.contextPath}/search/"...
It will work in dev and also prod.
EDIT :
Path : search will try to load relative from current path so : http://domain.com/contact/search/
Path : /search will try to load relative from the domain so : http://domain.com/
With the context path above, it will work no matter if it change from dev to prod.
Hope this helps!

Related

How to find out what url HTML button submits to

So I'm scraping a website (instacart.com) and it requires a zip code to determine what data it displays. I want to use Python requests to post an arbitrary zip code. The only problem is I don't know what url to post it to and whether it requires any other arguments like an authenticity token or a user cache key. The zip code is entered via an text box that looks like this:
<form data-radium="true">
<input id="postalcode-16749"
name="postal_code"
type="text"
aria-invalid="false"
aria-describedby=""
autocomplete="on"
placeholder=""
data-radium="true"
value="" style=(super long block of css stuff)>
</form>
and then posted via a button that looks like this:
<button type="submit"
data-radium="true"
style="touch-action: manipulation; (long block of more css)">
Continue
</button>
I don't know a lot about web programming, but I was taught in school that HTML forms would look more like this: <form action="/action_page.php" method="get"> and you could use the action attribute to find where it was posting to. Is there a way to use the developer console to find what I'm looking for? How can I post a zip code to this website with Python?
Edit: I did a little more digging and I found that the request payload is {"current_zip_code":"some_zip_code"}, and that it's actually not using POST, it's using PUT. There's still a problem though, the request url looks like this: https://www.instacart.com/v3/bundle?source=web&cache_key= and then there's a different code each time for the cache_key. How do I know what url to post to?
I'm posting this answer in case anyone tries to do a similar thing. I found the url the button posts to and its parameters by looking in the network tab of the developer console and clicking the button. Then I ran into the problem that the url it sends the PUT request to changes every time, always ending in a different cache_key.
The solution was to use a python module called seleniumwire to simulate a browser and then grab all the network traffic. From there I looped through it and found urls containing cache_key= and stored everything after that as a string. Then tacked that string to the end of this url: https://www.instacart.com/v3/bundle?source=web&cache_key= and went back to using requests.
hope this helps someone!

Why does submit button demand a local resource request to favicon.ico?

I've got a very basic form here in which I want to test whether the input text value is correctly picked up when clicking the submit button.
<form id="form1" onSubmit="console.log(document.getElementById('inkomen1').value)">
<input type="text" id="inkomen1" name="inkomen1">
<button type="submit" form="form1" value="Submit">Submit</button>
</form>
So I've set the form onSubmit to display the contents of the input text field through console.log. Live this works, but locally I always get this strange error in my console:
Not allowed to load local resource: file:///favicon.ico
Why doesn't this work when I text the file locally and why this request for 'favicon.ico'? Can I make it work locally somehow too?
It's has nothing to do with the code you included in your question. You have an HTML code that has a link to a favicon. The link has an absolute path file:///favicon.ico which is not correct when running your code in a different environment.
Use a relative path, if your favicon is in the same path as your HTML you can set it to:
/favicon.ico
UPDATE
Try to add custom action to your form
<form id="form1" action"targetPageHere" onSubmit="console.log(document.getElementById('inkomen1').value)">
Maybe the default action page is not your page and has a different code that include this reference.

Laravel best way to form an HTML form and token

Hi I am working with Laravel and I have a question about the . Back in time we used to write something like the following when we want to submit data.
<form action="insert.php" method="POST"> </form>
Now, I have seen in Youtube videos and here in stackoverflow many code snips where we use the following:
<form action="{{URL::to('/insert')}}" method="POST">
I would like to know what is the difference? In the second way /insert is pointing to a file or a controller? The first one is wrong? Or it is just an alternative old fashion way?
Also I have seen two ways of inserting token. Which is the best? What are the differences? Both work the same? What will happen if I do not insert a token?
{{csrf_field()}}
#csrf
Thank you for your time!
Well, when you do this:
<form action="insert.php" method="POST"> </form>
What you are doing is telling the html markup explicitly the relative path to post the data, in your case index.php
Say, you were on this url in your app: http://myapp.com/some-page and you were click the submit button, what will happen is that it will post the data to this relative url: http://myapp.com/some-page/index.php
Now, the reason why we use the URL helper facade is to make urls relative to the application's url.
For example, if you've defined in your application config (or .env) that the APP_URL is equal to something like http://myapp.com then when you use this: URL::to('/insert') it will output the following url: http://myapp.com/insert - regardless of which url you are on.
Hope this makes sense. As for this:
{{csrf_field()}}
#csrf
I believe they achieve the same, they generate an hidden input field with your current csrf token in it.

Glassfish how to prevent direct access to a html file

I am running glassfish 3.1
Say I have 2 html files: a.html b.html and their urls are:
http://localhost:8080/mysite/a.html
http://localhost:8080/mysite/b.html
I only want a user to click a link from within a.html to get to b.html. if a user try to directly access b.html using http://localhost:8080/mysite/b.html, he should be redirected back to a.html.
How do i achieve this? I read something about .htaccess file. Does glassfish support this where should I find this file and modify it to suit my needs as stated above?
when user clicks link on a.html to go to b.html, save a flag in database(using javascript for click -> database for save ). On loading b.html, first check the flag(database for retrieval -> javascript to check), if untrue, go back to a.html, else show b.html.
Depending on how your users would move from page a to b. What you could do is instead of clicking a link going to page-b have a button.
Then that button can be part of a form with a hidden value... and check on page-b if that value was posted if not then redirect to page-a for example.
page-a:
<form action="b.php" method="post">
<input type="submit" name="a_submit" value="Go to page b">
</form>
top of page-b:
<?php
if(isset($_POST['a_submit'])){
///they came from page a
}else{
header('Location:a.html');
}
?>
With these two pieces of code you can implement exactly what you wanted. The only catch is you have to rename your b.html to b.php this is because only php files can handle php scripts. But do not worry there is no down side of using a php file instead of html; they act exactly the same but they only also allow php scripts to be ran.

Passing HTML form data in the URL on local machine (file://)

I'm building a small HTML/JS application for primary use on local machine (i.e. everything is accessed via file:// protocol, though maybe in the future it will be hosted on a server within intranet).
I'm trying to make a form with method="get" and action="target.html", in the hope that the browser will put form data in the URL (like, file://<path>/target.html?param1=aaa&param2=bbb). However, it's not happening (target.html opens fine, but no parameters is passed).
What am I doing wrong? Is it possible to use forms over file:// at all? I can always build the url manually (via JS), but being lazy I'd prefer the browser do it for me. ;)
Here is my sample form:
<form name='config' action="test_form.html" method="get" enctype="application/x-www-form-urlencoded">
<input type="text" name="param1">
<input type="text" name="param2">
<input type="submit" value="Go">
</form>
It might be some browser specific restriction. What browser are you using?
I tested this in Firefox 3.6.3 and Internet Explorer 8, and it works just fine.
Ok, that was stupid. The controls of my form are generated dynamically (via JS), and the generation function was setting ids for them, but not names. So, from the form's point of view, there was no parameters at all.
Thanks to Guffa for providing me a nudge in the right direction!