It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm making a calculator-like web application that asks for user names, then displays their names on the next page and adds their score. I have three problems:
The user names won't display on the second page
It won't add the score
Whenever you enter a score for player 1, player 2's score is reset to zero, and vise-versia
You can access the page at: ripdvd.x10.mx/index.php
Thanks in advance!
First page:
<!DOCTYPE html>
<html>
<head>
<title>Select Players</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="scoregen.php">
<p class="par">
<label for="player1">Please type in the one of the players first name:</label>
<input type="text" id="player1" name="player1" size="17" maxlength="17" class="textbox" />
</p>
<p class="par">
<label for="player2">Please type in another players first name:</label>
<input type="text" id="player2" name="player2" size="17" maxlength="17" class="textbox" />
</p>
<input type="submit" class="button" name="button" value="Start Playing!" />
</form>
</body>
</html>
Second page(displays scores and provides a way to update them)
<?php
// Get data from HTML form.
//Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$addScore1 = $_POST['addScore1'];
$addScore2 = $_POST['addScore2'];
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
$curr1=$_COOKIE["score1"]+$addScore1;
$curr2=$_COOKIE["score2"]+$addScore2;
setcookie("score1", $_COOKIE["score1"]+$addScore1, time()+3600);
setcookie("score2", $_COOKIE["score2"]+$addScore2, time()+3600);
//Reset cookies if reset button is 't', which makes it clear scores
if ($clse = t){
setcookie ("score1", "", time() - 3600);
setcookie ("score2", "", time() - 3600);
}
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action=" ">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $oldscore1; ?>" />
<input type="submit" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
<input type="submit" class="button" value="Add Score!"/>
</form>
</p>
<form method="post" action=" ">
<input type="hidden" name="clsc" id="clsc" value="t" />
<input type="submit" class="reset" value="Clear Scores" />
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
I made several changes to your code (including the removal of cookies - hope that's ok). If you can live without cookies, try this:
<?php
// Get data from HTML form.
// Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
if ($_POST['addScore1'] == null)
$addScore1 = 0;
else
$addScore1 = $_POST['addScore1'];
if ($_POST['addScore2'] == null)
$addScore2 = 0;
else
$addScore2 = $_POST['addScore2'];
if ($_POST['oldScore1'] == null)
$oldScore1 = 0;
else
$oldScore1 = $_POST['oldScore1'];
if ($_POST['oldScore2'] == null)
$oldScore1 = 0;
else
$oldScore2 = $_POST['oldScore2'];
$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;
if ($_POST['clse'] == "t"){
$curr1 = 0;
$curr2 = 0;
}
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="submit" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
<input type="submit" class="button" value="Add Score!"/>
</p>
<input type="hidden" name="player1" value="<?PHP echo $player1;?>" />
<input type="hidden" name="player2" value="<?PHP echo $player2;?>" />
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="clsc" id="clsc" value="t" />
<input type="submit" class="reset" value="Clear Scores" />
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
Note, I haven't actually ran this, but try it and let me know if you get any errors or still have problems.
Try this one:
index.php
<!DOCTYPE html>
<html>
<head>
<title>Select Players</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="scoregen.php">
<p class="par">
<label for="player1">Please type in the one of the players first name:</label>
<input type="text" id="player1" name="player1" size="17" maxlength="17" class="textbox" />
</p>
<p class="par">
<label for="player2">Please type in another players first name:</label>
<input type="text" id="player2" name="player2" size="17" maxlength="17" class="textbox" />
</p>
<input type="submit" class="button" name="names" value="Start Playing!" />
</form>
</body>
</html>
scoregen.php
<?php
// Get data from HTML form.
//Gets the player names
if(isset($_POST['player1'])){
$player1 = $_POST['player1'];
}else{
$player1 = "No Player";
}
if(isset($_POST['player2'])){
$player2 = $_POST['player2'];
}else{
$player2 = "No Player";
}
//CHECKS IF THE ADDSCORE BUTTON IS HIT
if(isset($_POST['AddScore'])){
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
if(!empty($_POST['addScore1'])){
$addScore1 = $_POST['addScore1'];
}else{
$addScore1 = 0;
}
if(!empty($_POST['addScore2'])){
$addScore2 = $_POST['addScore2'];
}else{
$addScore2 = 0;
}
}else{//INITIALIZES THE SCORES TO ZERO; OR SETS IT TO ZERO IF THE CLEAR SCORES BUTTON IS HIT
$oldScore1 = 0;
$oldScore2 = 0;
$addScore1 = 0;
$addScore2 = 0;
}
$curr1 = $oldScore1 + $addScore1;
$curr2 = $oldScore2 + $addScore2;
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action=" ">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="submit" name="AddScore" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
<input type="submit" name="AddScore" class="button" value="Add Score!"/>
</p>
<input type="hidden" name="player1" value="<?php echo $player1;?>"/>
<input type="hidden" name="player2" value="<?php echo $player2;?>"/>
<p class="par">
<input type="submit" class="reset" value="Clear Scores" />
</p>
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
1) I did not use cookies here, you don't actually need it for the scores, you can use to save the usernames though.
2) You can actually just have one addScore button, but in this code it doesn't matter. it add the scores anyway.
3) I always check if a an element of the POST is set or if it has a value or not. So i put some checking for the scores and the names.
4) In this line of your code:
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
i changed the $oldscore to $curr, because the $curr score would be needed in the next submit as the oldscore so the score would add up.
$oldScore1 does not equal $oldscore1
and in any case you need to keep the new values to post to the next page
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
Drop the cookies and just use
$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;
It's also worth checking if the posts are set to avoid warnings, and making sure they are integers, e.g.
$oldScore1 = isset($_POST['oldScore1'])? (int)$_POST['oldScore1'] : 0;
$oldScore2 = isset($_POST['oldScore2'])? (int)$_POST['oldScore2'] : 0;
Related
I am working on a php situation that i have. What it should do is like. each product has a unique id value. if the product id is posted throught submit button, A count action should be activited to count the number of time that product id is posted, so increment.
I hope i exposed the situaation clearly. This is the way i thought doing it, but can't get it work:
<?php
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do;
if ($do > 1)
{
$i= $do+1;
echo $i;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/orange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="2" />
<input type="hidden" id="price" name="price" value="25" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="3" />
<input type="hidden" id="prrice" name="price" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
If I assume the value in the hidden input #id is the times users vote the product:
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do++;
}
If I assume it incorrectly I don't understand what you want to do, sincerly.
I would like to know how to increase a element, each time that element is posted.
I have to use for loop for the auto increment, but i am not getting right. So any advise or guidance will be great.
Here is the way i have tried to do:
Thanks
<?php
$id=0;
if (isset($_POST['submit'])) {
$do = $_POST['prodCode'];
$di = count($do);
while ($di > $id) {
$id++;
echo $id;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Session test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/bestorange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="f102" />
<input type="hidden" id="prodPrice" name="prodPrice" value="25" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="W122" />
<input type="hidden" id="prodPrice" name="prodPrice" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
Try below code, counts are stored in session, but for real life app you should use database and also you should get your products from database:
<?php
// initialize counts for f102 and W122 products
if (!isset($_SESSION['count_f102']) {
$_SESSION['count_f102'] = 0;
}
if (!isset($_SESSION['count_W122']) {
$_SESSION['count_f102'] = 0;
}
if (isset($_POST['submit'])) {
$do = $_POST['prodCode'];
// increment count for product which was submitted
$_SESSION['count_'.$do] = 1+ (int) $_SESSION['count_'.$do];
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Session test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/bestorange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="f102" />
<input type="hidden" id="prodPrice" name="prodPrice" value="25" />
<input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_f102'] ?>" size="1" readonly="readonly" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="W122" />
<input type="hidden" id="prodPrice" name="prodPrice" value="1" />
<input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_W122'] ?>" size="1" readonly="readonly" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
Are your $_POST['prodCode'] always in the form one letter + id ? If yes maybe this could help :
if (isset($_POST['prodCode'])) {
$value = $_POST['prodCode'];
// save the letter
$letter = substr($value, 0, 1);
// get id
$id = (int) substr($value, 1, strlen($value) - 1);
// get value with letter and incremented id
$valueIncremented = $letter . ++$id;
}
// with $_POST['prodCode'] = 'f102' you will get $valueIncremented = 'f103'
Hope it helps.
i'm trying to make CMS with PHP
i need to get the if from post request
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
how can i make this input's value = id
<input type="hidden" name="id" />
on control page
public function Update()
{
/*
1-get data into variables
2-validation
3-Database
*/
if(isset($_GET['id']) && (int)$_GET['id']>0)
{
$id = (int)$_GET['id'];
$user = $this->videoModel->Get_By_Id($id);
print_r($user);
echo
'
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>
';
}
else
{
if(isset($_POST['addVideo']))
{
$id = $_POST['id'];
echo "5ara" ;
$title = $_POST['title'];
$desc = $_POST['desc'];
$url = $_POST['ytl'];
//Validation
$vid = $this->getVid($url); //video id -_-
$data = array(
'title' => $title,
'desc' => $desc,
'vid' => $vid
);
if($this->videoModel->Update($id,$data))
{
System::Get('tpl')->assign('message','User Updated');
System::Get('tpl')->draw('success');
}
else
{
System::Get('tpl')->assign('message','Error Updating User');
System::Get('tpl')->draw('error');
}
}
else
{
System::Get('tpl')->assign('message','NO USER CHOSEN');
System::Get('tpl')->draw('error');
}
}
}
Quite simple really. As you checked it exists and moved it into a variable you can just echo the $id into the value attribute of that input tag.
<input type="hidden" name="id" value="' . $id . '" />
Using your code:
echo '
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" value="' . $id . '" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>';
Define the VALUE attribute :
<input type="hidden" name="id" value="ID_VALUE"/>
OR if you have id in varible than use below code :
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
OR
As you are using the HTML in PHP than:
<input type="hidden" name="id" value="'.$id.'"/>
ID_Value should be your id like 1,2,3..etc.
And Get the id on Action page :
$id = $_POST['id'];
Apart from this you have mention POST in form method and on your
action page you are trying to get the values using GET method,
which is wrong.
Edited due to question edit.
<input type="hidden" name="id" value="'.$id.'" />
You echo a big block of text, this way you can concatenate the $id by stopping block output, including $id and resuming block output.
How would i get the $frompage variable to send to the page it is posting to here is what i thought:
<link rel="stylesheet" type="text/css" href="style1.css" />
<?php
$frompage = $_SERVER['HTTP_REFERER'];
echo '<form name="form1" method="post" action="report.php">';
echo "What is Wrong?";
echo '<textarea style="resize: none;" name="message" cols="70" rows="10" id="message"> </textarea>';
echo'<input type="hidden" name="$frompage" value="$frompage">';
echo '<input type="submit" name="Submit" value="Submit">';
echo "</form>";
?>
If you change this line:
'<input type="hidden" name="$frompage" value="$frompage">'
into this:
'<input type="hidden" name="frompage" value="$frompage">'
when the user send the data, you can retrieve it using:
$_POST['frompage']
Keep it simple, no need to echo all of the HTML.
<link rel="stylesheet" type="text/css" href="style1.css" />
<form name="form1" method="post" action="report.php">
<label>What is Wrong?</label>
<textarea style="resize: none;" name="message" cols="70" rows="10" id="message"> </textarea>
<input type="hidden" name="frompage" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
<input type="submit" name="Submit" value="Submit" />
</form>
First your using too much echos :P this is how I would do it.
<link rel="stylesheet" type="text/css" href="style1.css" />
<?php
$frompage = $_SERVER['HTTP_REFERER'];
?>
<form name="form1" method="post" action="report.php">
What is Wrong?
<textarea style="resize: none;" name="message" cols="70" rows="10" id="message"> </textarea>
<input type="hidden" name="<?php echo $frompage" ?> value="<?php echo $frompage" ?>>
<input type="submit" name="Submit" value="Submit">
</form>
When you use echo ''; The single quotes forces everything to be read exactly as it stated. If you REALLY wanted to echo all that html, use double quotes.
Such as echo "Hello $frompage";
When I input numeric value in Number 1 and Number 2, and press "Add". It does not display the total added value. Please see my coding below. and advice me, what to is the problem, and what can be done.
<html>
<head>
<title>Simple Calculator</title>
<?php
if(isset($_POST['submitted'])){
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
$add = ($_POST['number1'] + $_POST['number2']);
echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
}
}
?>
<script type="text/javascript">
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>
<input type="button" name="add" value="Add" />
<input type="button" name="minus" value="Minus" />
<input type="button" name="multiply" value="Multiply" />
<input type="button" name="divide" value="Divide" />
<input type="reset" name="rest" value="Reset" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
You are echoing the result data into the <head>, so it will not be displayed.
You forgot to echo $add.
Your <input>s are of type button and not submit, so the form will not be submitted to the server.
Because you are echoing the previously entered values into the form, <input type="reset"> will probably not do what you want/expect it to do. I think it would be better to implement this as another submit.
Because this form affects only what the next page displays and does not make a permanent change to the server, you should use the GET method and not POST.
Try this:
<html>
<head>
<title>Simple Calculator</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="get">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
<input type="submit" name="add" value="Add" />
<input type="submit" name="minus" value="Minus" />
<input type="submit" name="multiply" value="Multiply" />
<input type="submit" name="divide" value="Divide" />
<input type="submit" name="reset" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</form>
<?php
if (isset($_GET['submitted']) && !isset($_GET['reset'])) {
echo "<div>";
if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {
if (isset($_GET['add'])) {
$result = $_GET['number1'] + $_GET['number2'];
echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;
} else if (isset($_GET['minus'])) {
$result = $_GET['number1'] - $_GET['number2'];
echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;
} else if (isset($_GET['multiply'])) {
$result = $_GET['number1'] * $_GET['number2'];
echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;
} else if (isset($_GET['divide'])) {
$result = $_GET['number1'] / $_GET['number2'];
echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;
}
} else {
echo "Invalid input";
}
echo "</div>";
}
?>
</body>
</html>
The solution of DaveRandom works fine if you change this
action="simple_calculator.php"
by
action="<?php echo $_SERVER['PHP_SELF'] ?>"