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";
Related
I have created a form for payment in html. The values in the form are added automaticaly usng session. when the user logs in, the values are added using the user infromation. my html is below:
<table>
<form name="postForm" action="form_process.php" method="POST" >
<tr><td><input type="hidden" name="txnid" value="<?php echo $txnid=time().rand(1000,99999); ?>" /></td></tr>
<tr><td>amount</td><td><input type="text" name="amount" value="<?php echo $_SESSION['amount']; ?>" /></td></tr>
<tr><td>firstname</td><td><input type="text" name="firstname" value="<?php echo $_SESSION['firstname']; ?>" /></td></tr>
<tr><td>email</td><td><input type="text" name="email" value="<?php echo $_SESSION['email']; ?>" /></td></tr>
<tr><td>phone</td><td><input type="text" name="phone" value="<?php echo $_SESSION['phone']; ?>" /></td></tr>
<tr><td>productinfo</td><td><input type="text" name="productinfo" value="<?php echo $_SESSION['productinfo']; ?>" /></td></tr>
<tr><td><input type="hidden" name="surl" value="success.php" size="64" /></td></tr>
<tr><td><input type="hidden" name="furl" value="fail.php" size="64" /></td></tr>
<tr><td><input type="submit" /></td><td><input type="reset" /></td></tr>
</form>
</table>
in the login page, i have stored the session as below:
<?php
session_start();
include("config.php");
if(isset($_POST['login']))
{
$user_email=$_POST['email'];
$user_pass=$_POST['pass'];
$check_user="select * from users WHERE user_email='$user_email'AND user_pass='$user_pass'";
$run=mysqli_query($dbcon,$check_user);
if(mysqli_num_rows($run)>0)
{
$result=mysqli_fetch_array($run);
$_SESSION['email']=$user_email;
$_SESSION['access']=$result['access'];
//here session is used and value of $user_email store in $_SESSION.
echo "<script>window.open('index.php','_self')</script>";
}
else
{
echo "<script>alert('Email or password is incorrect!')</script>";
}
}
?>
<html>
<head lang="en">
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
<title>Login</title>
<link rel="icon" type="image/png" href="images/imageedit_2_5125240109.gif"/>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/appointment_style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/font-awesome.css" rel="stylesheet">
<!-- //for bootstrap working -->
<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,300i,400,400i,600,600i,700" rel="stylesheet">
</head>
<style>
.login-panel {
margin-top: 150px;
}
</style>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Sign In</h3>
</div>
<div class="panel-body">
<form role="form" method="post" action="login.php">
<fieldset>
<div class="form-group" >
<input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="pass" type="password" value="">
</div>
<input type="submit" value="login" name="login" >
<!-- Change this to a button or input when using this as a form -->
<!-- Login -->
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
the user email is correctly inputted in the form , but the mobile number and other details are not being added into the form. i tried string them as variables like above, but its not displaying. i think i need to fetch the details from database. can anyone tell me how to do this?
I have done alot of research and finally go the answer myself.
i used the $_SESSION and the select query to get the information of the current user from the database. and used the while loop to put them in variable.
the updated code is below:
<div class="sing"><?php
session_start();
if (isset($_SESSION['email']) && $_SESSION['email'] == true) {
echo "   Welcome " . $_SESSION['email'] ;
echo "<div style='float: right;'><a href='logout.php'>Logout</a> </div>";
} else {
}
?>
</div>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<h2>Payment Gateway Testing Sample</h2>
<h3>Fill the form and submit it for starting the transaction...</h3>
</div>
<div>
<?php
include("config.php");
$user_email=$_SESSION['email'];
$check_user="select * from users WHERE user_email='$user_email'";
$run=mysqli_query($dbcon,$check_user);
while($row = $run->fetch_assoc())
{
$user_name=$row['user_name'];
$user_mobile=$row['user_mobile'];
}
//here session is used and value of $user_email store in $_SESSION.
?>
<table>
<form name="postForm" action="form_process.php" method="POST" >
<tr><td><input type="hidden" name="txnid" value="<?php echo $txnid=time().rand(1000,99999); ?>" /></td></tr>
<tr><td>amount</td><td><input type="text" name="amount" value="" /></td></tr>
<tr><td>Name</td><td><input type="text" name="firstname" value="<?php echo $user_name; ?>" /></td></tr>
<tr><td>email</td><td><input type="text" name="email" value="<?php echo $_SESSION['email']; ?>" /></td></tr>
<tr><td>phone</td><td><input type="text" name="mobile" value="<?php echo $user_mobile; ?>" /></td></tr>
<tr><td>productinfo</td><td><input type="text" name="productinfo" value="" /></td></tr>
<tr><td><input type="hidden" name="surl" value="success.php" size="64" /></td></tr>
<tr><td><input type="hidden" name="furl" value="fail.php" size="64" /></td></tr>
<tr><td><input type="submit" /></td><td><input type="reset" /></td></tr>
</form>
</table>
</div>
</body>
</html>
i post data into form action to php self some characters make errors. This charactor are " or \ if u put " input name return into \ how to fix it
<form id="form1" name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<label for="name"></label>
<input name="name" type="text" id="name" value='<? print htmlspecialchars($_POST['name']); ?>' />
<br />
<input type="submit" name="button" id="button" value="Submit" />
<? print htmlspecialchars($_POST['name']); ?>
</form>
Im not exactly sure what the problem is but I think you are getting errors when returning special characters into an input field. If so try this: http://php.net/manual/en/function.htmlentities.php you will need to convert the special characters into html entities so the data isnt read as code which will cause issues.
I have just tested this by changing the message input to <?php echo htmlentities($_POST['name']); ?> and the characters are displaying as normal.
Use this code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name"></label>
<input name="name" type="text" id="name" value="<?php echo htmlentities($_POST['name']); ?>" />
<br />
<label for="message"></label>
<textarea name="message" id="message" cols="45" rows="5"><?php echo $message; ?></textarea>
<br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
echoed (<?php echo htmlentities($_POST['name']); ?>);
</body>
</html>
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;
I can't seem to figure out how to make this script write data to the section of the file. Here is an excerpt of the code that applies:
$file = $url.'.php';
if (!$file_handle = fopen($file,"a+"))
{
echo $lang['cannot_open_file'];
}
if (!fwrite($file_handle,stripslashes(html_entity_decode($data))))
{
echo $lang['file_is_not_writable'];
}
fclose($file_handle);
echo ($lang['success']);
echo ('<meta http-equiv="Refresh" content="4;url='.$url.'.php" />');
}
}
?>
<form action="<?php echo $url;?>.php" method="post">
<?php echo $lang['name']?> <span style="font-weight:100;">(<?php echo $lang['required']?>)</span>:<br />
<input class="textfield" type="text" name="name" value="" /><br />
<?php echo $lang['website']?> <span style="font-weight:100;">(<?php echo $lang['without_http'];?>)</span>:<br />
<input class="textfield" type="text" name="site" value="" /><br />
<?php echo $lang['message']?>:<br />
<textarea class="textarea" rows="2" cols="25" name="message"></textarea><br />
<img src="captcha-image.php" alt="Image verification" /> <br />
<?php echo $lang['value_from_the_image'];?>:<br />
<input class="textfield" type="text" name="img_ver" value="" /><br />
<input type="reset" name="reset" value="<?php echo $lang['reset'];?>" />
<input type="submit" name="send" value="<?php echo $lang['send'];?>" />
</form>
<hr>
<h3>Comments:</h3>
<!--comments -->
<?php include('../templates/footer.php'); ?>
Would anyone mind helping me out on this one? It would be much appreciated.
Use file get contens to retrieve the contens first.
Then use preg_replace to find where or what you want to be replaced.
Then rewrite the file with the new content.
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'] ?>"