unable to run html code in laravel - php

i just wanted to run html source code but when i enter html source
code to my text area and run it it displays only htmlcode as a string
but i wanted to run that code...............................
View page
#extends('layouts.theme')
#section('content')
<div class="col-sm-8 col-sm-offset-2" style="border: 1px solid #ccc;">
<form class="form-horizontal login-form" method="post" name="loginFrm" id="loginFrm">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="f" class="col-sm-3 control-label"></label>
<div class="col-sm-12">
<textarea name="txtinput" placeholder="Paste your html code here" class="col-sm-12" rows="20"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="submit" name="subBtn" class="btn btn-info pull-right">Format and Analyze</button>
</div>
</div>
</form>
</div>
<div style="clear:both"></div>
<div>
#if(count($htmlpagedata)>0)
{{ htmlentities($htmlpagedata) }}
#endif
</div>
#stop
Controller
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class JHtmlParsing extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return view('input');
}
/**
* Parse HTML Page.
*
* #return Response
*/
public function parsehtml(Request $request)
{
$data=$request->get('txtinput');
return view('input')->with('htmlpagedata',$data);
}
}

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.
If you do not want your data to be escaped, you may use the following syntax:
{!! $variable !!}

you are using blade template so you need to extend .blade.
for example your file name is login then it should be
login.blade.php

Related

submit form not storing data to database

So i was trying to make a posting form using ckeditor and post it to database, and then try to display it in the same view, but after i submit the form i don't see anything in my database table so clearly it's not even storing to database, is there any mistakes in my controller or view ?
this is my GuestbookController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Guestbook;
class GuestbookController extends Controller
{
public function index()
{
$guestbooks = Guestbook::get();
return view('post.post_textarea',[
'guestbooks' => $guestbooks,
]);
}
public function store(Request $request)
{
Guestbook::create([
'name' => $request->name,
'message' => $request->message
]);
return redirect()->back();
}
}
this is my routes
Route::get('/posting','GuestbookController#index')->name('guestbook');
Route::post('/posting','GuestbookController#store')->name('guestbook.store');
this is my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Guestbook extends Model
{
protected $fillable = ['name', 'message'];
}
and this is my view
<section class="games-single-page">
<div class="container">
#foreach ($guestbooks as $guestbook)
<div class="card">
<div class="card-body">
<label>mike</label>
<h3>{{ $guestbok->name }}</h3>
{!! $guestbook->message !!}
</div>
</div>
#endforeach
<div class="card">
<div class="card-body">
<form action="/posting" method "POST">
<div class="form-group">
<label style="color: black;" >Title</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label style="color: black;" >Your Input</label>
<br>
<textarea class="form-control" name="message" id="" rows="10"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value"Send">
</div>
</form>
</div>
</div>
</div>
</section>
You forgot an equal sign '=' and a csrf field. try the answer.
<form action="/posting" method="POST">
{{csrf_field()}}

how to pass value from input form into another page in laravel

Excuse me, i'm new in learning about Laravel and I have a problem about show data value from form input.
I have create.blade.php :
#extends('layouts.master')
#section('content')
<div class="container">
<div class="header">
<h1><b>Create an account</b></h1>
<h5>Welcome to Konoha Village</h5>
</div>
{{ csrf_field() }}
#if(isset($name))
<div class="alert alert-warning alert-dismissible" role="alert">
Halo <strong>{{$name}}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">$times;</span>
</button>
</div>
#endif
<div class="form">
<form action="{{ url('final-test') }}" method="post" id="form1">
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
</div>
</div>
#endsection
and my controller with name AccController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AccController extends Controller
{
public function create() {
return view('home.create');
}
public function show(Request $r) {
$soul = $r ->name;
$pesan = "Your name is {$r->name}";
}
}
and my route in web.php :
//route to get play form
Route::get ('start', 'AccController#create' )->name('home.create');
Route::post('final-test', 'AccController#show');
i want to show in another page view that i called show.blade.php :
#extends('layouts.master')
#section('content')
<div>
{{$name = Input::get('name')}}
<h1>Your name is {{ $pesan }}</h1> </div>
#endsection
nothing's error in the end but it couldn't show the value from the input form, would you help me please?
Regards, Aga.
Your web.php:
//route to get play form
Route::get('/start', 'AccController#create')->name('home.create');
Route::post('/final-test', 'AccController#show')->name('home.show');
Added a name home.show to the route final-test.
Your AccController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AccController extends Controller
{
public function create()
{
return view('home.create');
}
public function show(Request $r)
{
$soul = $r->name;
return view('home.show')->with('soul', $soul);
}
}
Your show.blade.php:
#extends('layouts.master')
#section('content')
<div>
<h1>Your name is {{ $soul }}</h1>
</div>
#endsection
Your create.blade.php:
#extends('layouts.master')
#section('content')
<div class="container">
<div class="header">
<h1><b>Create an account</b></h1>
<h5>Welcome to Konoha Village</h5>
</div>
#if(isset($name))
<div class="alert alert-warning alert-dismissible" role="alert">
Halo <strong>{{$name}}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">$times;</span>
</button>
</div>
#endif
<div class="form">
<form action="{{ route('home.show') }}" method="post" id="form1">
{{ csrf_field() }}
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
</div>
</div>
#endsection
Here's where the most important change has been made: I moved {{ csrf_field() }} inside the form, so you don't get The page has expired due to inactivity. Please refresh and try again. Also changed the form action to the named route {{ route('home.show') }}.
I kept your <span aria-hidden="true">$times;</span> but this will only show $times;, might need to tweak that.
You are not following conventions here. show method is to data from database. In order to show form data you have do it in store method
HTML Code
There should be some minor changes in html form
{{--Changing in just action--}}
{{-- If it doesn't accept that action then replace it with {{AccController.php#store}} --}}
<form action="AccController.php#store" method="post" id="form1">
<div class="form-group">
<input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
</div>
<br>
<div class="form-group">
<input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
</div>
<div class="form-group">
</div>
</form>
Then in Controller you will get that form data in store function
public function store(Request $r) {
$soul = $r ->name;
$pesan = "Your name is {$r->name}";
return $pesan;
}
How to get it in route?
Well you are doing some mistakes in route.php. You just have to follow conventions which laravel provides us.
Replace your route code with this.
Route::resource('/posts', 'AccController.php');
Here it will automatically call all by default functions of Controller and assign them particular routes. For example Below the picture
You just have to type php artisan route:list in terminal or command prompt and you will see list of routes which you have created and which laravel creates for you along with method, URI and name. You just have to follow conventions and it will give you results automatically
Give it a try and tell me

Laravel 5.5 - $request->file returns null

I have created a form that uploads a file but it returns a null value when I submit. When I add in the enctype="multipart/form-data" it reloads the page and doesn't seem to go through my controller.
MY HTML FORM
<form class="form-horizontal" role="form" name="importform" method="POST" action="{{ route('import_type') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="control-group">
<label class="control-label"> </label>
<div class="controls">
<div class="control-group text-center">
<label class="btn btn-primary" for="file-selector">
<input id="file-selector" name="template_upload" type="file" value="" required autofocus style="display:none" onchange="$('#upload-file-info').html(this.files[0].name)" required> Upload List </label>
<span class='label label-default' id="upload-file-info"></span>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" id="import-submit" name="import-submit">
</div>
</div>
</form>
MY CONTROLLER: I am using the import method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImportTypeRequest;
use \App\Guest;
use \App\Role;
use \App\User;
use \App\Type;
use Illuminate\Support\Facades\Auth;
class GuestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::with('roles')->where('id', Auth::id())->get();
$types = Type::where('user_id', Auth::id())
->where('active',1)->get();
return view('view_import',compact('user','types'));
}
public function import(ImportTypeRequest $request)
{
$template_upload = $request->file('template_upload');
dd($template_upload);
}
}
Here are some suggested ways trying to solve this.
First of all in your import method add dd($request->all()) at its top and see what's the response. You should see all your form data and of course template_upload file. That's how you make sure that you see all the coming data from your form to your controller method.
Then try to get rid of ImportTypeRequest and just use Illuminate\Http\Request to see what will you get. If you got different result then the problem is in ImportTypeRequest class.
Then why don't you just use $request->template_upload?! It's cleaner I guess.

Eloquent $model->update() does not make changes on database record

When I update my comment it goes back to the page and changes the comment back to orignal, so the update hasn't been done. No errors or something.
db: comments
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('articleID')->unsigned();
$table->string('comment');
$table->timestamps();
});
model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['comment', 'articleID'];
public function article() {
return $this->belongsTo('App\Article');
}
}
CommentsController
public function edit($commentID) {
$comment = Comment::findOrFail($commentID);
return view('pages.comments.edit', compact('comment'));
}
public function update($commentID, CommentRequest $request) {
$comment = Comment::findOrFail($commentID);
$comment->update($request->all());
return view('/');
}
editComment view
<form action="{{ route('updateComments', ['commentID' => $comment->id]) }}" class="form-horizontal" method="get">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<!-- Article data -->
<div class="form-group">
<label class="col-sm-3 control-label" for="body">Comment</label>
<div class="col-sm-6">
<textarea class="form-control" id="body" name="body" maxlength="1000">{{ $comment->comment }}</textarea>
</div>
</div>
<!-- Add Article Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-default" type="submit"><i class="fa fa-pencil-square-o"></i> Edit comment</button>
</div>
</div>
</form>
Your problem is:
You cannot do form submit with get method, even with hidden method_field('PATCH') in fact it does not event get to update action (:
Your form field name body is not correct if we look at fillable field of model
So just fix Your form:
<form
action="{{ route('updateComments', ['commentID' => $comment->id]) }}"
method="post"
class="form-horizontal">
{{ csrf_field() }}
<!-- Article data -->
<div class="form-group">
<label class="col-sm-3 control-label" for="comment">Comment</label>
<div class="col-sm-6">
<textarea
id="comment"
name="comment"
maxlength="1000"
class="form-control"
>{{ $comment->comment }}</textarea>
</div>
</div>
<!-- Add Article Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-default" type="submit">
<i class="fa fa-pencil-square-o"></i> Edit comment
</button>
</div>
</div>
</form>
or change Your schema and model to have field called body not comment
p.s. also fix Your update action:
public function update(CommentRequest $request, $commentID) {
$comment = Comment::findOrFail($commentID);
$comment->update($request->except(['articleID'])); // for safety we are ignoring "possible existence" of articleID in forma
return redirect(route('updateComments', compact('commentID')));
}
doing return view('/') is not correct - it's trying to find file with name / that of course does not exist.

Creating New Forms in Laravel 5 and Submitting Them

I wish to clarify the steps to get a new form properly submitted to my database using Laravel 5.2 and Bootstrap 3.
I have the login/register pages set up properly using Laravel's defaults, and they work fine. I now want to create a user profile page accessible to authenticated users. I am using one row in the database for all of their user info. Some fields were filled in during registration, and now I want them to have access to additional fields (while restricting access to certain registration fields like user name).
In the example code below, there are fields to upload a personal photo, enter a first name, and enter a last name. (None of these were done during registration.)
What I have already done (all code is below):
Create the view profile.blade.php
Create a controller profileController.php
Update routes.php in the controller directory.
A note:
When I try to submit the form as it appears below, I get, Type error: Argument 1 passed to App\Http\Controllers\ProfileController::update() must be of the type array, none given.
What are the next steps required to get this page working properly?
profile.blade.php:
#extends('layouts.app')
#section('content')
<div class="container" style="padding-top: 30px;">
<h1 class="page-header">User Profile</h1>
<div class="row">
<!-- left column -->
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="text-center">
<i class="fa fa-user fa-5x"></i>
<h6>Please upload a photo...</h6>
<input type="file" class="text-center center-block well well-sm">
</div>
</div>
<!-- edit form column -->
<div class="col-md-8 col-sm-6 col-xs-12 personal-info">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/profile') }}">
{!! csrf_field() !!}
<div class="form-group">
<label class="col-lg-3 control-label">First name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo Auth::user()->firstname; ?>" id="firstname" name="firstname" placeholder="First..." type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Last name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo Auth::user()->lastname; ?>" id="lastname" name="lastname" placeholder="Last..." type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Submit
</button>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
</div>
</div>
</div>
#endsection
profileController.php:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('profile');
}
protected function update(array $data)
{
return User::update([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
]);
}
}
And I added the following in the routes middleware:
Route::get('/profile', 'ProfileController#index');
Route::post('/profile', 'ProfileController#update');
It's a protocol mismatch, since you're POSTing your form. You need to change your route to
route::post('/profile', 'ProfileController#index');
Using a validator is a great idea, since it will make sure that your input is exactly what you need it to be, and all required fields are filled out.
Your update function should look something like this:
public function update(Request $request)
{
$first_name = $request->input('firstname');
$last_name = $request->input('lastname');
$id = Auth::user()->id;
$user = \App\User::find($id);
$user->firstname = $first_name;
$user->lastname = $last_name;
$user->save();
return view('profile');
// Sanitize, validate, before you do ANYTHING with update
// Instead of returning the update result, you can instead show another view or forward them to another page.
}

Categories