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?
Related
This question already has answers here:
How to add elements to an empty array in PHP?
(8 answers)
Closed 8 months ago.
I'm new at PHP and sessions. For a small project for school. How do i view only the name property of each object stored in a session variable?
I tried using foreach and then using $log->name to only get the name property, but it gives me a "Warning: Attempt to read property "name" on string in".
This is where i add new object to session variable:
<?php
class Log
{
public $name;
public $location;
public $activity;
}
if (isset($_POST['submit'])) {
session_start();
$new_log = new Log;
$new_log->name = $_POST['name'];
$new_log->location = $_POST['location'];
$new_log->activity = $_POST['activity'];
$_SESSION['logs'][] = serialize($new_log);
header('Location: data.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p class="lead">Mensen aanmelden</p>
<form action="index.php" method="POST">
<!-- Vul de form tag aan zodat deze informatie naar process.php zal gepost worden -->
<input type="text" name="name">
<hr>
<input type="radio" name="location" value="inside"> Binnen<br>
<input type="radio" name="location" value="outside"> Buiten
<hr>
<input type="radio" name="activity" value="drinks"> Drinken<br>
<input type="radio" name="activity" value="dance"> Dansen
<hr>
<input type="submit" name="submit" value="Voeg toe">
</form>
</body>
</html>
This is where i want to view each name of the object:
<?php
session_start();
foreach ($_SESSION['logs'] as $log) {
echo $log ->name. "<br>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
return
</body>
</html>
Try this:
$_SESSION['logs'][] = serialize($new_log);
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...
I am creating a report generator, that should save a report and also redirect the user to a version of the report on a specific url (in this case I just added yahoo as test).
Question: Is it possible to save a file and redirect a user to an url in same script?
Problem: The script does the redirect but does not store the file.
<!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>Report generator</title>
</head>
<body>
<pre>
<form method="post" action="https://se.yahoo.com" target="_blank">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<input type="submit" name="button_1" value="button_1">
</form>
<?php
var_dump($_POST);
// Button event listener.
if (isset($_POST["button_1"])) {
$json_data = "string";
$data = json_encode($json_data);
file_put_contents(
"user_data/data.txt",
$json_data
);
}
?>
</body>
</html>
If you put https://se.yahoo.com is the action attribute that is where the form will post the form data to, so it wont go to this script at all
<?php
// Button event listener.
if (isset($_POST["button_1"])) {
$json_data = "string";
$data = json_encode([$json_data]);
file_put_contents("user_data/data.txt",$json_data);
// really not sure if this is what you wanted to do, bit of a guess really
header('Location: https://se.yahoo.com?fname='.$_POST['fname']);
exit();
}
?>
<!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>Report generator</title>
</head>
<body>
<form method="post" action="" target="_blank">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<input type="submit" name="button_1" value="button_1">
</form>
</body>
</html>
How can I get an HTML value to send to PHP. I would like to avoid using a form. For example I have an input:
<input name="input" id="input">Input</input>
while in PHP:
$input = $_POST['input'] --> but didn't work
or
$input = $_GET['input'] --> still didn't work
I know that I will be able to get it using form then action="another file" but I want it within a file. Please help. Thank you.
If you simply want to use js to append the input value in url variable then you can use js function as follows;
client.php
<!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>Document</title>
</head>
<body>
<input type="text" id="txt">
<a onclick="append()" id="anch" href="server.php?text">Send value</a>
</body>
</html>
<script>
function append()
{
txt=document.getElementById('txt');
anch=document.getElementById('anch');
anch.href=anch.href+"="+txt.value;
}
</script>
server.php
$variable = $_GET["input"];
echo $variable;
Output on localhost:
After clicking send value:-
Update: I just read the end of your question. If you want to show the value of input on same page/file then you can use following code;
<!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>Document</title>
</head>
<body>
<input type="text" id="txt">
<?php
if(isset($_GET['text']))
{
$text=$_GET['text'];
echo "<a onclick='append()' id='anch' href='?text=$text'>Enter Value</a>";
}
else
{
echo "<a onclick='append()' id='anch' href='?text'>Enter Value</a>";
}
?>
<br>
<?php
if(isset($_GET['text']))
echo "<div id='values'>".$_GET['text']."<div>";
?>
</body>
</html>
<script>
function append()
{
txt=document.getElementById('txt');
anch=document.getElementById('anch');
if(anch.href.match(/=/)=="=")
anch.href=anch.href+txt.value+"<br>";
else
anch.href=anch.href+"="+txt.value+"<br>";
}
</script>
Output:
Check output on phpFiddle
It seems that the way to go is to use the GET method.
You will need to access your page with the get parameters included like this:
http://localhost/index.php?input=VALUE_HERE
And in your php file:
$variable = $_GET["input"];
echo $variable;
But if you insist to use inline html elements to get data, you need to use javascript for that like this for jquery:
alert("This is the value of the input" + $("#input").val());
Don't use this
<input name="input" id="input">Input</input>
Use this
<input name="input" id="input">
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() !!}