PHP: Undefined index in $_POST - php

I am using forms select. I just want to check what user selects by echo-ing the result on the same page so I kept the action="". But its showing error undefined index slct. Can any one please help me
<form action="" method="post">
<select name="slct">
<option value="yes" selected="selected"> yes </option>
<option value="no"> no </option>
</select>
<input type="button" value="Submit" />
</form>
<?php
$tofd = $_POST["slct"];
echo $tofd;
?>
Why its showing the error
Notice: Undefined index: slct in C:\wamp\www\Univ Assignment\Untitled-4.php on line 21

You should use button type submit NOT button
<input type="submit" value="submit" />
And then test IT like
echo (isset($_POST['slct']))? $_POST['slct'] : 'Variable undefined..';

Use PHP isset to check if its exist first
Example :
$tofd = isset($_POST["slct"]) ? $_POST["slct"] : null ;
Example 2 Using a function
function __POST($var)
{
return isset($_POST[$var]) ? $_POST[$var] : null ;
}
$tofd = __POST("slct");

If they are on the same page, initaially, $_POST would be empty because the user has not posted anything. So you have to handle that.
if(isset($_POST["slct"]))
$tofd = $_POST["slct"];

<?php
if (isset($_POST["slct"])){
$tofd = $_POST["slct"];
echo $tofd; }
?>

Related

php set input "name" to php echo variable

I have a form with two radio buttons as "yes" and "No"
I m bringing the "name" of the input from PHP variable I get from the DB.
But however, it echoes in the form but when submitting gives the error.
My HTML code goes as:
<form action="phpfile.php" method="post">
<?php
$sql6 = "SELECT * FROM subadminpriv WHERE subadmin_id = '$sa_id'";
$result6 = $conn->query($sql6);
if ($result6->num_rows > 0) {
while($row6 = $result6->fetch_assoc()) {
$sa_id = $row6["subadmin_id"];
$privilege = $row6["privilege"];
$status = $row6["status"];
?>
<div class="togglebutton m-t-30" >
<label >
<strong>⇒ <?php echo $privilege; ?> </strong> <br>
<input name="<?php echo $privilege; ?>" value="1" type="radio">Yes
<input name="<?php echo $privilege; ?>" value="0" type="radio">No
</label>
</div>
<hr>
<?php } }else { } ?>
<button class="btn button" type="submit">Assign Privileges</button>
</form>
Where the $privilege is "Can See Vendors"
So in here, I'm trying to get the fields "sa_id, privilege, status" from the DB and echo as many radio(yes/no) as the fields in DB.
It works, however, and I echoed the "name" field in the input with PHP variable when I inspect it, it gives the value form the DB, but when I submit and echo it, it gives an error.
The PHP phpfile.php page goes as :
<?php include ('../db.php'); ?>
<?php
$priv1 = $_POST['privilege1'];
echo $priv1;
?>
Here "privilege1" is the actual privilege name from the DB field.
while it gives error as "Notice: Undefined index: privilege1 in D:\xampp\htdocs\blah blah blah/pIpfile.php on line 6"
While I Was expecting value I given to radio button like 1 or 0;
Hope i m Clear with my problem. Any Help is Appreciated...
Don't know how I missed it.
The name in the input field won't accept space, so I changed it to "Can_See_Vendors"
Solved

Can't use a form's value in a different php file

Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.

PHP: get the value of TEXTBOX then pass it to a VARIABLE

Good Day!.
I need a kinda little help for PHP. I’m really really newbie for PHP and I already search it to google and can’t find any solution.
My Problem is:
I want to get the value of textbox1 then transfer it to another page where the value of textbox1 will be appeared in the textbox2.
Below is my codes for PHP:
<html>
<body>
<form name='form' method='post' action="testing2.php">
Name: <input type="text" name="name" id="name" ><br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I also add the code below and the error is “Notice: Undefined index: name”
<?php
$name = $_GET['name'];
echo $name;
?>
or
<?php
$name = $_POST['name'];
echo $name;
?>
Your method is post, so use $_POST
Also, try wrapping it around an isset function:
if (isset($_POST['name'])){
echo $_POST['name'];
}
This will also handle the undefined error
This would be your textbox2:
<input type="text" name="textbox2" id="name" <?php echo (isset($_POST['name']) ? 'value=' .htmlspecialchars($_POST['name']). ' : ''); ?>/>
If you want to use the GET method, change post to get in your form and $_POST to $_GET in the php code of textbox2

PHP error: Undefined index: submit [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
In this once the submit button is clicked it retrieve the data from database. But once i enter this page Undefined index: submit is shown before i click the submit button.
<div id="title">
<form method="get" action="<?php echo $_SERVER['PHP_SELF']?>">
<h3>Select your product and click submit</h3><br />
<select name="prod">
<option value="default">Select your product</option>
<option value="os">Operating Systems</option>
<option value="mobile">Smart Mobiles</option>
<option value="mobile">Computers</option>
<option value="shirt">Shirts</option>
</select><br /><br />
<input type="submit" value="Submit" name="submit"/>
</form>
The php code is:
<?php
$submitcheck=$_GET['submit'];
echo $submitcheck;
if (!isset($submitcheck)) {
echo 'Pls select and submit';
} else {
....
}
?>
You need to change your PHP to:
if (isset($_GET['submit']))
{
// Form has been submitted
}
else
{
// Form has not been submitted
}
At the moment, you're assigning a value which might not exist to $submitcheck and then checking whether it's set. You need to do it the other way round: check $_GET['submit'] is set, then assign it to a variable.
It's bacause you code is executed from top to bottom and on the top you are using $_GET['submit'] and then this key does not exist. You could do something like that:
if(array_key_exists('submit', $_GET)){
echo $_GET['submit'];
}
or even
if (!isset($_GET['submit']))
{
echo 'Pls select and submit';
}
else
{
....
}

Can't get the $_POST Variable

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

Categories