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