The website generates the random number from 1 to 100 when accessing the first page(page1.php). And the user will guess the number.
The first page contains
- a text box for accepting a
number from the user, and a submit button.
The second page(page2.php) will be returned to the user if the guess number is too high or too low. And the page shows a message telling the user "Too High" or "Too Low". The page also contains
a button(retry button) that allows the user to go back to the first page(page1.php) to re-enter a new number
a button that allows the user to quit the game.
The third page(page3.php) is returned to the user if the guess is correct. The page displays "Correct", the random number, and the count of tries.
And I have this index.php which is heart for all the pages. And here is the code.
index.php
<?php
$name = '';
$inputnumber = '';
$random = 33; //this is just an assumption to keep it simple
$message = '';
$guesscount = '';
if (isset($_POST['action'])) {
$action = $_POST['action'];
}
if ($action === 'guess') {
$guesscount = $_POST['$guesscount'];
$inputnumber = $_POST['$inputnumber'];
if ($inputnumber == $random) {
$message = "Correct!";
include 'page3.php';
}
if ($inputnumber > $random) {
$message = "Too High";
include 'page2.php';
}
if ($inputnumber < $random) {
$message = "Too Low";
include 'page2.php';
}
}
if ($action === 'retry') {
include 'page1.php';
}
page1.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Number Guess: <input type="text" name="$inputnumber" value="<?php if(isset($inputnumber)==1){
echo $inputnumber;}else echo ""; ?>" /><br>
<input type="submit" name="action" value="guess" />
<hr>
Guess Count: <?php echo $guesscount; ?>
</form>
</body>
</html>
page2.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
<input type="hidden" name="$guesscount" value="<?php echo $guesscount;?>"/><br>
<input type="submit" name="action" value="retry" />
<hr>
Guess Count: <?php echo $guesscount;?>
</form>
</body>
</html>
page3.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
Number of Tries: <?php echo $guesscount; ?>
<input type="submit" name="action" value="ok" />
</form>
</body>
</html>
page1.php is the page to load first.
Challenge I have faced is, I couldn't keep the $guesscount stable always. It keeps resetting on me. I have tried session but couldn't resolve it.Please help resolving it.
Thanks in advance.
I don't know why but my gut feeling tells me that the reason why the session is not working for you on other pages is because you do not initiate it ??
So what you have to do is:
index.php
<?php
session_start();
$_SESSION['myVariable'] = 'myVariable';
?>
page1.php
<?php
session_start();
$mySessionVar = $_SESSION['myVariable'];
var_dump($mySessionVar); // <- this should print myVariable
?>
You may get an error saying that $_SESSION is null or not set and to prevent that you can just enclose $_SESSION inside and isset method
if(isset($_SESSION['myVariable']) && $_SESSION['myVariable'] != null) {
$mySessionVar = $_SESSION['myVariable'[;
}
Related
It is printing my print statement even though I haven't touched anything on the page. Without having put anything in the number box, or having pushed the submit button it seems to be running anyway, ignoring/passing the if statement.
<!doctype html>
<html>
<head>
<link rel="icon" type="image/ico" href="/favicon/favicon.ico">
</head>
<body style="background-color:black;color:white">
<form name="form1" method="POST" action="bogo.php">
Size
<input type="number" name="size" value="" max="10000" min="1">
<input type="submit" name="submit" value="submit">
</form>
<br/>
<?php
function foo(){
if(isset($_POST["size"]) && isset($_POST["submit"])){
$a = $_POST["size"];
$i = 0;
for($i=0;$i<$a;$i++) {
$arr[$i] = $i;
}
print("Original Array: <br/>");
}
else {
}
}
foo();
?>
</body>
</html>
You need to put php code outside html code.
<html>
</html>
<?php>
php code
<?>
I'm new to PHP so please bear with me.
I'm trying to decide the action based on the given input.
Given below is the code I've written for this simple task
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method="POST" action="<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
echo "output.php";
}
else
{
echo "wronguser.php";
}
}
?>">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
Here I want the form to redirect to "output.php" if the user is "USERA" or "wrongouput.php" for any other user name entered. Although it works as expected, I'm redirected to the right page only when I press the submit button the 2nd time.
Why is this ?
Also, reloading the page doesn't seem to bring back the original page with the text field containing the default "Enter Something" text. I have to run it all over again from the IDE.
Why is this ?
You can try a different approach
<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
require_once("output.php");
}
else
{
require_once("wronguser.php");
}
}
else
{
?>
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method="POST" action="">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
<?php
}
?>
Take your code out of the action, make the action for the form self so it fires on itself, then put your PHP above all the HTML at the top of the file. That'll stop the double click issue.
<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
echo "output.php";
}
else
{
echo "wronguser.php";
}
}
?>">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method='post' action="<?php $SERVER['PHPSELF']; ?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
This is my html page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hello and welcome to my site</title>
</head>
<body>
<p>are these 2 numbers equal? type yes or no in the box</p>
<p2>one</p2> and eight
<form action="welcome_get.php" method="get">
Answer: <input type="text" name="name"><br>
<input type="submit">
</form>
</body>
</html>
this is my new php page containing a previous answer and im having new errors
<html>
<body>
<?php
var_dump($_GET['name']);
$answer = $_GET['name'];
$saying = "congratulations";
if ($answer == "yes"){
echo $saying;
}
?>
</body>
</html>
the new errors are referring to my php page which is welcome_get.php
var_dump($_GET['name']);
Additionally, to see all available GET params:
var_dump($_GET)
To assign to a new variable:
if(!empty($_GET['name'])){
$answer = $_GET['name'];
if($answer == 'something'){
// do something
}
}
You can use isset function to check if $_GET['var'] is set
if(isset($_GET['name'])){
$answer = $_GET['name'];
$saying = "congratulations";
if ($answer == "yes"){
echo $saying;
}
}
I have been working with PHP and MySQL for about two months and I recently began using forms. I have a main form-processing file:
if($_POST['checkforpothole'] == "yes"){
echo "thank you for your help"."<br/>";
echo "please enter in a latitude and longitude"."<br/>";
include 'FormForGettingLatAndLong.php';
$Lat = $_POST['latitude'];
$Long = $_POST['longitude'];
include 'MySQLSample.php';
echo addCoord($Lat, $Long);
include 'MySQLSample.php';
die();
}
if($_POST["checkforpothole"] == "no"){
echo "here is the table"."<br/>";
include 'MySQLSample.php';
die();
}
?>
The form I am attempting to access is as follows:
<?php
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="FormProcessing.php" method="post">
Latitude: <input type="text" name="latitude"><br>
Longitude: <input type = "text" name = "longitude"><br>
<input type="submit">
</form>
</body>
</html>
Here is the checkforpothole form:
<?php
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="FormProcessing.php" method="post">
Add a new pothole? (yes or no): <input type="text" name="checkforpothole"><br>
<input type="submit">
</form>
</body>
</html>
I am having trouble accessing latitude and longitude in this form, which is in a separate file. Is there a way to access an index from a second form after accessing from another form initially. Please let me know if there is a problem in the first program, specifically with the first if statement.
First of all, I think the logic of your code needs a bit of revision.
if($_POST['checkforpothole'] == "yes"){
echo "thank you for your help"."<br/>";
echo "please enter in a latitude and longitude"."<br/>";
include 'FormForGettingLatAndLong.php';
$Lat = $_POST['latitude'];
$Long = $_POST['longitude'];
include 'MySQLSample.php';
echo addCoord($Lat, $Long);
include 'MySQLSample.php';
die();
}
if($_POST["checkforpothole"] == "no"){
echo "here is the table"."<br/>";
include 'MySQLSample.php';
die();
}
?>
As i concluded from your code it appears that you want to get additional information using a second form when user submitted the first form. and you're trying to do so by including the second form when you are processing the first form and then immediately reading the values of second form:
include 'FormForGettingLatAndLong.php';
$Lat = $_POST['latitude'];
$Long = $_POST['longitude'];
but this way, you won't have the Longitude and Latitude! because you just posted the second form to user to get that data! and when she posts back the second form, execution of your code will start from beginning (in a separate process). working with forms is not like conventional input procedures (like old "cin<<" , "fscanf()" or "System.console().readLine()"). instead you should print the second form and wait for user to submit that.
so you can do what you want using these three files:
index.html:
<!DOCTYPE HTML>
<html>
<body>
<form action="firstFormProcessing.php" method="post">
Add a new pothole? (yes or no): <input type="text" name="checkforpothole"><br>
<input type="submit">
</form>
</body>
</html>
firstFormProcessing.php:
<?php
if($_POST['checkforpothole'] == "yes"){
echo "thank you for your help"."<br/>";
echo "please enter in a latitude and longitude"."<br/>";
echo <<<END
<!DOCTYPE HTML>
<html>
<body>
<form action="secondFormProcessing.php" method="post">
Latitude: <input type="text" name="latitude"><br>
Longitude: <input type = "text" name = "longitude"><br>
<input type="submit">
</form>
</body>
</html>
END;
die();
}
if($_POST["checkforpothole"] == "no"){
echo "here is the table"."<br/>";
include 'MySQLSample.php';
die();
}
?>
secondFormProcessing.php:
<?php
echo "now i have Latitude=$_POST[latitude] and Longitude=$_POST[longitude]";
?>
P.S. it's my first Answer on stackoverflow. i hope this will help you and by the way sorry for my bad English.
I would take 2 files instead of messing on one page, this is how I would do it.
If this helps you then don't forget to mark the question as answered.
index.php
<?php
if($_SERVER['REQUEST_METHOD'] === "POST") {
if(isset($_POST['checkForPothole'])) {
if($_POST['checkForPothole'] == true) {
header("Location: addPole.php");
}
}
}
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="index.php" method="post">
Add a new pothole? (yes or no): <input type="text" name="checkForPothole"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
addPole.php
<?php
if($_SERVER['REQUEST_METHOD'] === "POST") {
if(isset($_POST['latitude']) && isset($_POST['longitude'])) {
if(!empty($_POST['latitude']) && !empty($_POST['longitude']) {
require_once("MySQLSample.php");
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
addCoord($latitude, $longitude); //guessing that this is a function in the mysqlsample file
}
}
}
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="FormProcessing.php" method="post">
Latitude: <input type="text" name="latitude"><br>
Longitude: <input type="text" name="longitude"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I'm trying to make a basic html form that passes information to a php object then adds said object to an array. It works to the point of passing the information from the form, to the object, and adding it to the array and displaying said object. However, when I try to add a second object to the array it only seems to replace the array with a new single element array rather then adding to it. Here is my code... any ideas?
index.php file:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Forms</title>
</head>
<body>
<h2>Add Data</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
<button type="submit" name="submit" value="client">Submit</button>
</form>
<?php
include_once 'clientInfo.php';
include_once 'clientList.php';
if ($_POST) {
$clientArray[] = new clientInfo($_POST["Fname"], $_POST["Lname"]);
}
if (!empty($clientArray)) {
$clientList = new clientList($clientArray);
}
?>
<p>go to client list</p>
</body>
</html>
clintInfo.php file:
<?php
class clientInfo {
private$Fname;
private$Lname;
public function clientInfo($F, $L) {
$this->Fname = $F;
$this->Lname = $L;
}
public function __toString() {
return $this->Fname . " " . $this->Lname;
}
}
?>
clientList.php file:
<?php
class clientList {
public function clientList($array) {
foreach($array as $c) {
echo $c;
}
}
}
?>
EDITED WORKING CODE WITH ANSWER
index.php file:
<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Forms</title>
</head>
<body>
<h2>Add Data</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
<button type="submit" name="submit" value="client">Submit</button>
</form>
<?php
if ($_POST) {
$testClient = new clientInfo($_POST["Fname"], $_POST["Lname"]);
echo $testClient . " was successfully made. <br/>";
$_SESSION['clients'][] = $testClient;
echo end($_SESSION['clients']) . " was added.";
}
?>
<p>go to client list</p>
</body>
</html>
clientList.php file:
<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>
Accessing session variables
</title>
</head>
<body>
<h1>
Content Page
</h1>
<?php
for ($i = 0; $i < sizeof($_SESSION['clients']); $i++) {
echo $_SESSION['clients'][$i] . " was added. <br/>";
}
?>
<p>return to add data</p>
</body>
</html>
The object file clientInfo.php stayed the same. The objects needed to be stored in a multidimensional $_SESSION array and recalled with a for loop, a foreach loop would not work, unless some one else knows a way to make a foreach loop work, stick to a for. On a side note the $testClient variable could be skipped and just created and placed with in the $_SESSION at the same time, however doing it with the temp variable made it easier to trouble shoot and see how to make it work. Just thought I'd post the working code with the answer supplied by Josh!
You need to add persistance to your array object.
See PHP's $_SESSION.
If you don't store it to memory or disk in between requests, it cannot possibly exist on successive loads. There is a nice tutorial in that link that should get you up and running.
Alternatively, you can store things in a database, for larger needs.