Laravel best way to form an HTML form and token - html

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.

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!

Standard Web Form - POST method not working when adding hidden input name _token ( Laravel 5.5 + VueJS 2X )

I searched so much and I tried so many things but i didn't get solution.
I hope can given answer and find solution for this issue .
Im using Laravel 5.5 and Vuejs 2.
I have ordinary web form trying top send POST data .
But my form not sending POST method when Im adding there CSRF token .
My raw codes samples here...
<form action="http://192.168.1.100/login" method="POST">
<input name="_token" type="hidden" v-model="csrfToken">
<button type="submit"> SUBMIT </button>
</form>
it's looking on browser...
<form data-v-1b610de5="" action="http://192.168.1.100/login" method="POST"><input data-v-1b610de5="" name="_token" type="hidden" value="3jQ5KJV9TiCcTXF0fICpXYgX0C1irPpdHLkZWiAg"> <button data-v-1b610de5="" type="submit"> SUBMIT </button></form>
Actually all is looks like OKAY...
I tried all all all things..
There is a problem name of hidden input or value ....
When Im changing value any for example writing "Chuk Norris" sending POST data well.
But real CSRF data - not sending POST . Refreshing self .. Not going to action address .
I tried this on CHROME and MOZILLA too.
Is there any idea or solution for this ? Why not sending POST data ?
GET is sending.... But I need POST ...
Thank you !!!
I found problem and solution...
But interesting...
I used always my local IP for testin, when i used domain address ( I add fake domain on hosts file ) then, tried , sending POST data ...
All is okay now , thank you !!!
actually there was a different problem... not IP or URL ..
let csrf_token = document.head.querySelector('meta[name="csrf-token"]');
window.csrf = csrf_token.content
i get CSRF data like this on HTML . I don't know what happening there...
Then i try to store directly local storage csrf then get from local.Storage and then worked well ....
have a nice day !

How do you post to a secure service from unsecure page without using fully qualified domain name?

I would like to post have a form that sits on a non secure page without using the fully qualified domain name.
http://www.domain.com/page.aspx
post to a secure service on the same domain
https://www.domain.com/service.aspx
I am currently doing this which works.
<form action="https://www.domain.com/service.aspx" id="formId" method="POST">
The main issue is that we have qa versions of the site
http://qa.domain.com/page.aspx
https://qa.domain.com/service.aspx
the form here looks like
<form action="https://qa.domain.com/service.aspx" id="formId" method="POST">
and there is some publishing issues because when we publish from qa to prod we have to manually update the domain name.
What I'd like to do is somehow point to
<form action="/service.aspx" id="formId" method="POST">
but make the form use the prefix https. We can dynamically write the URL in the form, but I was looking for a way to do it with HTML.
Thanks
You can't do this with URL syntax. You must always restate anything to the right of the component of a URL that you change when constructing relative URLs.
You could generate the URLs programmatically (preferably with server side code).
Note, that as per my comment on the question, you shouldn't do this.

Need jsp form to submit query to specific path

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!

Rails 3, make submit call specific function in controller

new to web development, here. I have a form like this:
<form name="myForm" id="myForm" method="post" >
<select id="id" name="foo">
...some stuff
</select>
<input type="submit"/>
</form>
The submit button calls the 'index' method of my controller, as expected. I would like to make it call some other function, such as 'update', how do I do that? I need to do something with the #_params hash, but I don't want invoke the index function, to do it. Thanks.
You did not include the view logic so I am going to take a stab in the dark and guess you are writing the HTML for the form rather than using the RoR helpers.
In rails there are a set of helpers that help you generate forms and form items.
Please check the docs for form_for
Using form_for will follow the basic restful routing unless you modify the url parameter. So if you are on /new you will be routed to /create, if you are on /edit, you will be routed to /update. More precisely, if the object is new, you will submit to create, if the model exists you will submit to update.
If your form doesn't use a model, you can use the form_tag helper that takes a url parameter and you can pass a string specifying what path to submit to.
If you just need to know how to do this in plain HTML, read this. Essentially, you need to include an action attribute on form that specifies the path to the action you want to post to.