Undefined index: command [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)
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
}

Related

Notice: Undefined variable: world in abc.php on line [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 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>

Isset is not working [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)
PHP & Case Sensitivity [duplicate]
(3 answers)
Closed 5 years ago.
I have a problem with isset. What I want is some echos after getting, checking and initialising the values from isset. it doesn't work. I don't see any reason behind this. It only refreshes the page after submitting the form.
Below is the code
<?php
if (isset($_post['contact_name'] ) && isset($_post['contact_email']) && isset($_post['contact_text']))
{
echo $contact_name = $_post["contact_name"];
echo $contact_email = $_post['contact_email'];
echo $contact_text = $_post['contact_text'];
//!empty($_post['contact_name'])
//echo 'no value submit';
if(!empty($contact_name) && !empty($contact_email) && !empty($contact_text))
{
echo 'ok';
}
else
{
echo 'all fildes required !!!!!!!!!!!!!';
}
}
?>
<html>
<form action="index.php" method="post">
name : <br> <input type="text" name="contact_name" ><br><br>
Email address : <br> <input type="text" name= "contact_email "><br><br>
Message :<br><textarea name="contact_text" rows="6" cols="30" type="text" ></textarea><br><br>
<input type="submit" value="send">
</form>
</html>
You need to use this;
<?php
if (isset($_POST['contact_name'] ) && isset($_POST['contact_email']) && isset($_POST['contact_text']))
{
...
Use $_POST instead of $_post. Hope this will work.

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

Categories