Undefined variable: POST in C:\wamp\www\forms.php - php

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;
?>

Related

Is the default value of a hidden element in html 1?

<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.

getting sum of textbox 1 and 2 into textbox 3

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.

Displaying text after space in PHP

I have my website connected to a database with MySQL and I am echoing out the data but it doesn't display information after a space. E.g. in the database 'Hello there' would just be 'Hello'.
I know this is quite a common question asked but I just couldn't get mine working. An example is this:
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>
You're missing quotes around your HTML attribute value. Without it the first word is considered the value and everything else is considered a new HTML attribute.
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>
Should be:
County: <input type = "text" name = "county" value = "<?php echo $row["county"]; ?>"> <br><br>
//^ ^

Query execution php script in the same page

I have this form html code in update.php. For updating, it's required to link to another page save_seeker.php with mysql update script for it to be executed. Is there any way to execute the script in same page on submitting form so that after query execution it remains on same page?
<form action= "save_seeker.php" method = "post">
Update details !<br><br>
First Name
<input type = "text" name = "fname" value = "<?php echo $disp['fname'];?>"><br><br>
Last Name
<input type = "text" name = "lname" value = "<?php echo $disp['lname'];?>"><br><br>
Contact number
<input type = "text" name = "contact" value = "<?php echo $disp['contact'];?>"><br><br>
Email-id
<input type = "email" name = "email" value = "<?php echo $disp['email'];?>"><br><br>
Address
<input type = "text" name = "address" value = "<?php echo $disp['address'];?>"><br><br>
Experience
<input type = "number" name = "experience" value = "<?php echo $disp['experience'];?> "><br><br>
Qualification
<input type = "text" name = "qualification" value = "<?php echo $disp['qualification'];?>"><br><br>
<input type = "Submit" value = "Update">
</form>
You could do the processing in the same file by adding this into the form action.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<!-- Input fields in here-->
<input type="submit" name="form_submit" value="Submit">
</form>
Now check for the submit action by checking if it is set in the post variable
<?php
if ( isset( $_POST['form_submit'] ) ) {
// Do processing here.
}
?>
You could use the include('page-with-update.php') , call the update function and use the $_SERVER["PHP_SELF"].
I hope helped!!

Adding a link to a HTML 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 &nbsp&nbsp&nbsp&nbsp&nbsp
<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");

Categories