I want to submit the form to usercontroller file and call function invitebyemail.
When I click on submit it gives me error like "Whoops, looks like something went wrong."
Below are the structure of my Files and code.
Route File:
Route::POST('event/inviteforevent', 'UserController#invitebyemail');
Blade.php file
<form action="{{ route('inviteforevent') }}" method="POST">
<input type="email" class="form-control full-width" id="add-members-event-email" name="email" placeholder="Email address">
<input type="hidden" class="form-control full-width" name="eventid" value="{{ $event->id }}" >
<input type="submit" name="invite" value="submit" />
</form>
UserController File:
public function invitebyemail(Request $request){
$event = Event::find($request->eventid);
$timelineId = $event->timeline_id;
$username = Auth::user()->username;
$timelines = Timeline::find($timelineId);
return redirect()->back()->with('message', 'Invitation send to user by email.');
}
The route() helper function expects a route name.
Route::post('event/inviteforevent', 'UserController#invitebyemail')->name('inviteforevent');
Edit
Run php artisan serve and access your server at http://localhost:8000. You have not set the web root and laravel url rewrite won't work properly along with the url and route methods generating wrong links.
Apart from the Route change that was suggested, you may need to add the
{{ csrf_field() }}
after
<form action="{{ route('inviteforevent') }}" method="POST">
Read more here: https://laravel.com/docs/5.4/csrf#csrf-introduction
change
Route::POST('event/inviteforevent', 'UserController#invitebyemail');
to
Route::POST('event/inviteforevent', 'UserController#invitebyemail')->name("inviteforevent");
Reference : https://laravel.com/docs/5.4/routing#named-routes
Please replace this code in route:-
Route::post('event/inviteforevent', 'UserController#invitebyemail')->name('inviteforevent');
OR
Route::post('event/inviteforevent', ['as' => 'inviteforevent', uses' => 'UserController#invitebyemail']);
You have to add {{ csrf_field() }} which was suggested but one question? Where is your table names and id in your form opening? How that form have to know which table you want to add data in?
You have to add $events and $events->id into your form
Related
I have one view displaying a form. Now, depending on the button the user chooses, I want to have the data processed in a different way. These "different ways" correspond with different views that I want the POST request go to.
Please help me building a form with multiple buttons leading to different urls/views.
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Process form with view ONE">
<input type="submit" value="Process form with view TWO">
<input type="submit" value="Process form with view THREE">
</form>
My problem here is that the action attribute of the form tag defines where this POST request is going to. How can I change that target url via multiple buttons?
I know I could also handle this logic on the server-side. But the question is: Do I have to? If not, please show me the way
you can use ajax with onclick attribute. post_url as an argument like this
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Process form with view ONE" onclick='testFun(url_one)'>
<input type="submit" value="Process form with view TWO" onclick='testFun(url_two)'>
<input type="submit" value="Process form with view THREE" onclick='testFun(url_three)'>
</form>
To handle this on the frontend without even using ajax, you can use JavaScript to change the action URL of the form based on which button is clicked before submitting the form.
Slight change on your HTML code
<form method="post" id="my-form">
{% csrf_token %}
{{ form }}
<input type="submit" id="view1" value="Process form with view ONE">
<input type="submit" id="view2" value="Process form with view TWO">
<input type="submit" id="view3" value="Process form with view THREE">
</form>
JavaScript for handling the redirection of the form based the button clicked
const form = document.getElementById('my-form');
const button1 = document.getElementById('view1');
const button2 = document.getElementById('view2');
const button3 = document.getElementById('view3');
button1.addEventListener('click', () => {
form.action = '/view1/';
});
button2.addEventListener('click', () => {
form.action = '/view2/';
});
button3.addEventListener('click', () => {
form.action = '/view3/';
});
// Submit the form
form.submit();
Note: You can use jQuery for better code optimization
using this for django model form, we cant create beautiful front end.
<form method="POST" action="">
{% csrf_token %}
<div class="esor">
Name: {{form.Name}}<br>
Class: {{form.Class}}<br>
Publisher: {{form.Publisher}}<br>
</div>
<input type="submit" value="submit">
</form>
Isn't there any ways so we can use html code like:
<form method="GET" action="#">
<input type="text" name="name" placeholder="Name of book">
</form>
instead of {{form.Name}} or {{form.as_p}}
Yes, you can get a beautiful interface that way, just pass the name you use in the form.py
Example:
forms.py
class NameForm(forms.ModelForm):
class Meta:
model = MyModel
fields = [
"whatever"
]
Html Template
<form method="POST">{% csrf_token %}
<input type="text" name="whatever" placeholder="Write whatever">
</form>
There are two approach I normally use:
Install widget tweaks, so you can customize your input easily:
{% load widget_tweaks %}
{{ form.Name|add_class:"css_class"|attr:"placeholder:Name of book" }}
Or 2, in your forms.py, set all the attributes you want for the field:
Name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Name of book'}))
You can do this by giving the name of your input element same as the name of your form field. And you can get name of your form field by displaying {{ form }} in html and then inspecting the form field in your browser and then the name attribute of that field will be name of the form field.
i am building online payment system for my project and i am new to yii2.
I have a simple html with the address http://localhost/ops/onlineStore/home.php with the following simple code
`
<form method="post" action="http://localhost/ops/frontend/web/index.php?r=site/payment">
<input type="hidden" value="35" name="price">
<input type="hidden" value="Breathney Spears Audio CD" name="product_name">
<input type="hidden" value="someSore" name="Store">
<button type="submit" name="redirect"><br>Breatheny Spears <br/>Audio CD<br/>35 Afs</button>
</form>
`
now i want to get these posted data from my yii2 app please guide me step by step what to do. Thank you
in your SiteController in PaymentAction:
you can simply get the values:
$_POST['price'];
...
or to use yii2 functions:
$post_arr = Yii::$app->request->post();
$price = $post_arr['price'];
...
If your form i realetd to a Yii2 model You can use Yii2 function load ..
$model = new MyModel();
$post = $model->load(Yii::$app->request->post();
then you can refer to each attribute as
$post->my_attribute;
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
http://www.yiiframework.com/doc-2.0/yii-base-model.html
http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html
I want to login on the homepage instead of separate site. When I type my credentials in /login route, everything is okay, but it does not work on the / route.
I render login form with this command in base.html.twig (for testing purposes I want to render login form at each page now):
{{ render(controller("AppBundle:Security:login")) }}
Here is my login.html.twig:
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
<div class="form-group">
<input type="text" class="form-control" id="username" placeholder="Email" name="_username" value="{{ last_username }}" />
<input type="password" class="form-control" id="password" placeholder="Heslo" name="_password" />
</div>
<button type="submit" class="btn btn-success">Přihlásit</button>
Registrovat
</form>
And my loginAction:
/**
* #return array
* #Route("/login", name="login")
* #Template()
*/
public function loginAction()
{
$authenticationUtils = $this->get('security.authentication_utils');
// get error
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return [
'last_username' => $lastUsername,
'error' => $error
];
}
And secured_area in security.yml:
secured_area:
pattern: ^/
anonymous: ~
provider: main
form_login:
login_path: login
check_path: login
logout:
path: /logout
target: /
The login form successfully renders at homepage. But nothing happen when I enter the credentials and click the button. Maybe because the button tries to find a form in action defined for homepage instead of action defined for login page?
Is there an option to "force" the button to use original (login) action instead?
Thank you for your answers.
You should post the login form to the check_path configured in your security.yml, not to the controller displaying your login form.
It is this URL (which is intercepted entirely by the security extension) that will authenticate the user, and if an error occurs, will redirect the user back to the login action.
For detailed instructions on how to set-up form based login, check the dedicated cookbook article in the documentation: http://symfony.com/doc/current/cookbook/security/form_login_setup.html
i am working on a search function, herefore i need the value of an input to generate the final url (which shows the results)
Let's say the user enters the content he is looking for here:
Name: <input type="text" id="myText">
now i need to generate a hyperlink from
http://constant/constant?query=NAME&someotherconstantthings
here, the NAME needs to be replaced from the content of the input
Try to use PHP, you could add a action to the Form Element to post the entered informations to the PHP file, then generate ur hyperlink.
<form action="phpfilename.php" method="post">
Name: <input type="text" id="myText" name="myText">
<input type="submit" value="Search">
</form>
<?php
$name = $_POST['myText'];
$hyperlink = 'http://constant/constant?query='.$name;
?>
You need something like this:
<form action="URL" method="get">
Enter your name here: <input type="text" name="query" value="">
<input type="submit" value="Search">
</form>
You need to replace the keyword URL with the path to the page which performs the search. You can remove the keyword URL if want to submit the form to the same page.