these are the two files in which error is occurred:
html_form.html
<html>
<body>
<center>
<h1>Admission Form</h1>
<br>
<form action="php_register.php" name="registration">
Firstname: <input type="text" name="name1" placeholder="Enter Firstname"/>
<br>
Lastname: <input type="text" name="name2" placeholder="Enter Lastname"/><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>
php_register.php
<?php
$fname=$_POST['name1'];
$lname=$_POST['name2'];
$conn=mysqli_connect("localhost","root","","test");
if (isset($fname) && isset($lname))
{
mysqli_query($conn, "insert into test_table(firstname,lastname)
values ('$fname','$lname')");
}
else
echo "<br> Errror....Values are not set in variables...!!!";
?>
( ! ) Notice: Undefined index: name1 in C:\wamp2\www\PHP_project\php_register.php on line 2
Call Stack
Time Memory Function Location
1 0.0022 131712 {main}( ) ...\php_register.php:0
( ! ) Notice: Undefined index: name2 in C:\wamp2\www\PHP_project\php_register.php on line 3
Call Stack
Time Memory Function Location
1 0.0022 131712 {main}( ) ...\php_register.php:0
Errror....Values are not set in variables...!!!
You need to add method="post" to your <form> tag.
Also you could a check if those values are set before on your php_register.php page to ensure the script will run successfully.
e.g.
if (empty($_POST['name1']) || empty($POST['name2']) {
// some kind of error setting and redirecting back maybe
}
By default, form method is GET but you are trying to get values By $_POST.
You didn't define form POST method then you can get values by $_GET or $_REQUEST as:
$_GET['name1'];
$_GET['name2'];
or
$_REQUEST['name1'];
$_REQUEST['name2'];
If you want to use $_POST then you should define form method="post"
If you have no idea about form method, It's a better way to use $_REQUEST. By $_REQUEST you can get both type values.
Example:
$fname=isset($_POST['name1'])?$_POST['name1']:'';
$lname=isset($_POST['name2'])?$_POST['name2']:'';
$conn=mysqli_connect("localhost","root","","test");
if (!empty($fname) && !empty($lname))
{
mysqli_query($conn, "insert into test_table(firstname,lastname)
values ('$fname','$lname')");
}
else
echo "<br> Errror....Values are not set in variables...!!!";
u don't declare POST or GET in the form ,but u use $_POST['name1'] to get the value, thats where the error occured.
Related
I have two pages that i like to exchange variable. in the first page i have form like this:
<form method='post' action='rehabCreate2.php' onsubmit='return validateForm();'>
<input class="textbox" type='text' id='txt_stuNum' name='txt_stuNum'/ required>
<input type="submit" value="NEXT" id="btnNext">
</form>
then i set the session variable like this:
if ( isset( $_POST['btnNext'])){
$stuId=$_POST['txt_stuNum'];
$_SESSION["stuId"]=$stuId;
}
then in my page2 i want to it:
<?php
session_start();
$stuId=$_SESSION["stuId"];
echo $stuId;
?>
but it gives me error:
Notice: Undefined index: stuId in...
what am i missing?
and another thing, how can i make a "back button" and the values are still in there?
EDIT:
originally my "session_start()" was place at the top of the page1, but when i transfer it below my ""
this error message show:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\cerecare\portal\somepage.php:176) in C:\xampp\htdocs\cerecare\portal\rehabCreate.php on line 16
by the way: the line 176 of somepage is the end of the script tag and nothing else follows
First replace (give a name attr to submit button to get on POST)
<input class="textbox" type='text' id='txt_stuNum' name='txt_stuNum'/ required>
<input type="submit" value="NEXT" id="btnNext">
to
<input class="textbox" type='text' id='txt_stuNum' name='txt_stuNum' required />
<input type="submit" value="NEXT" id="btnNext" name="btnNext">
Add a session_start() on page1
session_start();
if (isset( $_POST['btnNext'])){
$stuId=$_POST['txt_stuNum'];
$_SESSION["stuId"] = $stuId;
}
Then Add a check for session value exist or not on page2
$stuId=(isset($_SESSION["stuId"]) ? $_SESSION["stuId"] :'');
You forgot to add name for submit button but in addition whenever you are using any variable whether session or local variable just make sure it exists or not. so to save yourself from this problem always use this style of code given by #Rakesh Sharma.
$var = isset($_SESSION['any_var']) ? $_SESSION['any_var'] : '';
or the better way to perform check and reduce calculation mistake use like this
$var = ( isset($_SESSION['any_var']) && !empty($_SESSION['any_var']) ? $_SESSION['any_var'] : '';
In statements also always use this style
if ( isset($_SESSION['any_var']) && !empty($_SESSION['any_var'])){
/// code in statement
}
use this style of code always in your code and fee free at least from undefined index error and some calculation mistakes if value is empty.
I'm trying to make a form that updates a datebase but it gives me two errors. Do you have any idea what it could be from?
The errors:
Notice: Undefined variable: Points inD:\2013.1\xampp\htdocs\ranklist_get.php on line 9
Notice: Undefined variable: Skype in D:\2013.1\xampp\htdocs\ranklist_get.php on line 9
welcome.html
<body>
<form action="ranklist_get.php" method="get">
Skype: <input type="text" id="Skype"><br>
Points: <input type="number" id="Points"><br>
<input type="submit">
</form>
</body>
</html>
ranklist_get.php
<?php
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE Persons SET Points='".$Points."' WHERE Skype='".$Skype."'");
mysqli_close($con);
?>
Initialize your variables with the expected values before you use them in your query.
$Points=mysqli_real_escape_string($con,$_GET["Points"]);
$Skype=mysqli_real_escape_string($con,$_GET["Skype"]);
Also make sure to add the name attribute to your form fields. name="Points" and name="Skype", otherwise it wont work.
GET variables are stored in the global $_GET array (just like POST and COOKIE). You can either use them directly in your code like so $_GET["Points"] or store them in a variable.
Please note you should use the name property on each input to specify it's key in the array.
At the top of your code put:
$Points = $_GET["Points"];
$Skype = $_GET["Skype"];
Your form should be rewritten like so:
<form action="ranklist_get.php" method="get">
Skype: <input type="text" id="Skype" name="Skype"><br>
Points: <input type="number" id="Points" name="Points"><br>
<input type="submit">
</form>
You should also sanitize your MySQL query like so:
$query = mysqli->prepare($con, "UPDATE Persons SET Points=? WHERE Skype=?");
$query->bind_param('ss', $points, $skype);
$points = $_GET["Points"];
$skype = $_GET["Skype"];
$query->execute();
You can read more about prepared statements here: http://php.net/manual/en/mysqli.prepare.php
make HTML as
<html>
<body>
<form action="ranklist_get.php" method="get">
Skype: <input type="text" id="Skype" name="Skype"><br>
Points: <input type="number" id="Points" name="Points"><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
ranklist_get.php
<?php
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if($_REQUEST['Submit']=='Submit'){
$Points=$_REQUEST['Points'];
$Skype=$_REQUEST['Skype'];
mysqli_query($con,"UPDATE Persons SET Points='".$Points."' WHERE Skype='".$Skype."'");
}
mysqli_close($con);
?>
You have to get the values before you use it.
It should be a multiple upload form for pictures
I get the HTML Code for a Upload-Form:
<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">
<label id="filelabel" for="fileselect">Choose the Pictures</label>
<input type="file" id="fileselect" class="fileuplaod" name="uploads[]" multiple />
<span class="text">Exist Album</span><br />
<select id="existAlbum" name="existAlbum" size="1">
<option value="noAlbum">SELECT ALBUM</option>
</select>
<span class="text">OR</span>
<span class="text">New Album</span><br />
<input id="newAlbum" name="newAlbum" type="text" maxlength="20" placeholder="ALBUM NAME"/>
<input type="submit">
</form>
The form link to the uploaded.php. But there i get:
Notice: Undefined index: existAlbum in E:\xampp\htdocs\fotokurs\upload\upload.php on line 11
Notice: Undefined index: newAlbum in E:\xampp\htdocs\fotokurs\upload\upload.php on line 12
Here's the upload.php:
<?PHP
$allowedExtensions = array('png', 'jpg', 'jpeg');
$maxSize = 20971520;
$i = 0;
$first = 0;
$exist_album = $_POST['existAlbum'];
$new_album = $_POST['newAlbum'];
Where is my fault? I can't find it...
EDIT
Add following to my code:
if( isset( $_POST['existAlbum'] ) or isset( $_POST['newAlbum'] ) ){
$exist_album = $_POST['existAlbum'];
$new_album = $_POST['newAlbum'];
}else{
echo 'no album <br />';
}
print_r($_POST);
new output:
no album
Array ( )
Notice: Undefined variable: new_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 20
Notice: Undefined variable: exist_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 21
Notice: Undefined variable: new_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 22
Notice: Undefined variable: exist_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 23
One of your issues is that existAlbum has no actual values associated with it.
You have <option>Select Album</option> which has no value associated with the option element. If there is no value associated, the select element is not posted to the server. You should change it to be:
<option value="">Select Album</option>
EDIT
Since the user only has to supply one or the other, you should use the following to set your variables:
$existsAlbum = (isset($_POST['existAlbum']) && !empty($_POST['existAlbum'])) ? $_POST['existAlbum'] : 'defaultValue';
$newAlbum = (isset($_POST['newAlbum']) && !empty($_POST['newAlbum'])) ? $_POST['newAlbum'] : 'defaultValue';
One important thing to note is that Internet Explorer does not support the placeholder attribute.
EDIT 2
Here is my quick test page that worked test.php:
<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">
<label id="filelabel" for="fileselect">Choose the Pictures</label>
<input type="file" id="fileselect" class="fileuplaod" name="uploads[]" multiple />
<span class="text">Exist Album</span><br />
<select id="existAlbum" name="existAlbum" size="1">
<option value="noAlbum">SELECT ALBUM</option>
</select>
<span class="text">OR</span>
<span class="text">New Album</span><br />
<input id="newAlbum" name="newAlbum" type="text" maxlength="20" placeholder="ALBUM NAME"/>
<input type="submit" value="Submit">
</form>
upload.php
<pre>
<?php print_r($_POST); ?>
<?php print_r($_FILES); ?>
</pre>
results
Array
(
[existAlbum] => noAlbum
[newAlbum] =>
)
Array
(
[uploads] => Array
(
//Contents here
)
)
Try if the value existAlbum get set, because it won't return any value if you there is nothing picked. You could give the existAlbum picker a default='1' or something:
if isset($_POST['existAlbum']){
echo 'yes';
}
else{
echo 'no';
}
I think that there is something wrong with the rule enctype="multipart/form-data". Try to just remove this, it should be set automatically by your browser.
You have no value for the option select album, even if you don't intend that option to be used give it a value such as 0 so that it will always be set in the POST variables.
<option value="0">SELECT ALBUM</option>
<option value="some album">Some Album</option>
...
If select is not picked you will not get it at all (you expect it to be empty, which is not true). You have to check first
$exist_album = isset($_POST['existAlbum']) ? $_POST['existAlbum'] : '<DEFAULT VALUE>';
and same for checkbox.
The newAlbum thing should work as text inputs are always there. See
print_r($_POST);
to see what's really in there, and in my case it is - on "empty" submit I get:
Array
(
[existAlbum] => SELECT ALBUM
[newAlbum] =>
)
BTW: you should use <?php rather than <?PHP.
print $_POST Array using print_r($_POST); Make sure your form action is correct
<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">
The code is working, however I still recieve undefined index with the Id "addmsg"
<?php
$addmsg=$_GET["addmsg"];
if (isset($addmsg)) // If the user wants to add a Message
{
?>
This is the code for the textarea and the submit buttons:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p>Post your Message:<br />
<textarea name="msg" rows="10" cols="70" wrap></textarea><br />
<input type="submit" name="smtmsg" value="SUBMIT" /></p>
</form>
Lastly, this code is to connect to the Mysql database
$url = $_SERVER['PHP_SELF'] ."?addmsg=1";
You need to check isset before assigning the variable:
<?php
if (isset($_GET["addmsg"])) // If the user wants to add a Message
{
$addmsg=$_GET["addmsg"];
}
?>
You use $_POST so the field msg would never be evaluated here. Either change the $_GET['addmsg'] to $_POST['msg'] or change the field msg to addmsg and change the form type to "get"
There is no field named "addmsg" in your HTML. The only one you have is named "msg".
Your form is submitting to <?php echo $_SERVER['PHP_SELF'];?>, so on your server side, there is no index addmsg in the $_GET superglobal.
You should change your form action to <?php echo $_SERVER['PHP_SELF'] . "?addmsg=1";?> as you have in the last line of your question.
The message mean exactly what it says. The is nothing set at $_GET['addmsg'].
In this case, you have two problems. Your form method is set to post. You must therefore look for your value in $_POST
Second, you must actually have a field named addmsg in your form. The field in your form is named msg. So the value would be available as $_POST['msg']
There is 2 errors:
First:
IF the method is "post", the variable is $_POST.
Second:
The textarea's name is "msg", then you have to do $_POST["msg"].
Of course, if methos="get", the variable is $_GET["msg"]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
strange php error
Submit an HTML form with empty checkboxes
PHP form checkbox and undefined index
Some reason I am getting undefined index errors in the following code. Any help would be appreciated. Here are the errors I am Recieving
Notice: Undefined index: username in C:\xampp\htdocs\register.php on line 5
Notice: Undefined index: password in C:\xampp\htdocs\register.php on line 6
Notice: Undefined index: firstname in C:\xampp\htdocs\register.php on line 7
Notice: Undefined index: surname in C:\xampp\htdocs\register.php on line 8
Code:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("clubresults") or die(mysql_error());
$username = $_POST['username'];
$password = md5($_POST['password']);
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$sql = "INSERT INTO members (Username, Password, Firstname, Surname) VALUES ($username, $password, $firstname, $surname);";
mysql_query($sql);
?>
<html>
<div class="content">
<center>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Register</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="password" maxlength="10">
</td></tr>
<tr><td>Firstname:</td><td>
<input type="text" name="firstname" maxlength=210">
</td></tr>
<tr><td>Surname:</td><td>
<input type="text" name="surname" maxlength=210">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit"
value="Register"> </th></tr> </table>
</form>
</center>
</div>
</html>
When your page loads for the first time (unless it was the action of another page), the REQUEST_METHOD is 'GET'; as a result, no POST variables exist (although the $_POST superglobal exists).
When you POST, either from another page, or from the same page, all the data set in the form will be present in the $_POST superglobal, and can be accessed the way you accessed them earlier; unfortunately, if a data is not present, and you try to access it, an error will be thrown, similar to what you have.
To solve the issue, especially if you want to post to the same page as the form, use something like
if (isset($_POST["username"]))
{
// do whatever
}
if (isset($_POST["password"]))
{
// do whatever
}
if (isset($_POST["firstname"]))
{
// do whatever
}
if (isset($_POST["surname"]))
{
// do whatever
}
With that, your code won't throw an error if those data aren't present.
Hope that helps.
On an unrelated, but important note, do not use mysql_* functions anymore. They have been deprecated; use mysqli or PDO functions for data access.
Happy coding!