How to define correct action parameter on forms? - php

Right now I have those.
user_add.blade.php
<form method="post" id="customForm" action="admin/user_add">
...
</form>
routes.php
Route::post('admin/user_add', 'admin#user_add');
admin.php
public function action_user_add()
{
print_r(Input::get());
}
How should I give the correct action path? Like; admin/user_add or something like {{ Base_path }}admin/user_add? I don't want issues when I move into production server. (e.g using subfolders etc.)
Also, is my approach correct or are there better ways?
Ps. Should I use Form methods? Like <input type="text" id="username" name="username" class="small"> to Form:text("username", "", "small");

Try this:
<form method="post" id="customForm" action="{{ URL::to_action('admin#user_add') }}">
Whether you want to use the form helpers or not, is really your choice. They make some things simpler, for example declaring the target URL, but they're a little cumbersome for writing additional HTML like you did (adding an id attribute to the form).

Related

Trying to use PATCH method works with AJAX but not with a regular html form

I'm currently trying out http://altorouter.com/ and it's working well for me so far, except for this one issue I'm having
My route is set up like this:
$router->map( 'PATCH', '/admin/pages', 'pageController#update');
If I use the following jquery, the route works perfectly:
$.ajax({
type: "PATCH",
url: "/admin/pages",
data: {page_items:page_items, page_name: 'test_page'},
success: function(returned_data)
{
console.log(returned_data);
}
});
However, no matter what I put in my HTML I can't get a regular form to submit in a way it accepts as PATCH:
<form action="/admin/pages" method="post">
<input type="hidden" name="form_function" value="edit_theme">
<input type="hidden" name="_METHOD" value="PATCH">
<button type="submit">Save Page</button>
</button>
I've tried "_METHOD", "_method", "method" etc. None of them work.
I've also tried
method="PATCH"
but that only causes it to do a GET.
When I echo the $_SERVER['REQUEST_METHOD'] on the target page I get "PATCH" for the ajax, but just "POST" for the form. Hope someone can help.
In short, you cannot.
As you'll see in the W3 Spec
The only valid methods for HTML based forms are "GET" and "POST".
However you can work around this if you wish, on the server side instead. Theres a great article about how Laravel does it here: Theres no Put/Patch Delete Methods
A quick snippet of code from that article:
<form method="POST" action="" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PUT">
</form>
<form method="POST" action="" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PUT">
</form>
If you are not using Laravel and want to build a form manually, you cannot use PUT/PATCH – there’s just no such methods supported by forms in browsers – it’s only GET and POST. So how does Laravel make it work with {{ Form::create([‘method’ => ‘PUT’]) }}?
Actually, under the hood the generated HTML looks like this:
That’s right, Laravel constructs a hidden field with name _method and
then checks it upon form submittion, routing it to the correct
Controller method.
So if for any reason you would need to build the FORM tag yourself,
don’t put (same applied to patch and delete) – it
just won’t work. Instead add hidden fields, if necessary.
So back to your issue, Altorouter. It appears their documentation is rather lacing the best guide I can find for you is here https://recalll.co/app/?q=rest%20-%20PHP%20detecting%20request%20type%20(GET%2C%20POST%2C%20PUT%20or%20DELETE)%20-%20Stack%20Overflow it might be worth your while finding a better router, as Alto doesn't seem to have been updated in around 3 years.
Managed to find a working solution after digging around in the code. Altorouter's match method actually accepts a method parameter, which doesn't seem to be documented anywhere.
Where I used to have
$match = $router->match();
I now have:
if(isset($_POST['_method']))
{
$match = $router->match(null, $_POST['_method']);
}
else
{
$match = $router->match();
}

Is it possible to declare a Global <input> variable that gets used for all forms?

I am wondering if there is a way to declare a Global HTML- tag, that gets submittet with each form that gets called.
I mean something in the way like this:
<input type="hidden" name="x" value="200">
<form action="test.php" method="POST" enctype="multipart/form-data">
#Some Form-Stuff
</form>
<form action="test.php" method="POST" enctype="multipart/form-data">
#Some more Form-Stuff
</form>
And in both of these Forms, when a Submit is being made, there is supposed to be the Global hidden Input-Tag that gets delivered with the rest.
I already tried it and it didnt work, but I wonder if there is just something that is missing or if there is a similar or better way to accomplish this.
Would appreciate your help.
EDIT:
What I want in my current Project is to let the User <SELECT> an option that represents like an Folder in which there will be displayed data that is inside that folder.
I dont want the User to have to Select the Folder again after each click and the folder has to be submitted every time to the php-script so that it can handle data accordingly to the folder selected.
My Question in this case however still remains on wether you can or cannot make a <input> tag global or not, if it´s permanent or not doesnt matter.
Either include the following in your form
<input type='hidden'>
It is great for passing values without much code.
Or set it as a cookie/session variable as required.

search url manager in Yii

I implemented my project in Yii. i done URL management its working fine. but i search URL is not being proper. i added my code here. please tell me how to change this code according to my URL..
my main.php:
<form class="navbar-form " role="search" name="searchform" method="POST" action="<?php echo Yii::app()->baseUrl.'/index.php/recipe/course1/';?>" id="menu-form-style" onsubmit="return ValidateSearchForm();">
<div class="form-group">
<input type="text" name="search" id="search" placeholder="Quick Search...." class="form-control" onkeyup="lookup(this.value);" onblur="fill();">
i sent to my recipe controller.
where i did like this: recipe controller
public function actionCourse1(){
if(isset($_POST["search"]))
$course=$_POST["search"];
$this->redirect(array('recipe/course','searched'=>$course));}
public function actionCourse(){
$model=new Recipe;
if(isset($_GET['searched']))
$count=$_GET['searched'];
$bigArray=array();
in my configure main.php
'searched'=>'recipe/course',
above config line i wants to be show.
but am getting like this
search/searched/asdfadfadfa
http://kitchenking.ebhasin.com/
this our website. please suggest me suitable answer
if i understod your question true try this...
$this->render('/recipe/course/ or your file place', array('searched'=>$course));}
or
$this->redirect('/recipe/course/searched/'.$course);
Re-read http://www.yiiframework.com/doc/guide/1.1/en/topics.url this one, yes I know that is not an answer, but I think your routes is invalid.
change your config main.php:
'search/<searched>'=>'recipe/course',

Is it possible to work GET and post option together in form?

I have a form in my site. I wanna add GET and post option together. Which will indicate 2 different destination.
Example : -
when someone submit a form ( name and address ) then he can enter a restricted section.
Then code will be
<form name="loginform"
action="http://site.com/viparea"
method="post">
And beside this i wanna keep a log.And for the log code is
<form name="loginform"
action="logcode.php"
method="GET">
And encrypted log will be save in a text file.
I have used two methods individually . And those are working fine individually.
But i wanna make them work together.
So, i am not a coder but after searching something i just did a simple work like a noob :P .
<form name="loginform"
action="http://site.com/viparea"
method="post"><form name="loginform"
action="logcode.php"
method="GET">
But not working. Any suggestion please.
You can only achieve this by using:
<form name="loginform" action="http://site.com/viparea?var1=var&var2=var" method="post">
<input type="text" name="var3" />
</form>
this way you will get var1 and var2 as $_GET, var3 as $_POST OR all 3 vars as $_REQUEST
No, you can't; but in php you can use $_REQUEST[], to see data from both GET and POST; you may need just a little changes in your php code.

Access method directly from URL

I've watched video tutorial for a little MVC framework(if i can call it framework) - http://net.tutsplus.com/tutorials/php/create-your-first-tiny-mvc-boilerplate-with-php/
,i download the source code and changed a little bit the files but I can't figure out how to call a specific method as CodeIgniter's form_open() and make a simple login system.Thanks in advance!
So ,how to send POST data from a form to the controller?
PS:I don't want to reinvent the wheel, just practicing
I also used this TinyMVC to learn MVC structure,
On your HMTL form:
<form method="POST" action="yoururl.com/index.php">
<input type="text" name="username" id="username"/> // name or ID.
<input type="submit" value="submit"/>
</form>
Then in your controller or method just use
$username = $_POST('username');
You have to post to index.php, and it will go to your controller for you to pick up with there. You can also pick it up in a method using the same example.

Categories