"Undefined index" notice after submitting HTML form - php

So I have a sign up form, and there is some PHP at the top of the code. It gives me this error:
Notice: Undefined index: register in /home/content/04/7195304/html/index.php on line 20
This is line 20:
if ($_POST['register']) {
Here is the submit button:
<input type="submit" class="gobutton" value="Register" name="register"/>
Edit
So here's my form tags:
<form action="index.php" method="POST">

You should check it like this:
if ( isset($_POST['register']) ) {}
to avoid getting notice.

Is your form using method="GET" instead of method="POST"?
You can also add addition checks to make sure the index exists, like this:
if (isset($_POST['register'])) {
// do stuff
}
You can also debug the form submission like this:
var_dump($_POST);

Related

PHP-site - login error

I have been following this series: https://www.youtube.com/watch?v=hdZuhlQ88e8&list=WL&index=52. I have followed about 4 of his videoes, but at that point nothing really worked.. So I decided to start all over again, and this time i only got this far:
<?php
if($_POST['login'])
{
//get data
$userrname = $_POST['username'];
$password = $_POST['password'];
}
?>
<form action="login.php" method="POST">
Brukernavn:<br />
<input type="text" name="username"><p />
Passord:<br />
<input type="password" name="password"><p />
<input type="submit" name="login" value="Logg inn">
</form>
I got this error, and I can't figure out why..
"Notice: Undefined index: login in C:\wamp\www\hotellformidling\ny\login2.php on line 3"
Function: {main}( )
Location: ..\login2.php:0
Does anyone have any idea to what I can do?
Thanks:)
first do,
echo var_dump($_POST);
to check $_POST data,then do like below
if(isset($_POST['login']))
form action="login.php" method="POST"
In this line as action = "logoin.php", it will search for a file named login.php when you click the submit button. But I suppose, you have named your file login2.php. Change either one of them, so that there is same name on both the places [in form action and your file name].

embed html form to php code

I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks
The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.
Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...
I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you
Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}

Why am I getting this PHP error?

So here's my full code
<!DOCTYPE html>
<html>
<body>
<h1>Encrypt</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter word to encrypt<input type="text" name="in">
<input type="submit">
<hr>
</form>
<h1>Decrypt</h1>
<form>
Enter word to decrypt<input type="text" name="out">
<input type="submit">
<hr>
</form>
</body>
</html>
<?php
$encrypt = $_POST['in'];
?>
And here's the error I get
Notice: Undefined index: in in /Users/idrisk/Colourity/si/index.php on line 20
Line 20 is $encrypt = $_POST['in']; and I don't see what I'm doing wrong with it. Any ideas?
As a general practice for forms in php, always check if the submit button has been clicked.
First name your submit button:
<input type="submit" name="submit">
then further in your php:
if (isset($_POST['submit'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
EDIT #1: Added to that, you seem to have 2 forms and 2 submit buttons. I suggest you keep only one form, and one submit button (remove the 2nd form element and submit button).
If you really need 2 forms, name your submit buttons differently and then you can call them separately.
<input type="submit" name="submit-in">
<!-- ... -->
<input type="submit" name="submit-out">
<?php // ...
if (isset($_POST['submit-in'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
if (isset($_POST['submit-out'])) {
// do your stuff, eg...
$dencrypt = $_POST['out'];
}
EDIT #2: If you want to echo stuff posted in your form, make sure you do the form submission checking and variable setting before the form and then echo the variable after the form (or wherever you want).
you need to first check if the form has been sent, if it hasn't then $_POST['in'] does not yet exist thus throwing the error
May be nothing but you called a php script after closing the form /form, the body /body and then then the HTML /html
replace this code $encrypt = $_POST['in']; by this $encrypt = #$_POST['in'];
this is an error on client server when you upload this file on remote server you will not saw this. use # sign on the client server when you saw this error in future.

how to use $editFormAction in javascript?

I have created a form that has a subit button which will submit the for to a database, and a email button which will run a .cgi script to process the mail. Im having trouble getting the submit button to work.
here is the form buttons
<FORM name="drop_list" method="POST" >
<input name="emailForm" type="button" id="emailForm" onClick="sendFormEmail()" value="Email">
<input name="add_patient" type="button" id="add_patient" onClick="addPatient()" value="Add Patient">
</FORM>
And here is my javascript
function sendFormEmail() //email form
{
alert ("Email, this is disabled atm");
document.drop_list.action = "html_form_send.php"
document.drop_list.submit(); // Submit the page
return true;
}
function addPatient() //Post form to data base
{
alert ("Post to database");
document.drop_list.action = <?php echo $editFormAction; ?>;
document.drop_list.submit(); // Submit the page
return true;
}
The sendFormEmail() works fine, but when I try to use addPatient(). The form will not submit and something in the document.drop_list.action = ; line completely breaks the java script.
Thanks in advance for your help.
-Gregg
You haven't shown the value of $editFormAction, but I'll bet it doesn't have quotes around it. So you need to write:
document.drop_list.action = "<?php echo $editFormAction; ?>";
If you checked in the Javascript console, you should have seen an error message about an undefined variable or undefined not having a php method. And then if you looked at the JS source, you would have noticed that the script name doesn't have quotes around it, like it does in sendFormEmail.

How to supress the warning message in the php

I am pretty new to php world. I wrote the following:
<html>
<head>
<title>It joins simple1 and prac1 program together</title>
</head>
<body>
<?php
if($_POST['user'])
{
print "hello,";
print $_POST['user'];
}
else{
print <<<_HTML_
<form method="post" action="$_server[PHP_SELF]">
Your name:<input type="text" name="user">
</br>
<input type="submit" value="hello">
</form>
_HTML_;
}
?>
</body>
</html> ---- line 23
Getting Error message:
Parse error: syntax error, unexpected $end in C:\wamp\www\php_practice\simple2.php on line 23
I have removed all html tags and just kept php tags it worked:
<?php
// Print a greeting if the form was submitted
if ($_POST['user']) {
print "Hello, ";
// Print what was submitted in the form parameter called 'user'
print $_POST['user'];
print "!";
} else {
// Otherwise, print the form
print <<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Name: <input type="text" name="user">
<br/>
<input type="submit" value="Say Hello">
</form>
_HTML_;
}
?>
Output : Giving proper output but with an warning
Notice: Undefined index: user in C:\wamp\www\php_practice\test.php on line 3
Why it is not working with the previous case? What is going wrong?
How to remove or silent the warning message in the second code. It looks bad in the browser.
The cause of your parse error:
The closing of a HEREDOC statement must occur at the beginning of a line with no whitespace before or after. You have your _HTML indented to the same level as the rest of your code, but it must occur at the very first character position of the line.
_HTML_;
// Should be
_HTML_;
The cause of your undefined index warning:
To test if $_POST['user'] is set, use isset(). That will take care of your undefined index error.
if(isset($_POST['user']))
Update: The cause of the undefined variable _server notice:
Inside a HEREDOC or double quoted string, you will need to wrap complex variables (arrays, objects) in {}. Also, place quotes around PHP_SELF.
<form method="post" action="{$_SERVER['PHP_SELF']}">
You can suppress errors in PHP with an # before the function name (won't work in this case) or by setting error_reporting to a diffrent value (http://www.php.net/manual/en/function.error-reporting.php).
but you should fix the source of the warning instead of suppressing it. In this case there are whitespaces before your HTML;

Categories