<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>persistence demo</title>
</head>
<body>
<h1>Persistence Demo</h1>
<form action = "" method = "post">
<?php
//load up variables
$hdnCounter = filter_input(INPUT_POST, "hdnCounter");
//increment the counters
$hdnCounter++;
print <<<HERE
<fieldset>
<input type = "text" name = "txtBoxCounter"/>
<input type = "hidden" name = "hdnCounter" value = "$hdnCounter"/>
<h3>The hidden value is $hdnCounter</h3>
<input type = "submit" value = "click to increment counters"/>
HERE;
?>
</fieldset>
</form>
</body>
</html>
The above program prints the hidden value as 1 on first page load. The hidden value increments by 1 with each form submission. Apparently, the counting part is done by the $hdnCounter++ which we did not assign any number to start with.
$hdnCounter++ is a part of the hidden element, so I'm thinking the default value of the hidden element must be 1 or how would we be able to increment it.
Welcome to PHP, where empty variables are equal to 0.
Related
I have three text boxes and I want to place a value in textbox 1 and 2,then add them together and get the result in textbox 3.With the below code I can get it to echo out onto the screen,but If I omit the echo then nothing happens after placing numbers in T1 andT2.
Can anybody tell me what I am doing wrong.
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="textentry2.php" method="POST">
<input type = "submit" name = "submit" value = "go">
<input type = "text" name = "text1" >
<input type = "text" name = "text2" >
<input type = "text" name = "text3" >
<?php
$text_entry = $_POST['text1'];
$text_entry2 = $_POST['text2'];
$text_entry3 = $_POST['text3'];
{
$text_entry3 = ($text_entry + $text_entry2);
echo ($text_entry3);
}
?>
nothing happens after placing numbers in T1 andT2
PHP is a server-side language. you can't expect from PHP to act real-time...
input values need to go to server, then PHP Server can calculate your values on server then after refresh, results comes...
if you want to write a real-time calculator, you need to write this with javaScript. absolutely you can do that with AJAX or anything else but javaScript is a easy and fast way for this.
try this:
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="textentry2.php" method="POST">
<input type="submit" name="submit" value = "go">
<input type="text" name="text1" onkeyup="calc()">
<input type="text" name="text2" onkeyup="calc()">
<input type="text" name="text3" >
</form>
<script>
function calc()
{
var elm = document.forms["myform"];
if (elm["text1"].value != "" && elm["text2"].value != "")
{elm["text3"].value = parseInt(elm["text1"].value) + parseInt(elm["text2"].value);}
}
</script>
</body>
</html>
For Strings:
$text_entry3 = ($text_entry.$text_entry2);
in php you combine strings with a . between them.
For Integer:
$text_entry3 = ((int)$text_entry + (int)$text_entry2);
You need to cast them as you get Strings from your from and they won't add together that easy without casting.
For Placing it:
as Aravona stated allready:
You should make all the calculation befor you output the html. Php is dynamic. HTML not. Thus you need to put the dynamic part into the static one
Make the <?php ... ?> in the beginnin and than use this to output the result:
<input type = "text" name = "text3" value="<?=$text_entry3;?>" >
I think your code would be like this:
<?php
$text_entry1 = (int)$_POST['text1'];
$text_entry2 = (int)$_POST['text2'];
$text_entry3 = ($text_entry1 + $text_entry2);
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="textentry2.php" method="POST">
<input type = "submit" name = "submit" value = "go">
<input type = "text" name = "text1" value = "<?php echo $text_entry1 ?>" >
<input type = "text" name = "text2" value = "<?php echo $text_entry2 ?>" >
<input type = "text" name = "text3" value = "<?php echo $text_entry3 ?>" >
</form>
</body>
</html>
Firstly put your PHP above your HTML so the values are defined.
Then:
<input type = "text" name = "text3"
value="<?php echo ((int)$text_entry + (int)$text_entry2); ?>">
A very good tips I tend to give those who are new to PHP, is to put all of your PHP code before any HTML. This means that you'll take care of all of your data processing, before you try to send the client anything. The beauty of that, is that if some erroneous condition appears, you can simply redirect or do whatever's necessary to provide the user with a clear and informative responce. Instead of just dumping a half-created page, with an ugly error (or several) in the middle of it.
Thus your PHP code should look something like this:
<?php
// First make sure that the variable is defined, to prevent notices
// when trying to use it later.
$answer = '';
// If something has been posted (submitted).
if (!empty ($_POST)) {
// Calculate the answer.
// TODO: Add some validation to make sure that the values are indeed numbers.
// See filter_var () in the PHP manual.
$answer = $_POST['text1'] + $_POST['text2'];
}
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myform" action="textentry2.php" method="POST">
<input type="submit" name="submit" value = "go">
<input type="number" name="text1" >
<input type="number" name="text2" >
<input type="number" name="text3" value="<?php echo $answer; ?>">
</form>
Normally I'd use htmlspecialchars() on $answer when echoing it, to prevent XSS attacks. However, since it only echo out a number it is not necessary in this case.
When running this PHP code it works, but doesn't return any results - can anyone tell me why please? I have tried checking the code, but as it runs it's not that obvious. So I think it has a problem with the echoing SQL statement.
<!doctype html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bed & Breakfast</title>
<style type = "text/css">
</style>
</head>
<body>
<form style = "text-align:center; margin-top:200px;"method="POST" action ="">
<span> Hotel Venue<span><input style ="margin-left:20px;" type="number" Name ="HotelVenueRef"/><br/><br/><br/>
<input style = "margin-left:100px;" type='submit' value='HotelVenueRef' Name ="HotelVenueRef"/>
</form>
<?php
if (isset($_POST['HotelVenueRef']))
{
require_once 'connecttodatabase.php';
$sql=" select * from`B&B` where Hotel_Venue_ref =" .$_POST['HotelVenueRef'];
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
echo'
<td align="center">'.$row['Delegate_Student_uid'] .' </td>';
echo'
<td align="center">'.$row['Name'] .' </td>';
echo'
<td align="center">'.$row['Event_uid'] .' </td>';
echo'
<td align="center">'.$row['Hotel_Venue_ref'] .' </td>';
}
}
?>
</body>
</head>
I know about the injection vulnerability, that's not a problem at the moment. Just need to get it working for now.
$sql=" select * from `B&B` where Hotel_Venue_ref = '" . mysql_escape_string($_POST['HotelVenueRef']) . "'");
$result = mysql_query($sql);
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
Add this code before while statement
I have a feeling that your issue is a result of the fact that you have 2 input elements with the same name (HotelVenueRef).
Here's your form, I've cleaned up the formatting and taken out some style attributes because they're irrelevant to the problem and it's cleaner without them. I've also lower-cased all attribute names (because consistency is key!!)
<form "method="POST" action ="">
<span>Hotel Venue<span>
<input type="number" name="HotelVenueRef"/>
<br/>
<br/>
<br/>
<input type="submit" value="HotelVenueRef" name="HotelVenueRef"/>
</form>
The issue is that you have both an <input type="number"/> and an <input type="submit"/> that have the same name.
When a form submits to PHP, the $_POST array is populated with key-value pairs, where the key is based off the name attribute of the html input element, and the value is the value of that input.
Let's say you set the number input to have a value of 5, and then submit the form.
PHP will now auto-generate the $_POST array. Behind the scenes, it does something like
$_POST['HotelVenueRef'] = 5;
Based off of the value in the number input. Then it goes and does something like
$_POST['HotelVenueRef'] = 'HotelVenueRef';
This is the part you haven't noticed! The submit button also submits to PHP as part of the form, and its value now overwrites the value you're interested in.
The reason you're not getting any results is that your query resolves to look like:
SELECT * FROM `B&B` WHERE `Hotel_Venue_ref`='HotelVenueRef'
With the value of the submit button, instead of the value of the number input.
You can solve your problem by removing the "name" attribute on the submit button, which will prevent its value from submitting to PHP along with the rest of the form. You also have the option of renaming your submit button.
I'm just getting into mysql and i am creating a basic registration page. However, when i click submit, i want to be directed to another page, say a page where you login, as many sites have.
How do i go about allowing the submit button to link to a new page. I know it'll be something simple, but i have looked and can't seem to find much on it.
This is the current code that i've tried:
<html>
<head>
<title>MySQL Test - Registration</title>
<script LANGUAGE="JavaScript">
function testResults() {
window.document.location.href = "http://www.google.com";
}
</script>
</head>
<body>
<form action = "rego.php" method = "POST">
Username:<br/>
<input type = "text" name = "username"/><br/>
Password:<br/>
<input type = "password" name = "password"/><br/>
Confirm Password:<br/>
<input type = "password" name = "passvery"/><br/>
Email:<br/>
<input type = "text" name = "email"/><br/><br/>
Please select your gender:<br/>
<input type = "radio" name = "gender" value = "male"/>Male      
<input type = "radio" name = "gender" value = "female"/>Female<br/><br/>
<input type = "checkbox" name = "agree" value = "agree"/>I agree to the terms and conditions.<br/>
<br/>
<input type = "submit" value = "Sign Up!" onclick = "javascript:testresults()" />
</form>
</body>
</html>
Form action="rego.php" means that your form will be submitted to that file.
If you want to go somewhere else after rego.php, you should redirect from that file, for example using the location header, before any output:
// do stuff (registration)
header("Location: another-php.php");
I have this simple form.
a.php
<html>
<head>
</head>
<body>
<?
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'text' name = 'name'>
<input type = 'submit' value = 'SEND' name = 'send'>
</form>
";
?>
</body>
</html>
a2.php
<?
$name = $_REQUEST ['name'];
echo $name;
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'submit' value = 'EDIT' name = 'edit'>
</form>
";
?>
How can I keep the value introduced when I click EDIT and i'm back to the first form?
Thanks.
EDIT 2: using hidden inputs
on a2.php just put another <input type="hidden" name="hidden_name" value="{$_POST['name']}" /> after you hit submit on a2.php (BTW for it to go back to a.php, you need to change the form action="a.php" on a2.php), a.php will have a $_POST['hidden_name'], that will contain the value from the first iteration.
EDIT: before you start handling $_SESSION variables, first initiate the session before any html output with a session_start() function.
Use a superglobal like $_SESSION so in your case you would need to fetch the incoming in a2.php $_SESSION['name'] = $_POST['name'] and refer to the $_SESSION['name'] in your a.php. Remember that $_SESSION['name'] will retain the last assigned value until the the session is terminated, I.e. browser window is closed.
You can read more in http://www.php.net/manual/en/reserved.variables.session.php
Also about session_start: http://www.php.net/manual/en/function.session-start.php
1.creating the hidden inputs in forms by using type="hidden"
The following code doesn't work. forms.php and forms.html are the file names, both saved in the root directory.
The error is "Undefined variable: POST in C:\wamp\www\forms.php". I know I'm not handling multiple inputs properly, either. Can anyone help with either of these?
<html>
<head>
<title>
Registration for Placement
</title>
</head>
<body>
<h1> Start your Placement process...NOW...</h1>
<form action = "forms.php" method = "POST">
<p> Name <input name = "Name" type = "Text" size = "20" maxlength = "30">
<p> Branch: CSE<input name = "branch" type = "radio" >
ECE<input name = "branch" type = "radio">
MEC<input name = "branch" type = "radio">
<p> Languages Known:English<input name ="lang[]" type = "checkbox" value = "1">
Hindi<input name = "lang[]" type = "checkbox" value = "2">
Tamil<input name = "lang[]" type = "checkbox" value = "3">
<p> State<select name = "state" size = "2">
<option> Jammu and Kashmir
<option> Delhi
<option> Tamil Nadu
<option> M.P
<option> U.P
<option> Maharashtra
</select>
<p> Thanks for submimitting the form
<p><input type = "Submit" value = "enter" >
<input type = "Reset" value = "clear" >
</form>
</body>
</html>
<html>
<head>
<title>Thank You</title>
</head>
<body>
<h1>Thank You</h1>
<p>Thank you for registering. Here is the information you submitted:</p>
<p>Name </p><p><?php echo $POST['Name']; ?></p>
<p>Branch </p><p><?php echo $POST["branch"];?></p>
<p>Lang </p><p><?php echo $POST["lang[0]"];?></p>
<p>STATE </p><p><?php echo $POST["state"];?></p>
</body>
</html>
Instead of $POST, use $_POST, more info:
http://php.net/manual/en/reserved.variables.post.php
you should write like this
$_POST['Name'] or
$_REQUEST['Name']
Don't know much but maybe the "double quotes" are creating a problem. Try using
$_POST['state'];
This might do the trick.
Its $_POST not $POST also you should be checking the variable is defined and set, also you should use htmlspecialchars() on user input when outputting to save you from a XSS vulnerability:
<?php
echo isset($POST['Name']) ? '<p>'.htmlspecialchars($_POST['Name']).'</p>' : null;
?>