I have a small PHP application (MVC) with a form. The action looks something like this:
<form action="<?php sprintf("/v/.../update?job=%s", $job->id);?>" method="post">
Here $job is a PHP object I pass the view from the controller. I hold onto the id field (an integer) so that I can update the row in the database corresponding to the object.
I'm not seeing this value in $_POST (it is in $GET, though) when I step into my update function for a post request. How should I retrieve this value? Is this expected?
Try to get by $_GET['job'] or remove 'job' from action url and send in hidden text field to get as $_POST['job'].
Also I think your action url is not creating. use echo
<form action="<?php echo sprintf("/v/.../update?job=%s", $job->id);?>" method="post">
you pass your job param as url string, and in this case it can be seen in $_GET or $_REQUEST arraies not $_POST whatever your form action get or post, because this param isn't form input.
Related
So I have a form in my view:
{{Form::file('projectPicture', ['class' => 'uploadedImage', 'data-some-attribute' => ''])}}
with the attribute data-some-attribute.
And in my route I retrieve it like so:
$request->file('projectPicture');
How do I get a data-some-attribute in the route? Is it even possible?
I know I can use ajax to pass any data, but can it be avoided in this case?
Thank you!
It is not possible how you intend it to work, only because it's not how form data is working under the hood. The second argument in your sample Form::file is just decorating the rendered form element. It has no correlation with the form data that is transferred between the server and client.
For all intents and purposes form data is just a glorified set of key value pairs. If you wanted to pass some-data-attribute to your route controller, you have two options -
Add another form field, and make it empty using Form::hidden. In this case, you would just name the field some-data-attribute.
If your form is submitted through a POST method, you can tack on some-data-attribute onto the form's route and retrieve it from the request.
ie - your/route becomes your/route?some-data-attribute=whatever, and you can retrieve it later with something like $request->input('some-data-attribute').
Hello I have the following form that collects data entered and later I output it. It works just fine when I use POST but when I use REQUEST like the teacher said to do, the echo $word comes back empty. Any ideas guys? please?
<Form name ="form1" Method ="REQUEST" Action ="">
<Input Type = "text" Value ="<?php echo $word ?>" Name ="word">
<Input Type = "Submit" Name = "Submit1" Value = "Submit">
<?php
if (isset($_POST['Submit1'])) {
$word = $_POST['word'];
$book = $_POST['book'];
}
?>
There is no method called REQUEST on a Form. It should be either GET or POST
Maybe your teacher is confused with the $_REQUEST in PHP.
I think you are looking for GET, not REQUEST.
GET will include the contents of the form submission in the URL itself, so it's suitable for things that should be able to be bookmarked, like search form submissions.
Here's more: http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
Not sure why your teacher asked you this, but "REQUEST" is not a standard HTTP method so I don't think there's any shortcut in PHP to retrieve the data. I found that even using PATCH sometime causes problems.
What you could try is to read the raw data directly using:
file_get_contents("php://input")
There is NO method named REQUEST. You can use only two methods : POST and GET.
If you are using POST as method, you can get the values using only POST OR REQUEST.
If you are using GET as method, you can get the values using only GET OR REQUEST.
For more information please refer to this page: http://www.w3schools.com/tags/ref_httpmethods.asp
I have used this page http://www.binarytides.com/blog/php-redirect-go-back-to-previous-page/
to go back
but from
http://page.co/test.php?item=26
I post something to post.php and then call the php Go back function but I go back to
http://page.co/test.php
losing the argument path, any idea?
In Your form fill in the query string to the action attribute, like this:
<form action="?item=26" name="myform">
...
</form>
and after the submission Your HTTP_REFERER will contain this query string so redirect to it will be successfull...
EDIT: If the form is on the page post.php, it is enough to use action="?item=26" - of course You can and should use PHP to write down the number/ID of item from whenever it may come...
Lets say Your item ID is stored in the variable $item_id - then Your action will look like this: action="?item=<?php echo $item_id; ?>".
You should use sessions for stuff like this. Set a session when posting the data and you're set.
What I have is that when someone clicks a link on my index it calls index.php?action=signup and that will display a signup form.
How do I then cause the signup form to maintain the action=signup portion of the query string so that when the user clicks "Submit" the new query string is index.php?action=signup&email=user#email.com?
What is the best way to set this up? Should I somehow split the actions into GET and the data into POST?
I am not sure if I am describing this correctly but I want to keep all my functions on the index.php so when I integrate .htaccess everything will be like www.site.com/signup, www.site.com/login and so on.
The best way to do this on a form submission is to change the <form> action to the correct URL and the method to GET, like so:
<form action="index.php?action=signup" method="GET">
Then, hidden in your form, you put something like the following:
<input type="hidden" name="action" value="signup" />
Then your URL will contain the correct parameters when it is submitted.
However, if you need to modify this server side, then you can get all of your query parameters via $_SERVER['QUERY_STRING']. Use parse_str to get an array of values, and use http_build_query to recreate the query string:
Example:
$params = array();
parse_str( $_SERVER['QUERY_STRING'], $params);
$params['email'] = 'user#email.com';
echo http_build_query( $params); // Will output a new query string for your URLs
I have a form that sends stuff like names and email and a message and I get it with $_POST['etc']; - But, I also want to send action=someaction as a part of the url, but I don't want to have any hidden form fields.
Can this be done?
Thanks!
Yep, just add it to your url: <form action="url.php?action=someaction" method="post">. You can retrieve them in your php script using $_GET (in this case, $_GET['action']).
action=someaction cannot be read by $_POST['whatever'] because it's submitted in a GET-Request. You can access GET and POST variables by using $_REQUEST instead of $_GET and $_POST.
To form the request, follow the answer of Spiny Norman.
If I understand you correctly, you simply need to add ?action=someaction to the action attribute of your form tag. These would be fetched from PHP by using $_GET instead of $_POST.
Example:
<form action="http://example.com?action=someaction">...</form>
and to get the value:
$action = $_GET['action'];