PHP - Processing after input is entered twice in a row - php

I have a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.

<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0

You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>

Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>

Related

PHP send variable value external webpage

I'm currently trying to send the two variable values, $incorrect and $correct from a form.
I have tried to use $_POST[] to create this functionality, yet it doesn't work!
Could anyone help me when creating such a functionality ?
page1.php
<form action="testresult.php" method="POST">
<label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
<input type="text" id="question1_answer" name="question1_answer" required><br><br>
<label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
<input type="text" id="question2_answer" name="question2_answer" required><br><br>
<label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
<input type="text" id="question3_answer" name="question3_answer" required><br><br>
<input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
<input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" />
<input type="submit" name="submitAnswers" value="Submit">
</form>
<?php
if(isset($_POST["submitAnswers"])){
$correct = 0;
$incorrect = 0;
$qn1_ans = $_POST['question1_answer'];
$qn2_ans = $_POST['question2_answer'];
$qn3_ans = $_POST['question3_answer'];
if ($qn1_ans == $array_answer[$question_num_array[0]]){ $correct ++; }
else { $incorrect ++; }
if ($qn2_ans == $array_answer[$question_num_array[1]]){ $correct ++; }
else { $incorrect ++; }
if ($qn3_ans == $array_answer[$question_num_array[2]]){ $correct ++; }
else { $incorrect ++; }
}
?>
And on the receiving end
page2.php
<html>
<head>
<title>Results</title>
</head>
<body>
<?php
$correct = $_POST['correct'];
$incorrect = $_POST['incorrect'];
echo $correct."test ".$incorrect;
?>
</body>
</html>
Your first page should JUST show the questions, it will be like this.
<form action="testresult.php" method="POST">
<label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
<input type="text" id="question1_answer" name="question1_answer" required><br><br>
<label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
<input type="text" id="question2_answer" name="question2_answer" required><br><br>
<label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
<input type="text" id="question3_answer" name="question3_answer" required><br><br>
<!--input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
<input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" /-->
<input type="submit" name="submitAnswers" value="Submit">
</form>
When you hit the submit button, the answers are posted to testresult.php.
In this file, first checking the answers, then showing the result.
The page doesn't contain questions, so you need to include the php file too.
<html>
<head>
<title>Results</title>
</head>
<body>
<?php
if(isset($_POST["submitAnswers"])){
include 'question.php'; #include the question array.
$correct = 0;
$incorrect = 0;
$qn1_ans = $_POST['question1_answer'];
$qn2_ans = $_POST['question2_answer'];
$qn3_ans = $_POST['question3_answer'];
if ($qn1_ans == $array_answer[$question_num_array[0]]){ $correct ++; }
else { $incorrect ++; }
if ($qn2_ans == $array_answer[$question_num_array[1]]){ $correct ++; }
else { $incorrect ++; }
if ($qn3_ans == $array_answer[$question_num_array[2]]){ $correct ++; }
else { $incorrect ++; }
echo $correct."test ".$incorrect;
}
?>
</body>
</html>
Where did those variables from from?
In your code you're calling two vars $array_answer and $question_num_array.
None of these two variables are set! Please fix this issue.
The Form action you've set to your testresult.php. So surely the form will carry all of its data to that page. But you are trying to get the data on the same page as if(isset($_POST["submitAnswers"])) where is the form; what actually should exist in that testresult.php page. So should be like this:
index.php or something.php
<form action="testresult.php" method="POST">
<label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
<input type="text" id="question1_answer" name="question1_answer" required><br><br>
<label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
<input type="text" id="question2_answer" name="question2_answer" required><br><br>
<label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
<input type="text" id="question3_answer" name="question3_answer" required><br><br>
<input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
<input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" />
<input type="submit" name="submitAnswers" value="Submit">
</form>
testresult.php
<?php
// questions array that is called in the form's page should be
called here too to get the questions and comparisons.
if (isset($_POST['submitAnswers'])) {
$correct = 0;
$incorrect = 0;
$qn1_ans = $_POST['question1_answer'];
$qn2_ans = $_POST['question2_answer'];
$qn3_ans = $_POST['question3_answer'];
if ($qn1_ans == $array_answer[$question_num_array[0]]) {
$correct++;
} else {
$incorrect++;
}
if ($qn2_ans == $array_answer[$question_num_array[1]]) {
$correct++;
} else {
$incorrect++;
}
if ($qn3_ans == $array_answer[$question_num_array[2]]) {
$correct++;
} else {
$incorrect++;
}
}
?>
<html>
<head>
<title>Results</title>
</head>
<body>
<?php
$correct = $_POST['correct'];
$incorrect = $_POST['incorrect'];
echo $correct . 'test ' . $incorrect;
?>
</body>
</html>

I am getting a really weird result on my php form output

Here is my code:
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_POST['postName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$age = $_POST['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_GET['getName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$age = $_GET['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
I have two forms. Both displaying the exact same thing, but one form using POST and one using GET.
I have gotten so close to finishing this off but now I have another small/weird issue.
The code technically works correctly, but here's the output explanation:
when I first open up the page the GET form already has the result "Sorry, try again when you're 16 or older." When I fill out the first 'simple' form, it displays the result correctly but then the POST form shows the "Sorry, try again..." result. Then, when I fill in the information and click submit, it displays the correct result and the other two forms are blank as they're supposed to be, and then the same result when I fill out the GET form.
Any help on this is much appreciated.
Try this code :
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
if (isset($_POST['firstName']) && $_POST['lastName'])
{
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
}
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_POST['postName']))
{
echo $_POST['postName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (isset($_POST['age']))
{
$age = $_POST['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_GET['getName']))
{
echo $_GET['getName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
if (isset($_GET['age']))
{
$age = $_GET ['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
Please try this. I hope it will help.
Replace
if ($_SERVER['REQUEST_METHOD'] == "POST") {
with
if (isset($_POST['age'])) {
Similarly,Replace
if ($_SERVER['REQUEST_METHOD'] == "GET") {
with
if (isset($_GET['age'])) {
When you first enter on page, default REQUEST_METHOD is GET so you should check if isset($_GET['age']) {
and here check if it is more than 16
}
also you should check this one
echo $_GET['getName']; and change on this
echo isset($_GET['getName']) ? $_GET['name'] : "";
You should also check $_POST request like this and your program will work correctly.

PHP help me with this some notifications

I'm having a hard time figuring how to fix this notification. I already did it but the result was the code didn't work. My logic is not that high here is the code:
Here is the error.
Would you also explain why the gender is undeinfed but the textbox and textarea was just fine?
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$x=$_POST['name'];
$y=$_POST['email'];
$z=$_POST['website'];
$c=$_POST['comment'];
$v=$_POST['gender'];
if($x == '' or $y == '' or $z==''or $c=='' or $v==''){
echo "* required all fields";
}else{
$result=$x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>'.$v;
}
}
?>
<form action="" method="post">
NAME:<input type="text" name="name"><br><br>
EMAIL:<input type="text" name="email"><br><br>
WEBSITE:<input type="text" name="website"><br><br>
<textarea name="comment" rows="5" cols="40"></textarea><br><br>
GENDER:<input type="radio" name="gender" value="Male">MALE
<input type="radio" name="gender" value="Female">Female<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h3>YOUR INPUT:</h3>";
echo $result;
?>
</body>
</html>
if($x == '' ||$y == '' || $z=='' || $c=='' || $v==''){
echo "* required all fields";
}else{
You cant user or man check the PHP manual
try this way
if(($x="" || empty($x))&&($y="" || empty($y))&&($z="" || empty($z)))
{
echo "* required all fields";
}
may be it will help
What you need is a JS function to validate your radio buttons at first, or you could also use the HTML attribute required (I have written both of them) followed by only a few if-statements This should work just fine.
<!DOCTYPE HTML>
<html>
<head>
<script>
function validateForm(){
for(i=0; i<document.form.radios.length; i++){
if(document.form.radios[i].checked==false){
c=1;
}
else{
c=0;
break;
}}
if(c==1){
alert('Please select an option');
}
}
</script>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$x=$_POST['name'];
$y=$_POST['email'];
$z=$_POST['website'];
$c=$_POST['comment'];
if($x == '' or $y == '' or $z==''or $c=='')
{ echo "* required all fields";
}else{
$result=$x.'<br>'.$y.'<br>'.$z.'<br>'.$c;
}
}
?>
<form action="" method="post">
NAME:<input type="text" name="name"><br><br>
EMAIL:<input type="text" name="email"><br><br>
WEBSITE:<input type="text" name="website"><br><br>
<textarea name="comment" rows="5" cols="40"></textarea><br><br>
<input type="radio" name="gender" value="Male" required /> Male
<input type="radio" name="gender" value="Female" /> Female
<input type="submit" name = "submit" value="SAVE" />
</form>
<?php
if(isset($_POST['submit']))
{
if(!($x == '' or $y == '' or $z==''or $c=='')){
echo "<h3>YOUR INPUT:</h3>";
echo $result;
}else
echo "NOT ALL FIELDS WERE SELECTED.";}
?>
</body>
</html>
You have some undefined variables.In order to make it work try this:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
$result = '';
$x = '';
$y = '';
$z = '';
$c = '';
$v = '';
if(isset($_POST['submit'])){
if(isset($_POST['name'])){
$x= $_POST['name'];
}
if(isset($_POST['email'])){
$y=$_POST['email'];
}
if(isset($_POST['website'])){
$z=$_POST['website'];
}
if(isset($_POST['comment'])){
$c=$_POST['comment'];
}
if(isset($_POST['gender'])){
$v=$_POST['gender'];
}
.......
But you should separate your HTML code from the PHP,
maybe creating and index.html and from.php file.
Keeping all into the same file is a very bad practice.
$v=isset($_POST['gender'])?$_POST['gender']:'';

Php(sessions)...how to put textbox value in session and pass it to another page?i m getting error

<?php
session_start();
?>
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<?php
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
?>
<input type="submit" value="submit" />
</form>
2nd page
<?php
session_start();
?>
<?php
if (isset($_SESSION['userGet'])) {
$form_value = $_SESSION['userGet'];
}
echo $form_value ;
?>
..........................................................................................................
You have used the text box name with that check the submit button name too.
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
session_start();
if(isset($_POST['submit'])&&isset($_POST['user'])) {
$_SESSION['userGet']=$_POST['user'];
}
?>
And in your second page
<?php
session_start();
if (isset($_SESSION['userGet'])) {
echo $form_value = $_SESSION['userGet'];
}
Give the submit button a name of submit then use the following PHP code:
if(isset($_POST['submit'])){
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
}

two HTML forms submitting same php script, but i want 2nd form to run the code following it not the entire script

i want to do somthing like:
1.php:
<html>
<form action=1.php method=POST enctype="multipart/form-data">
Choose a user name:<input type="text" name="username">
<input type="submit" value="Save and Proceed">
</html>
<?php
if(isset($_POST['username']))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="1.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
f( isset($_POST['age']))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
After the second form is submitted i do not want the script 1.php to run from the start but i want it to run the code following the form which is echoing the age only.
i do not want to go putting the later code in second script and accessing the variable of first script through making them session variables.please help. thankyou in advance
Change your condition to this
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed')) {
}
this is the code i changed:
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
Choose a user name:</font> <input type="text" name="username">
<input type="submit" value="Save and Proceed">
</form>
</html>
<?php
if(isset($_POST['username']) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
if( isset($_POST['age']) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your name is" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>

Categories