Notice: Undefined variable: world in abc.php on line [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 months ago.
I have a html/php code as shown below in the file abc.php in which at Line#A, I am getting the following error:
Notice: Undefined variable: world in abc.php on line#A
html/php code (abc.php) :
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$world = $_POST['world'];
}
<form method="post" action="abc.php?id=<?= $id; ?>">
<input type="hidden" name="world" value="<?= $world; ?>"/> //line#A
</form>
This is what I have tried but it doesn't seem to work:
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$world = isset($_POST['world']);
}

The $world variable will only be defined if page served via POST method.
So, what you need is define it before the condition:
$world = "";
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$world = $_POST['world'];
}
<form method="post" action="abc.php?id=<?= $id; ?>">
<input type="hidden" name="world" value="<?= $world; ?>"/> //line#A
</form>

Related

Undefined variable: details in C:\xampp\htdocs\redo\consultation.php on line 21 in Php [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
<?php include 'connection.php'; ?>
<?php
if (isset($_GET['consultation']))
{
echo "Consultation Details";
$no=$_GET['consultation'];
?>
<form class="" method="get">
<textarea name="details" rows="8" cols="80" placeholder="enter consultation details"></textarea><br>
<button type="submit" name="c">submit</button>
</form>
<?php
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
//$details= $_GET['details'];
$insertQuery="INSERT INTO redo (consultation) VALUES ($details) WHERE no=$no;";
$insert = mysqli_query($conn,$insertQuery);
if ($insert) {
echo "recorded";
?>
Back to Total Patients
Back to Registration
<?php
}
else {
echo "record couldnt be inserted";
}
}
?>
No $_GET['details'], you sure you're using a GET method in the form? Change $_GET['details'] to $_REQUEST['details'].
To be exact, this one fails:
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
I mean, the if condition is not met, so $details is not defined, as you don't define it anywhere in the code outside that if.
Other solution would be adding:
$details = '';
in front of that if.

Notice: Undefined index: m in [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
Notice: Undefined index: m in C:\xampp\htdocs\test.php on line 2
Notice: Undefined index: uname in C:\xampp\htdocs\test.php on line 3
<?
$m = $_REQUEST["m"];
$uname = $_REQUEST["uname"];
if($m=="chkfrm"){ //Check about true values
if($uname==""){ //if input form 'uname' is null
?>
No content
<?
} else { // Else input form 'uname' is value
//$m=""; // Reset chkfrm
?>
In Content
<? }
} else { //Main page
?>
<form name="aa" method="post" action="./test.php">
<input type="hidden" name="m" value="chkfrm">
<input type="text" name="uname" size="30">
<p>
<input type="image" src="./jigoku/images/name.gif" class="mouse" onClick="javascript:returns();">
</p>
</form>
<?
} //Main End
?>
Use isset() when assigning the variable.
$m = isset($_REQUEST['m']) ? $_REQUEST['m'] : null;

PHP Form/Post Output [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
The HTML page
<form action="test2.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
and the test2.php
<?php
$firstname = $_POST['name'];
print("Name $firstname");
?>
and it shows error
Notice: Undefined index: firstname in test2.php on line 7
This is a problem when i run it on my PHPstorm. PHP i am using is v7.1.3
I don't know whats wrong.
INSERTING PICS
This was using XAMPP and still same results
Check your variable defined or not in following method
if (isset($_POST['name']) && !empty($_POST['name'])) {
$firstname = $_POST['name'];
echo "Name ".$firstname;
}

showing undefined index when try to submit the form [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
Every time I hit submit button after selecting an image will face the problem "Notice: Undefined index: profile_picture in C:\wamp\www\Test\testImage.php on line 5", and the page print the text not selected which must be display if image is not selected.
The index is defined, but why it is behave like this, please help with your suggestions
<?php
$photo = "";
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$photo = trim(stripslashes($_POST["profile_picture"]));
if($photo != '')
{
//$photo = trim(stripslashes($_POST["profile_picture"]));
header("Location: success.php");
}
else{
echo 'not selected';
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form name="signup_form" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" class="profile_picture" id="profile_picture" name="profile_picture"> </br>
<input type="submit" name="submit_btn" id="submit_btn" class="submit_btn" value="sign up">
</form>
</body>
</html>
Files are not posted over $_POST.
They are posted with $_FILES array.
Reference

Undefined index: command [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
undefined index [closed]
(2 answers)
Closed 9 years ago.
These are my codes And i'm getting error
Notice: Undefined index: command in E:\xampp\htdocs\Shop_cart\products.php on line 17
Javascript:
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
HTML Code:
<form name="form1" action="">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
<input type="button" value="Add to Cart" onclick="addtocart(1)" />
</form>
PHP Code:
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
In your PHP code add isset check to the condition:
if ( isset($_REQUEST['command']) && $_REQUEST['command'] == 'add' && $_REQUEST['productid']>0 ) {}
Change
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
to
if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0){
Same to other answer but I would literally proceed something like this for security purposes and better layout of my code.
//Check if request has been made
if (isset($_REQUEST['command'])){
//proceed for checking
if($_REQUEST['command'] == 'add' && $_REQUEST['productid']>0 )
{
//Your code here
}
}else{
//redirect or whatsoever
}

Categories