Learning Laravel Basic: Post Method Problem - php

So, I am trying to redirect the page from login.blade.php into halamanUtama.blade.php with a button in a form, but somehow it always shows page expired. I don't know why. Here is my source code:
login.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Login</title>
<!--CSS-->
<link rel="stylesheet" href="{{ asset('css/styles.css') }}" type="text/css">
</head>
<body>
<div class="login-page-frame">
<div class="header-login-page-frame">
<h3>Login</h3>
</div>
<div class="inner-login-form-frame">
<form method="post" action="./home" class="login-form">
<input type="text" placeholder="username" name="login-username">
<br>
<input type="password" placeholder="pass" name="login-password">
<br>
<button type="submit" class="btn-login">Log In</button>
</form>
</div>
</div>
</body>
</html>
web.php
Route::get('/', function () {
return view('login');
});
Route::post('/home', 'loginController#index');
loginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class loginController extends Controller
{
//
public function index(){
return view("halamanUtama");
}
}
halamanUtama.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Halaman Utama</title>
</head>
<body>
Selamat datang
</body>
</html>
Please help.

check this steps:
your login route is better to be get
check your directory in resource/views
your submit form
...
have a look on tutorials like scotch.io, etc...

Related

passing an array of objects from controller to blade view [Laravel 8]

I want to fetch some data from an API and then pass it to a blade view.
but when i try to pass the data to the blade view i get an error:
Undefined variable. $result is undefined
The function I'm using in my Controller is:
public function getAll(Request $request){
$userInput = $request->input();
$response = Http::withHeaders(
[
"x-rapidapi-host"=> "xxxxxxxx",
"x-rapidapi-key"=> "xxxxxxxxx",
]
)->get("https://imdb8.p.rapidapi.com/auto-complete?q=",$userInput);
return view('list', ['result' => $response]);
}
and my blade file is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>movies</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="m-2">
<form action="/getList" method="get">
<input class="form-control w-50 m-2" type="text" name="q" id="showName">
<input class="btn btn-primary m-2" type="submit" value="Search...">
</form>
</div>
{{$result['l']}}
</body>
</html>
I have tried to use foreach to loop through the array in the blade view but i had the same error so i changed the code to the code above but still having the same error!
and in my controller i have tried to use toArray(); with $response after reading the Laravel documentation for a while but still got the same error.
how can i pass an array of objects from the controller to the blade view?

Laravel 8 MVC how to send a value from Controller to View

I'm trying to display set of values(hashtags) which I get from DB to View through controller.
But I'm stuck at here in this controller. Not sure what function to use, view or With after getting DB values. What will be best option to use? and how to use it in view
here is my sample code of Controller class
<?php
namespace App\Http\Controllers;
use App\Models\Hashtag;
use Illuminate\Http\Request;
use App\Http\Requests\SearchHashtagRequest;
class BannedHashController extends Controller
{
public function index(){
if(request()->query('query_hashtag')){
$hashtags = HashTag::where('hashtag','LIKE',"%{request()->query('query_hashtag')}%");
//Stuck in here
}else{
}
return view('bannedhashtags.index');
}
}
Index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instagram Hashtags</title>
</head>
<body>
<h1>Check your Instagram hashtags</h1>
<form method="get" type="get" action="{{url('bannedhashtags')}}">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..." name="query_hashtag">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Go!</button>
</span>
</form>
</body>
</html>
you can do it like below:
public function index(){
if(request()->query('query_hashtag')){
$hashtags = HashTag::where('hashtag','LIKE',"%{request()->query('query_hashtag')}%");
return view('bannedhashtags.index',['hashtags'=>$hastags]);
}
return view('bannedhashtags.index');
}
and in this case you need to check for $hastags variable in your html like below
#if(isset($hashtags))
your html code here if the hashtags variable is set
#endif
if what you are looking for is to filter the hashtags list it will be better to show the hashtags list (maybe using pagination if the list is too big) and then filter it by using ajax request.

How to get php variable from an action form and use it in the form thats activating the action

I have two files here as a test the first one which is this below and when I click submit it suppose to do the action on the next page but I want to know how to get retrieve athe $life variable from the action php file and put it in the normal html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
Second file which is the php file:
<?php
$life ="Yo";
?>
check this code. you need to run this code in server
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
include_once 'edit.php';
echo $life;
?>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
and this is your include edit.php
<?php
$life = 'Ok';
?>
then your first file show ok when you run this code

Buttons that affect a function - PHP

Scenario:
I am trying to have a part of my page constently run a function, the function is then affected when the above buttons are clicked.
The buttons call functions that are within the main/constant function; master($selected).
Here is what i have tried.
index.php:
<? session_start();
require("inc.ini.php");
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta charset="utf-8">
<title>Buttons to funtions</title>
</head>
<body>
<main>
<header>
<form method="post" action="<?=$_SERVER['php_self']?>">
<button type="submit" name="change" value="insert, <?$selected = "func1";?>">Func 1</button>
<button type="submit" name="change" value="insert, <?$selected = "func2";?>">Func 2</button>
</form>
<!--
I want to press a button and when it is pressed it calls the function selected and gives it the value of which the button represents.
-->
</header>
<figure>
<?
if($_POST['change']){
echo $selected;
master($selected);
}
?>
</figure>
</main>
</body>
</html>
inc.ini.php:
<? session_start();
function master($selected){
if($selected == "func1"){
function func1(){
echo "func 1";
}
}
if($selected == "func2"){
function func2(){
echo "func 2";
}
}
}
?>
Another side question. do i need to do these if statements, can the $selector jump straight to the new function.
Php is a server side language, this means that a request needs to be sent to the server to make your code run.
Also when a form submits all of it's children are sent, so it is not possible to distinguish which has been clicked.
That being said:
<?php
session_start();
require("inc.ini.php");
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta charset="utf-8">
<title>Buttons to funtions</title>
</head>
<body>
<main>
<header>
<form method="post" action="<?=$_SERVER['php_self']?>">
<button type="submit" name="change" value="Func1">Func 1</button>
</form>
<form method="post" action="<?=$_SERVER['php_self']?>">
<button type="submit" name="change" value="Func2">Func 2</button>
</form>
</header>
<figure>
<?php
if (isset($_POST['change'])) {
echo $_POST['change'];
master($_POST['change']);
}
?>
</figure>
</main>
</body>
</html>
If you use 2 forms then you get different values for the same name.
Looking at your inc.ini.php file it seems you're defining functions based on which input has been entered. I would suggest not doing this but if your heart is set on it then okay.
Please add a comment to this post if you need more help.
Hope this helps.

Laravel - Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server

php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel PHP Framework</title>
</head>
<body>
<div class="welcome">
<h1>test page</h1>
<form action="{{ URL::route('dis') }}" method="post">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit">
</form>
</div>
</body>
</html>
display.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>display page</title>
</head>
<body>
<div class="welcome">
<h1>test page for display</h1>
</div>
</body>
</html>
Routes.php
<?php
Route::get('/', array('uses'=>'HomeController#showWelcome', 'as' => 'home'));
Route::group(array('before'=>'csrf'),function()
{
Route::post('/dis', array('uses'=>'HomeController#display', 'as' => 'dis'));
});
HomeController.php
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
public function display()
{
return View::make('display');
}
}
there is a error in laravel 5,
when i try to submit the form hello.html following error appears.
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
One thing I see is you need a CSRF token in the form. If you also install illuminate\html (which is not included now in Laravel 5) you can get it by using Form::open instead of creating the <form> tag in HTML as you are.
Alternatively you can add the token to a form by just calling csrf_field function:
{!! csrf_field() !!}

Categories