Show each name property of all the objects in PHP [duplicate] - php

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);

Related

How can I make an infinite amount of posts in PHP without overwriting said post on the same page?

I'm trying to create a post request on the same page with infinite amount of posts. The only problem is that the post that I am doing keeps overwriting.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="https://wikiscams.org/form.php" method="post">
<input type="text" placeholder="Anon" name="user">
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
if(isset ($_POST["submit"])) {
$user = $_POST['user'];
// echoes post but overwrites
echo $user;
}
?>
What should I do?
With HTML you can use arrays on PHP side and that's what you need right now.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="https://wikiscams.org/form.php" method="post">
<?php if(!empty($_POST['user'])){
foreach ($_POST['user'] as $key => $value) { ?>
<input type="hidden" name="user[]" value="<?=$value?>">
<?php }
} ?>
<input type="text" placeholder="Anon" name="user[]">
<input type="submit" name="submit">
</form>
<?php
if(!empty($_POST['user'])){
foreach($_POST['user'] as $key => $user){
echo $user."<br />";
}
}
?>
</body>
</html>

Forbidden You don't have permission to access this resource Error

I have started learning php recently and have confront with this error while working on localhost. I have tried all possible solutions available on the web but haven't got ridden of the problem.
I am using VS code for text editing and installed php and apache(as the webserver) on windows 10. I have trying the code to get the input and print that on the screen but this error is showing every time.
Please help me out about this
I am including the code
<!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>FORM</title>
</head>
<body>
<!--#an example showing the working of request variable-->
<form method="POST" action="<?php echo $_server['php_self'];?>">
name:- <input type="text" name="fname" id="1">
<input type="submit" value="submit">
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$name = $_REQUEST['fname'];
if(empty($name))
{
echo "name is empty";
}
else
{
echo "NAME IS ";
echo $name;
}
}
?>
</body>
</html>

Report generator - save and redirect to report

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>

Solving a mathematical question in PHP/HTML without Javascript

Im kind of new to programming (taking a course currently) and our task over the weekend is to build a website (really basic) where we have to solve a addition of 2 random numbers. I was able to create those random numbers, but what with is, to create something like a field the user (in this case me) has to fill out and "submit"; and then the website should show my own anwser and the correct one. The issue im facing in addition to that is, that when i create a submit function the website refreshes and those random numbers change each time.
My code looks like this (forgive me for using german terms):
<?php
$zufallszahl = rand(1,100) . "+";
$zufallszahl1= rand(1,100);
$solution= bcadd($zufallszahl,$zufallszahl1);
?>
<!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>Homework Taschenrechner</title>
</head>
<body>
<h1>Additionen</h1>
<hr>
<h4>Addiere die folgenden Zahlen:</h4>
<p> <?php echo $zufallszahl; echo $zufallszahl1 ?> <p>
<form method="post">
<input type="number" name="Usereingabe" min="0" placeholder="Deine Antwort">
</form>
</body>
</html>
I would really appreciate a "simple" solution, I googled this issue already for an hour, but most solutions are just too complicated for me right now, and i end up not understanding what the code is about. So just to sum up, I would like to know how to create a submit field that doesnt refresh the website so the random numbers stay and a field that "checks" the answer and says its true/false.
Although, if you were allowed to use JS, it would be a lot simpler. However, since you wanted it in just PHP, the code below should work. Feel free to modify it as per your needs
<?php
if(!isset($_POST['zufallszahl']) && !isset($_POST['zufallszahl1'])) {
$zufallszahl = rand(1,100);
$zufallszahl1= rand(1,100);
// $solution= bcadd($zufallszahl,$zufallszahl1);
} else {
$zufallszahl = $_POST['zufallszahl'];
$zufallszahl1 = $_POST['zufallszahl1'];
$solution = bcadd($zufallszahl,$zufallszahl1);
}
?>
<!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>Homework Taschenrechner</title>
</head>
<body>
<h1>Additionen</h1>
<hr>
<h4>Addiere die folgenden Zahlen:</h4>
<p> <?php echo $zufallszahl . " + "; echo $zufallszahl1 ?> <p>
<form method="post">
<input type="number" name="zufallszahl" value="<?php echo $zufallszahl; ?>" readonly />
<input type="number" name="zufallszahl1" value="<?php echo $zufallszahl1; ?>" readonly />
<input type="number" name="Usereingabe" min="0" placeholder="Deine Antwort" value="<?php echo $_POST['Usereingabe'] ?? ''; ?>">
<button>Check Answer </button>
<?php if(isset($_POST['Usereingabe'])){ ?>
<?php if($_POST['Usereingabe'] == $solution)
echo "Correct Answer";
else {
echo "Wrong Answer. Correct answer is $solution";
} ?>
<?php } ?>
</form>
</body>
</html>
NOTE
If you are using version below PHP 7, just use <?php echo isset($_POST['Usereingabe'])? $_POST['Usereingabe'] : ''; ?> in place of <?php echo $userSolution ?? ''; ?>
So you can keep them in a _SESSION variable and when they submit the answer you can check the _POST variable against the _SESSION one
<?php
if(isset($_POST['Usereingabe']))
{
if($_POST['Usereingabe'] == $_SESSION['sol']){
$check = "correct answer";
}
else
{ $check = "wrong answer"; }
}
else {
$zufallszahl = rand(1,100) . "+";
$zufallszahl1= rand(1,100);
$solution= bcadd($zufallszahl,$zufallszahl1);
$_SESSION['zufallszahl1']=$zufallszahl1;
$_SESSION['zufallszahl']=$zufallszahl;
$_SESSION['sol']=$solution;
}
// do anything else u want
?>
<!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>Homework Taschenrechner</title>
</head>
<body>
<h1>Additionen</h1>
<hr>
<h4>Addiere die folgenden Zahlen:</h4>
<?php if(isset($check)) { echo $check; } ?>
<p> <?php echo $_SESSION['zufallszahl']; echo $_SESSION['zufallszahl1']; ?> <p>
<form method="post" action="*your file.php*">
<input type="number" name="Usereingabe" min="0" placeholder="Deine Antwort">
<button type="submit">Submit!</button>
</form>
</body>
</html>
You could use two extra input tags in your form of type="hidden" that contain the random numbers in their value fields, and check if they are empty. If they are, generate new ones, otherwise keep the old ones.

how to store a value of an HTML input in a cookie in PHP

here is the html 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>
<form action="test.php" method="POST">
<input type="text" name="pseudo">
<input type="submit">
</form>
</body>
</html>
the form returns the value of the input in a page called test.php and the stock in a coockie but it does not work
here is the php code
<?php
setcookie("pseudo", $_POST['pseudo'], time() + 365*24*3600);
echo "Your name is " . $_COOKIE['pseudo'];
you are missing the cookie name parameter which is required to set a cookie.
please see the below implementation
setcookie("Cookie_Name", $_POST['pseudo'], time() + 365*24*3600);

Categories