How to put route url in the echo in laravel form inside a controller? - laravel

This is the code inside the controller of my laravel:
echo '
<form action="'.$link.'" method="POST">
';
csrf_token();
echo '
<div class="form-group">
<label for="reason">Total Worked Hours::</label>
<input type="text" class="form-control" disabled="disabled" id="hours" value="'.$totalhours.'"style="color:red;font-weight:bold;"">
</div>
<div class="form-group">
<label for="reason">Enter Your Reason:</label>
<input type="text" class="form-control" id="text">
</div>
<button type="submit" class="btn btn-default">Submit Reason</button>
</form>
';
and the error is 419 page expired what is the proper code for this.

change the line
csrf_token();
by
echo csrf_field();
Or, depending on your ajax, remove it completly.

Related

how to set and get checked or unchecked values in single checkbox using another form

i want to set value and retrieved value in the checkbox field when submitting a value from another form hidden filed.
<form action="store" method="post">
<div class="form-group">
<div class="row">
<label class="control-label col-lg-3"><b>Enable/Disable App</b>
</label>
<label class="Sg-switch">
<input type="checkbox" name="is_active" value="<?php $_POST['enable']; ?>"<?php if (isset($setting['is_active'])==1) { ?> checked="checked" <?php } ?>><span class="Sg-slider round"></span>
</label>
</div>
<div class="clearfix"></div>
<div class="form-group">
<button type="submit" class="sg-main-btn sg-primary-tn">Save</button>
My submit form is:-
<div class="modal-body">
<form class="form-horizontal" method="post" action="#" name="enable">
<div class="modal-footer" style="text-align:center;">
<input type="text" name="is_active" value="1" hidden>
<button type="submit" name="enable" class="sg-main-btn sg-success-btn dis_btn"><i class="fa fa-check"></i><i class="fa fa-spin fa-spinner" style="display: none;"></i>Yes</button>
<i class="fa fa-times"></i>No
</div>
</form>
you can use jquery.
Give your input id like id = "input".
<input type="text" name="is_active" value="1" hidden id = 'input'>
<input type="checkbox" id="input_new" name="is_active" value="<?php $_POST['enable']; ?>"<?php if (isset($setting['is_active'])==1) { ?> checked="checked" <?php } ?>>
<span class="Sg-slider round"></span>
<button id="button1">also add yourclass and type etc</button>
$('#button1').click( function() {
var active = $('#input').val();
if(active == 1){
$('#input_new').attr('checked');
}});

How to check CSRF token is valid or not after custom form submit in codeigniter

I am working on CodeIgniter custom form, In this form, I have used CSRF token after form submit I want to check in controller after form submit if CSRF token is valid or not, can anyone please help me for this issue?
PHP Form code
<form role="form" data-parsley-validate="" novalidate="" class="mb-lg" action="?c=
<?php echo isset($controller) ? $controller : "welcome"; ?>&m=login" method="post">
<div class="form-group has-feedback">
<input name="email" id="exampleInputEmail1" type="email" placeholder="Enter email" autocomplete="off" required class="form-control">
<span class="fa fa-envelope form-control-feedback text-muted"></span>
</div>
<div class="form-group has-feedback">
<input name="password" id="exampleInputPassword1" type="password" placeholder="Password" required class="form-control">
<span class="fa fa-lock form-control-feedback text-muted"></span>
</div>
<div class="clearfix">
<div class="pull-right">
Forgot your password?
</div>
</div>
<?php
$csrf = array(
'name' => $this->security->get_csrf_token_name(),
'hash' => $this->security->get_csrf_hash()
);
?>
<input type="hidden" name="<?php echo $csrf['name'];?>" value="<?php echo $csrf['hash'];?>" />
<button type="submit" class="btn btn-block btn-primary mt-lg">Login</button>
</form>
CodeIgniter does this for you automatically. If it is not valid it will do a show_error with show_error('The action you have requested is not allowed.', 403);
Relevant functions can be found in the /system/core/security class, functions: csrf_verify() and csrf_show_error() (if invalid).
If you have understood by now, if you have csrf enabled in the config and you do not have the appropriate csrf hidden field or use form_open (which adds the field for you) then when you post the request it will fail with the above message. The same goes for AJAX requests - to automate this for ajax requests you can add this to the top of you page wherever there is an ajax request:
<script type="text/javascript">
token["<?php echo $this->security->get_csrf_token_name(); ?>"] = "<?php echo $this->security->get_csrf_hash(); ?>";
jQuery.ajaxSetup({data: token, type: "POST"});
</script>

Edit db record with modal window

I'm trying to edit some database record based on an ID that I'm saving into a button value.
#foreach ($employment as $empl)
<button data-toggle="modal" data-target="#edit-empl" href="#edit-empl" class="btn btn-default editbtn-modal" value="{{ $empl->id }}" type="button" name="editbtn">Edit</button>
<h3 class="profile-subtitle">{{ $empl->company }}</h3>
<p class="profile-text subtitle-desc">{{ $empl->parseDate($empl->from) }} - {{ $empl->parseDate($empl->to) }}</p>
#endforeach
As you can see here, I have an edit button with an id attached.
When I click edit I open a modal window to edit the fields and later on submit the form.
The thing is, I'm not sure how to get that id from the button into the modal window so I can compare the values and display the correct fields..
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="">
#foreach ($employment as $empl)
#if ($empl->id == buttonidhere)
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
#endif
#endforeach
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
I was able to pass the button value into the modal using javascript.. I put it into a hidden input but that doesn't help me at all because I can't get the input value in order to compare the values..
Solution 1: Using ajax
Step 1: Create a route in laravel which will return a JSON object containing employing data of requested employee.
For e.g,
/profile/employment/data/{empl_id}
Will get you employement data of id empl_id.
Step 2: Change your form as below
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
<input class="editID" type="hidden" name="editID" value="">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 3: Use javascript(jQuery) to get the data using ajax and load it into the form in modal.
jQuery code:
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$.ajax({
url: url,
method: "get"
}).done(function(response) {
//Setting input values
$("input[name='editID']").val(id);
$("input[name='company']").val(response.company);
$("input[name='to']").val(response.to);
$("input[name='from']").val(response.from);
//Setting submit url
$("modal-form").attr("action","/profile/employment/edit/"+id)
});
});
Solution 2: Using remote modal
Step 1:
Create another blade file for eg. editEmployee.blade.php and add the above form in it.
<form class="app-form" id="modal-form" action="/profile/employment/edit/{{ $empl->id }}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="{{ $empl->id }}">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 2: Create a controller which would return the above form as HTML.
Tip: use render() function. example
Step 3: load the form into modal window before showing using javascript(jQuery)
considering your modal id is "emp-modal"
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$('#emp-modal').modal('show').find('.modal-body').load(url);
});
One solution would be to send the details you want the same way you send the id to the modal.
and the proper way to send variables to modal is to include this in the button that opens modal:
data-variablename="{{$your-variable}}"
use this jQuery to get the values of your variables to modal. where edit-empl is the id of your modal and data-target of your button
$('#edit-empl').on('show.bs.modal',function (e) {
var variablename= $(e.relatedTarget).data('variablename');
$(e.currentTarget).find('input[id="yourinputID"]').val(variablename);

Select/Option Laravel 5.3

I want to use select and option in laravel 5.3 in simple HTML form. How to use select and option in laravel 5.3 in HTML form?
My coding:
#extends('layout.navigation')
<html>
<head>
</script>
</head>
<div class="row" id="contact_form">
<div class="col-sm-4">
<form action="{{route('online_form')}}" method="post" enctype="multipart/form-data">
{!!csrf_field()!!}
<br/><br/>
<div class="panel panel-default" id="form_panel">
<div class="panel-body">
<p>fill the form </p>
<input type="text" name="username" class="form-control" placeholder="Name" required>
<br/>
<input type="text" name="email" class="form-control" placeholder="Email" required><br/>
<p style="margin-top:20px;font-size:14px">Gender :&nbsp&nbsp&nbsp&nbsp <label
class="radio-inline"><input type="radio" style="margin-top:1px" name="gender"
value="Male">Male</label>
<label class="radio-inline"><input type="radio" style="margin-top:1px"
name="gender" value="female">Female<br/></label>
<p style="margin-top:20px;font-size:14px"> Select Language
:&nbsp&nbsp&nbsp&nbsp<label class="checkbox-inline"><input type="checkbox"
name="language" value="english">English</label>
<label class="checkbox-inline"><input type="checkbox" name="language"
value="hindi">Hindi</label>
<br/><br/>
<select id="state">
<option value="rajasthan">Rajasthan</option>
<option value="madhya pradesh">Madhya Pradesh</option>
</select>
<input type="submit" name="submit" value="Enter" class="btn btn-primary" >
</form>
</div>
</div>
To get the state value in the controller method, you can use Input facade.
$state = Input::get('state');
You can include the following as a dependency in composer "laravelcollective/html": "~5.0" and then include it in your config/app
Now, you can create your select dropdown using:
echo Form::select('gender', array('Male', 'Female'));
echo Form::select('language', array('English', 'Hindi'));
and so on.
For more information on this collective,
check this out.

What are all the steps I have to do to get my bootstrap email form working?

What should I do to get my form working? My website and everything is set up. I just can't get my form working. Can I do it from rdp? or firezila server?
<form>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Email address">
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="3" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit Request</button>
</div>
</div>
</div>
</div>
</form>
Submit your form to php and send the mail, ulter the code as below and consider adding a subject field too:
<form name="nameforform" action="thephpfilename.php" method="POST">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Name" name="name">
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Email address" name="email">
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="3" placeholder="Message" name="message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit Request</button>
</div>
</div>
</div>
</div>
</form>
Then create a php file (eg: thefilename.php) [besure to change the action attribute of the form element to that of the created php file's name.
The php file should have the following code:
<?php
$to = $_POST['email'];
$subject = '';
$message = $_POST['message'];
$headers = 'From:'. $_POST['name'] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
It would work, be sure to tweak your server's settings and also specify more info in the question next time.
Lastly how it works,
On submitting the form the browser sends a get/post request to the file specified on the action attribute of the form element. The php
file handles the request and does the needy. The data is stored with
reference to the name attribute of the input and textarea elements.
The data can be stored as arrays too. Here we use the php's mail()
function to send the mail using appropriate headers. HTML or bootstrap
cannot send mail independently without server intervention.
Be sure to comment for more info and other help, enjoy out there.

Resources