writing program, where the computer guesses a number that user chooses -PHP - php

user doesn't need to submit a number etc. Just think one in their mind.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>guess_game</title>
</head>
<body>
>if guess is too low, user presses 'low button' (same with high button)
<p>think of a number, 1-100.</p>
<form method="post">
<button type="submit" name="low">too low</button>
<button type="submit" name="high">too high</button>
<button type="submit" name="match">it's a match</button>
</form>
<?php
if (isset($_POST['low']))
{
>store $guess to be lowest number range of guess
$low = $guess;
}
else{$low=1;}
if (isset($_POST['high']))
{
$high = $guess;
}
else{$high=100;}
if (isset($_POST['match']))
{
print "thank you for playing";
}
?>
<form method="post">
<input type="hidden" name="hLow" id="hLow" value="<?php $low ?>">
>store $low,High vars to hidden field
<input type="hidden" name="hHigh" id="hHigh" value="<?php $high ?>">
</form>
<?php
>place values stored in hidden back to php var.
$hlow=$_POST['hLow'];
$hhigh=$_POST['hHigh'];
$guess=rand($hlow,$hhigh);
print "is the number $guess";
?>
</body>
</html>
so far this code won't pass any value to hidden field.($guess is always 0)
I want $guess to be random numbers between $low and $high(that are stored in hidden)

You aren't putting $guess into the form. Put this in after you have calculated $guess.
<input type="hidden" name="hGuess" id="hGuess" value="<?php $guess ?>">

Try:
<?php
$guess = 'Think of a number between 1 and 100.';
if(isset($_POST['low']) || isset($_POST['high'])){
$guess = 'The Computer will have to guess again.'
}
elseif(isset($_POST['match'])){
$guess = 'The Computer just read your mind....Not.';
}
?>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<title>GUESSING GAME</title>
<style type='text/css'>
#import 'common.css'; #import 'game.css';
</style>
</head>
<body class='njs'>
<p><?php echo $guess;?></p>
<form method='post' action='<?php echo $_SERVER['PHP_SELF'];?>'>
<input type='submit' name='low' value='too low' />
<input type='submit' name='high' value='too high' />
<input type='submit' name='match' value='it's a match' />
</form>
<script type='text/javascript' src='common.js'></script>
</body>
</html>

Related

I have problems with "echo" empty in php

I tell you, I have a form where only email addresses are loaded
The idea is that when a user enters for example pepito#pepito.com the page says, "you enter pepito#pepito.com" the problem I have with the echo is that when you enter the page, you show me input "you income ", how can I eliminate that?
<?php echo "you income $email"; ?>
I created a simple example for it
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<?php echo !empty($_POST['email']) ? 'you enter ' . $_POST['email'] : ''; ?>
<form action="" method="post">
<input name="email" placeholder="somebody#example.com" />
<input type="submit">
</form>
</body>
</html>
I am using the ternary operator after check the email parameter with empty()
it will check if form is submitted or not and if submitted it will echo the email.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<?php
if(isset($_POST['submit'])) {
echo "you entered ". $_POST['email'];
}
?>
<form action="" method="post">
<input name="email" type="email" placeholder="somebody#example.com" required
placeholder="Enter Email">
<input type="submit" name="submit">
</form>
</body>
</html>

Pass session values to other page

I have two pages: order.php and checkout.php. I have 3 items in the order page and I want to pass quantity of the items to the checkout page.
I guess the problem is with isset($_POST['Submit']). My guess is that it still goes straight to the checkout page when I press submit without putting values to session variables.
I have been trying to pass the values from order like this:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="checkout.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
And here in checkout I'm trying to print one session value as test:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
print ($_SESSION["maara1"]);
?>
</body>
</html>
"I guess the problem is with isset($_POST['Submit'])."
Yes, the problem is with if (isset($_POST['Submit']))
Your submit button is named submit instead of Submit.
<input type="submit" name="submit" value="Order"/>
Change it to
<input type="submit" name="Submit" value="Order"/>
They are case-sensitive.
Or leave it the way it is and change
if (isset($_POST['Submit']))
to
if (isset($_POST['submit']))
Either method will work. You just need to make them both (letter cases) match.
What is happening is, PHP is looking for a named element called Submit.
That alone would have and should have thrown:
Undefined index Submit...
Sidenote:
I noticed:
<input type=hidden name=price1 value=5>
and other inputs.
I would suggest that you use quotes around it:
<input type="hidden" name="price1" value="5">
while doing the same for the others, as it could have adverse effects and/or unexpected results.
I have seen that happen before.
Edit:
You'll need to move this whole block in your second page and not be in the first page.
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
then do print ($_SESSION["maara1"]); from there.
Your first page does not recognize the POST variables because they have not been set.
Edit #2:
You could try setting a value value="{$_SESSION["maara1"]}" to your inputs.
I.e.:
Sidenote: You could try <form action="" method="post"> instead of <form action="checkout.php" method="post">
However, I'm unsure if the following is what you're looking to get. It does work if action="" but it won't work trying to get a value before it has been set. That's not how sessions work.
It's kind of like expecting an A+ in a test you haven't written yet, if I can say.
echo <<<END
<form action="checkout.php" method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" value="{$_SESSION["maara1"]}" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="Submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])){
$_SESSION["maara1"] = $_POST["maara1"];
$mar1 = $_SESSION["maara1"];
echo $mar1;
}
But as you said in a comment: "what is the point of me using session variables on second page then if I can refer them from $_POST anyways?"
A: Exactly.
I went and did it like this. With two button and it is also more close to webstore now. So I press first button to add the quantity of the items and at the same time it sets session variables. Then I press second button to proceed to the second page which is checkout. What you guys think?
First page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="teht7.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Fridge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Valitse tuotteet"/></p>
</form>
<hr />
<form action="teht7_kassa.php"
method="post">
<p><input type="submit" name="submit" value="Siirry kasssalle"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['submit'])) {
$ostostenmaara = $_POST["maara1"] + $_POST["maara2"] + $_POST["maara3"];
print ("Ostoskorissa on: $ostostenmaara tuotetta");
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
Second page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
$maara1 = $_SESSION["maara1"];
$maara2 = $_SESSION["maara2"];
$maara3 = $_SESSION["maara3"];
$summa = $_POST["price1"]*$maara1+$_POST["price2"]*$maara2+$_POST["price3"]*$maara3;
print ("Ostostesi yhteissumma on: $summa euroa");
?>
</body>
</html>

Input a number range to mysql database

how to input a number range, from 2 text fields, this is my code is seems to be not working, the range function i want it to be inserted to mysql database but this code is just to test if i can get the 2 textfields to connect to the range function. anyone can help me solve this problem?
<?php
$from = '.from';
$to = '.to';
$number = range('$from','$to');
print_r ($number);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Waybill</title>
<link rel="shortcut icon" href="../img/icon.ico">
<link rel="stylesheet" type="text/css" href="../css/forms.css" />
</head>
<body>
<form id="form3" name="form1" method="post" action="">
<p>From:
<input type="text" name="from" id="form_number" class="from" value="0"/>
- To:
<input type="text" name="to" id="form_number" class="to" value="50" />
</p>
<p>Waybill Booklet:
<select name="waybill_booklet" id="form_list">
</select>
</p>
<p>
<input type="submit" name="button" id="form_button" value="OK!" />
</p>
</form>
</body>
</html>
Replace your php code with this:
<?php
if (!empty($_POST)) {
$from = $_POST['from'];
$to = $_POST['to'];
$number = range($from,$to);
print_r($number);
}
?>
To access a form post in PHP you use the $_POST superglobal. More info found here: http://www.php.net/manual/en/reserved.variables.post.php.
I've put a check to see if it's empty or not at the start, so when the page first loads it won't attempt to run the PHP code. Presumably you'll then replace the print_r with whatever you need to insert the data into your database.

Phonebook in php without using MySQL

I just wanted to create a simple phonebook using php...I used the following code....but one entry overwrites another plz help...I want to do this without using MySQL
<?php
session_start();
if(isset($_SESSION['views']))
{
$_SESSION['views']=$_SESSION['views']+1;
}
else
{
$_SESSION['views']=1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Phonebook</title>
</head>
<body>
<form action="index1.php" method="post" style="border:thin">
Name: <input type="text" name="varname" style="border:dotted" />
<br/>
Roll Number:<input type="text" name="varroll" style="border:dotted" />
<br />
Phone Number: <input type="text" name="varno" style="border:dotted" />
<br/>
<input type="submit" name="submit" value="Register" /><br/>
</form>
<?php
$test1[$_SESSION['views']]=$_POST['varname'];
$test2[$_SESSION['views']]=$_POST['varroll'];
$test3[$_SESSION['views']]=$_POST['varno'];
for($j=1;$j<=$_SESSION['views'];$j++)
{
echo $test1[$j]." ".$test2[$j]." ".$test3[$j];}
echo "<br/>";
echo "No. of page views=".$_SESSION['views'];
?>
</body>
</html>
You could write it to a text file with | seperating each value or you could use an ini or xml file
You cannot just use $_SESSION, as it will be emptied when the user closes the browser.
Best to do something like this (sample untested code)
//loading
$data = unserialize( file_get_contents( 'mydata.txt' ) );
//editing
$entry = array();
$entry['roll']=$_POST['varroll'];
$entry['name']=$_POST['varname'];
$data[] = $entry;
//saving
file_put_contents( 'mydata.txt', serialize($data) );

How can save (multiple) entries from a html form to an array in php?

I have an HTML form with three fields, first name surname and mid. name and I want to save this information in a php page. The problem is I don't know how to save multiple entries in array without a connection of database.
HTML form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Εργασία 11 - Βασίλης Τρίγκας</title>
</head>
<body>
<h4>Καταχώριση βιβλίου</h4>
<form action="form_action.php" method="get">
Συγγραφέας <br/><input type="text" name="fnwriter" /><br/>
Τίτλος <br/><input type="text" name="title" /><br/>
Εκδότης<br/> <input type="text" name="editor" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP File
<?php
function display_welcome(){
print("Σωστή εισαγωγή, ");
print($_GET['fnwriter','title','editor'])
}
function display_empty_form(){
print <<<_HTML_
<FORM method="post" action="$SERVER['PHP_SELF']">
<BR/>
<INPUT type="submit" value="SUBMIT NAME">
</FORM>
_HTML_;
}
if ($_GET['fnwriter','title']) {
display_welcome();
}
else {
display_empty_form();
}
$Book=array("fnwriter","title","editor");
$fwriter=$_GET['$fnwriter']
$title=$_GET['$title']
$editor=$_GET['$editor']
echo $fnwriter;
echo $title;
echo $editor;
for ($i = 0; $i < count($fnwriter,$title,$editor); ++$i) {
echo "Book $i= $fnwriter[$i],$title[$i],$editor[$i]<br />";
}
//echo $Book[$_GET];
//print_r($);
?>
I don't really get your question, is that about saving data to a file, or about processing $_POST data?
If it's the first one, you have a plenty of possibilities, from simple CSV (or any other separator) to XML ;)

Categories