Imagine I have the following in my routes.php file:
Route::get('/test/{system}/{link}', function($system,$link)
{
return "test ok";
});
Then, I wanna access this route with a form from my view:
<form action="/" method="get">
<input type="text" name="system">
<input type="text" name="link">
<button type="submit">Go!</button>
<form>
What would be the best way to do that? I know I can do something like:
Route::get('/',function(){
$input = Request::only('link','system');
$url = 'test/'.$input['system'].'/'.$input['link'];
return redirect($url);
});
But I'm not sure if it's the correct way to achieve it.
Thanks in advance!
You could have js change the form action and then submit it. Your way is ok, but make sure to put it at the end of the routes file. I would actually have the form submit to some specific route (instead of / maybe /redirect-to or something) and from there redirect.
You can do this. I'm not sure if this is the best process.
In routes.php, do this:
Route::get('/form', function(){
return View::make('formview');
});
Route::get('/formaction', function(){
$link = Request::input('link');
$system = Request::input('system');
return "$link";
});
And in the view, as usual, the following:
<form method="get" action="formaction">
System: <input type="text" name="system" />
Link: <input type="text" name="link" />
<input type="submit" value="ok" />
</form>
The Request of Laravel does not depend on the verb used for the request.
Related
Are there other ways to get value of an <input> in laravel besides Input::get('name'); ?
Here is my route that tries and get the value
Route::get('delete_comment_action/{id}', function($id)/
{
$status_Id = Input::get('status_Id');
print_r($status_Id);
exit();
return Redirect::back();
});
here is the form that should have the data in it
<form action="" method="get">
<input type="hidden" name ="status_Id" value="{{$swagger->status_Id}}">
<button type="button" class="btn btn-danger">Delete</button>
</form>
Status_Id should at least equal 1, when i try using, but instead it just displays a blank page.
$variable = Input::get('status_Id');
print_r($variable);
your routes looks okay but change form submit tag instead link
Route::get('delete_comment_action/{id}', function($id){
$status_Id = Input::get('status_Id');
print_r($status_Id);
exit();
return Redirect::back();
});
In Form view change form action and replace anchor tag link with
submit button
<form action="{{{ url("delete_comment_action/$swagger->Id") }}}" method="get">
<input type="hidden" name ="status_Id" value="{{$swagger->status_Id}}">
<input type="submit" value="Delete">
</form>
You are trying to use same route twice. You can't use it. What you have to do is separate routes like the following
This route is to show view
Route::get('test', function() {
return View::make('example');
});
This route will handle when you submit your form
Route::get('newtest', function() {
dd(Input::all());
});
In your example.blade.php
<form action="" method="get">
<input type="text" name="hello">
<input type="submit" value="Submit">
</form>
I'm currently tutoring myself on Laravel 4 and I encounter a strange problem:
I have a blade form:
<form action="{{url('/')}}" method="POST">
<input type="hidden" name="foo" value="bar"/>
<input type="hidden" name="baz" value="boo"/>
<input type="submit" value="Send"/>
</form>
and a Route:
Route::any('/', function()
{
$data = Input::all();
var_dump($data);
});
Route::get('post-form',function()
{
return View::make('form');
});
If I use Get instead of Post (in the blade form) everything's running perfectly but when I try to use Post the returned array is empty
Any clues ? :)
(forgive my English since i'm french native)
try to use Route::post instead of Route::any and dd instead of var_dump
Route::post('/', function()
{
$data = Input::all();
dd($data);
});
Edit:
you have a little mistake in your html form:
<form action="{{url('/')}}" method="POST">
it should look like this:
<form action="/" method="POST">
I hope it will work this time
I tried your code out and it works perfectly. There is no problem with it.
I suggest you using Route::post when you are sure sending POST data
So i want to use the GET method and POST method on the same form. The GET to send the details from the form to the url bar and the post for a isset if statement to check if the form has been submitted. I would like to be able to do this. But if you can find another way of doing it please tell me
HTML
<form method="post">
<p>Username:</p><input type="text" name="Username">
<p>Password:</p><input type="password" name="Password">
<p>Post:</p><input type="text" name="Post" value=""><br><input type="submit"
value="submit" name="submited">
</form>
PHP
if (isset($_POST['submited'])){
$Username=$_GET["Username"];
$Password=$_GET["Password"];
$Post=$_GET["Post"];
$Password=md5($Password);
if(blah=blah){
echo "blah";
}
}
Change
isset($_POST['submited'])
to
isset($_GET['submited'])
But it is a really bad idea to send password using GET.
Kinda bad practice, but you could force it by sending parameters in URL:
Setting like action="index.php?data=123" should do the work:
<form method="post" action="index.php?name=a&surname=b"> //Here we go
<p>Username:</p><input type="text" name="Username">
<p>Password:</p><input type="password" name="Password">
<p>Post:</p><input type="text" name="Post" value="">
<br>
<input type="submit" value="submit" name="submited">
</form>
in your form you can change it's action -- so action="?var1=something&var2=example"
It could be done with javascript; example:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('form').submit(function() {
$('form').attr('action','?Username=' + $('input[name=Username]').val() + '&Password=' + $('input[name=Password]').val() + '&Post=' + $('input[name=Post]').val());
return true;
});
});
</script>
</head>
<body>
<form method="post">
<p>Username:</p><input type="text" name="Username">
<p>Password:</p><input type="password" name="Password">
<p>Post:</p><input type="text" name="Post" value=""><br><input type="submit"
value="submit" name="submited">
</form>
</body>
</html>
Personally I would POST the form to say my formprocess.php page, placing your
if(isset($_POST['submitted'])) {
//Code goes here, do checks/validation etc
}
Then once you've done what you needed to do with your code do a header (or meta redirect if you have already sent your headers) like so:
header('Location:http://www.mysite.com/index.php?value=formsubmitted&action=success');
So the value= and the action= could just be the values you want to pass back in the URL. You could also add some RewriteRule 's to your .htaccess to make these redirected URL's a bit prettier and better for SEO etc. Also if you do go down this route, make sure to set/define the redirection status i.e. 301 see below:
header("HTTP/1.1 301 Moved Permanently");
I think what everyone is wondering is why anyone would need to use $_GET if they are using $_POST.
i.e.:
if (isset($_POST['submited'])){
$Username=$_POST["Username"];
$Password=$_POST["Password"];
$Post=$_POST["Post"];
$Password=md5($Password);
if(blah=blah){
echo "blah";
}
}
... and if you want to check your parameters while developing just stick in...
print_r($_POST)
Is there any reason why you need to retrieve your form field data from a $_GET?
When i click my login button, it just reloads the page for some reason. it should alert the string i echo from my php page.
This is my login.js code:
$(document).ready(function(){
$('#login').click(function(){
$('#msgLoginStatus').show();
$('#msgLoginStatus').html("processing...");
$.post('login.php',{username:"bob",password:"pass"}, function(data){
alert(data);
});
});
});
my login.php:
<?php
echo "message";
?>
and my form:
<form id="loginForm" action="" method="post">
<fieldset id="body">
<fieldset>
<label for="username">Username</label>
<input type="text" name="username" id="username" />
</fieldset>
<fieldset>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</fieldset>
<button id="login">login</button>
<label for="checkbox"><input type="checkbox" id="checkbox" />Remember me</label>
<br />
<p id="msgLoginStatus" style="display:none"></p>
</fieldset>
<span>Forgot your password?</span>
</form>
There are no errors in browser console. I tried this also using $.ajax, it returned an error, i tried putting the error variable in an alert, but when it alerted, it was an empty string. Anyone have an idea whats wrong?
Your login button has an ambiguous action - add type="submit" like this:
<button id="login" type="submit">Login</button>
Now if you really want to execute an explicit POST with JavaScript, call e.preventDefault so the browser's automatic "submit" action will be suppressed.
e.preventDefault();
$.post(...);
But it will probably be better to let the form submit itself. To do this specify the correct action="login.php" attribute in the form:
<form id="loginForm" action="/login.php" method="post">
Keep your existing "click" handler on the login button, just remove the "$.post" part and let the browser handle the posting. You'll still get the nice "processing..." text.
Even better, handle the "submit" event on the form instead of the "click" event on the button:
$('#loginForm').submit(function(e) {
$('#msgLoginStatus').show();
$('#msgLoginStatus').html("processing...");
});
This way you'll get the nice updates whether the user submits the form using the button or by pressing "enter" on the keyboard.
Try:
$('#login').click(function(e){
e.preventDefault();
$('#msgLoginStatus').show();
$('#msgLoginStatus').html("processing...");
$.post('login.php',{username:"bob",password:"pass"}, function(data){
alert(data);
});
});
That prevents a "normal" submit from happening (which, I take, is why you are not getting any errors).
See http://api.jquery.com/event.preventDefault/
Add e.preventDefault(); to the clck handler (and grab the event object in the handler as e).
Or you can Just set the button type = 'Button' and not Submit. THis will also run your code
<button id="login" type="button">Login</button>
In this way you don't have to halt the browser's event
I have a simple form for a mailing list that I found at http://www.notonebit.com/projects/mailing-list/
The problem is when I click submit all I want it to do is display a message under the current form saying "Thanks for subscribing" without any redirect. Instead, it directs me to a completely new page.
<form method="POST" action="mlml/process.php">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="submit" value="" id="submit"name="submit" >
</form>
You will need AJAX to post the data to your server. The best solution is to implement the regular posting, so that will at least work. Then, you can hook into that using Javascript. That way, posting will work (with a refresh) when someone doesn't have Javascript.
If found a good article on posting forms with AJAX using JQuery .
In addition, you can choose to post the data to the same url. The JQuery library will add the HTTP_X_REQUESTED_WITH header, of which you can check the value in your server side script. That will allow you to post to the same url but return a different value (entire page, or just a specific response, depending on being an AJAX request or not).
So you can actually get the url from your form and won't need to code it in your Javascript too. That allows you to write a more maintanable script, and may even lead to a generic form handling method that you can reuse for all forms you want to post using Ajax.
Quite simple with jQuery:
<form id="mail_subscribe">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="hidden" name="action" value="subscribe" />
<input type="submit" value="" id="submit"name="submit" >
</form>
<p style="display: none;" id="notification">Thank You!</p>
<script>
$('#mail_subscribe').submit(function() {
var post_data = $('#mail_subscribe').serialize();
$.post('mlml/process.php', post_data, function(data) {
$('#notification').show();
});
});
</script>
and in your process.php:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'subscribe' :
$email_address = $_POST['address'];
//do some db stuff...
//if you echo out something, it will be available in the data-argument of the
//ajax-post-callback-function and can be displayed on the html-site
break;
}
}
?>
It redirects to a different page because of your action attribute.
Try:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="address" id="email" maxlength="30" size="23" />
<input type="submit" value="" id="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) : ?>
<p>Thank you for subscribing!</p>
<?php endif; ?>
The page will show your "Thank You" message after the user clicks your submit button.
Also, since I don't know the name of the page your code is on, I inserted a superglobal variable that will insert the the filename of the currently executing script, relative to the document root. So, this page will submit to itself.
You have to use AJAX. But that requires JavaScript to be active at the users Brwoser.
In my opinion it's the only way to do without redirect.
to send a form request without redirecting is impossible in php but there is a way you can work around it.
<form method="post" action="http://yoururl.com/recv.php" target="_self">
<input type="text" name="somedata" id="somedata" />
<input type="submit" name="submit" value="Submit!" />
</form>
then for the php page its sending to have it do something but DO NOT echo back a result, instead simply redirect using
header( 'Location: http://yourotherurl.com/formpage' );
if you want it to send back a success message simply do
$success = "true";
header( 'Location: http://yourotherurl.com/formpage?success='.$success);
and on the formpage add
$success = $_GET['success'];
if($success == "true"){ echo 'Your success message'; } else { echo
'Your failure message';
Return and print the contents of another page on the current page.
index.php
<html>
<body>
<p>index.php</p>
<form name="form1" method="post" action="">
Name: <input type="text" name="search">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_POST['search'];
include 'test.php';
}
?>
</body>
</html>
test.php
<?php
echo 'test.php <br/>';
echo 'data posted is: ' . $_POST['search'];
?>
Result:
Just an idea that might work for you assuming you have no control over the page you are posting to:
Create your own "proxy php target" for action and then reply with the message you want. The data that was posted to your php file can then be forwarded with http_post_data (Perform POST request with pre-encoded data). You might need to parse it a bit.
ENGLISH Version
It seems that no one has solved this problem without javascript or ajax
You can also do the following.
Save a php file with the functions and then send them to the index of your page
Example
INDEX.PHP
<div>
<?php include 'tools/edit.php';?>
<form method="post">
<input type="submit" name="disable" value="Disable" />
<input type="submit" name="enable" value="Enable" />
</form>
</div>
Tools.php (It can be any name, note that it is kept in a folder lame tools)
<?php
if(isset($_POST['enable'])) {
echo "Enable";
} else {
}
if(isset($_POST['disable'])) {
echo "Disable";
} else {
}
?>
Use
form onsubmit="takeActions();return false;"
function takeAction(){
var value1 = document.getElementById('name').innerHTML;
// make an AJAX call and send all the values to it
// Once , you are done with AJAX, time to say Thanks :)
document.getElementById('reqDiv').innerHTML = "Thank You for subscribing";
}