Notice: Undefined index: name when posting - php

So I'm trying to grasp the basics of how to make/use an API. So I have a bit of code that should check if the name is what is should be, then return a 1 or 0. So here's the code
$name = $_POST['name'];
//Set array
$status = array();
if(isset($name) || $name = "Bob"){
$status['success'] = 1;
echo json_encode($status);
}
else{
$status['success'] = 0;
echo json_encode($status);
}
Then I hop over to PostMan to send the POST, I do
http://localhost/index.php?name=Bob
But all I end up getting is
( ! ) Notice: Undefined index: name in /Users/matt/Dropbox/API Server/index.php on line 3
Line 3 turns out to be $name = $_POST['name'];. Any help would be great

if you are getting the value from URL(index.php?name=Bob) it has to be
$name = $_GET['name'];

Related

Notice: Undefined offset: 0 in C:\xampp\htdocs\isan\hasildata.php on line 21

Hi I receive the following error in my homework and I do not know what is the problem hasildata.php on line 21 :
$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");
$data = json_decode($dataJson,true);
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
$time=$data['rows'][0]['elements'][0]['duration']['text'];
Screenshot
You must check whether your request is OK or not
$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");
$data = json_decode($dataJson,true);
if ($data['status'] !== "OK") {
// Error
echo $data['error_message'];
// Do something
} else {
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
$time=$data['rows'][0]['elements'][0]['duration']['text'];
}

how to resolve this error: (Warning: Illegal string offset) and (Notice: Uninitialized string offset: 0)

Let's say I have search.php and edit.php.
Inside the search.php they can remove and update some records.
If the user clicks the "update" button, the system will redirect the user to another page called edit.php.
I successfully called the itemid.
but this happens..
<?php
$itemid = $_GET["itemid"];
$description = "";
$classification = "";
$unit = "";
$quantity = "";
$price = "";
$reorder = "";
$status ="";
$record = MYDB::query(
"select
*
from
item
where
itemid = ? ",
array($itemid),
"SELECT"
);
if(count($record) > 0)
{
$record = $record[0];
$description = $description['description'];
$classification = $classification['classification'];
$unit = $unit['unit'];
$quantity = $quantity['quantity'];
$price = $price['price'];
$reorder = $reorder['reorder'];
$status = $status['status'];
}
else
{
echo '<div class="fail">';
echo '<b>FAIL!</b> Item Not Found!</div>';
die();
}
if(isset($_POST["btnsubmit"]))
{
if(isset($_POST["description"])){
$description=ucwords(trim($_POST["description"]));
}
else{
echo "Please Enter description";
}
if(isset($_POST["classification"])){
$classification=ucwords(trim($_POST["classification"]));
}
else{
echo "Please Enter classification";
}
if(isset($_POST["unit"])){
$unit=ucwords(trim($_POST["unit"]));
}
else{
echo "Please Enter unit";
}
if(isset($_POST["quantity"])){
$quantity=ucwords(trim($_POST["quantity"]));
}
else{
echo "Please Enter quantity";
}
if(isset($_POST["price"])){
$price=ucwords(trim($_POST["price"]));
}
else{
echo "Please Enter Price";
}
if(isset($_POST["reorder"])){
$reorder=ucwords(trim($_POST["reorder"]));
}
else{
echo "Please Enter reorder";
}
if(isset($_POST["status"])){
$status=ucwords(trim($_POST["status"]));
}
else{
echo "Please Enter status";
}
}
?>
This are the errors,
Warning: Illegal string offset
Notice: Uninitialized string offset: 0
That error basically means that you are calling an array by a key which does not exist. I would tell you which key that is but you have not provided the specific variable.
Say I have this array:
$array = ('some_real_key' => 'a very important value');
Now if I call this $array['some_fake_key'], given that our array does not have that some_fake_key then it will produce the error you are seeing. Same goes for your 0 offset.
You are calling it in your code:
$record = $record[0];
That means that there's no 0 offset, which could mean a range of things... Again there's not enough data provided. But it would follow the same example as above.
To fix those issues you can use array_key_exists():
if ( array_key_exists( 'some_real_key', $array )
{
echo $array['some_real_key'];
}
else if ( array_key_exists( 'some_fake_key', $array )
{
echo $array['some_fake_key'];
}
This will output only the first array key; and will output no errors.
UPDATE
Thinking about it, I think your error may be produced from your variables as follows:
$record = $record[0];
$description = $description['description'];
$classification = $classification['classification'];
$unit = $unit['unit'];
$quantity = $quantity['quantity'];
$price = $price['price'];
$reorder = $reorder['reorder'];
$status = $status['status'];
You are not really setting them from an array; you may be wanting to call $record['key'] instead of $description = $description['description'];
For instance this would look like:
$description = $record['description'];
// ... so on ...
This looks like the culprit
$record = $record[0];
You cant be setting a variable as a none array with itself becoming an array with an uninitialized value. That's just crazy talk... :)
What do you think you are trying to do with that line?
Assuming $record[0] exists and its some kind of id, you could do...
$record_id = $record[0];
But you are using an associative array for all your other items, so where does the index of 0 come into the picture...
You'd need to perform a var_dump of $record to check you are getting what you expect.

Notice: Undefined index: in C:\wamp\www\tests\Joomla\Website\index.php on line 37

Hey i get this : Notice: Undefined index: in C:\wamp\www\tests\Joomla\Website\index.php on line 37
And my code is this :
<?php
$remarks=$_GET['remarks'];
if ($remarks==null and $remarks=="")
{
echo '';
}
if ($remarks=='success')
{
echo 'Registration Success';
}
?>
I don't understand why i get this . Please help!
First, you don't say where is the line 37... I ain't a wizard, but I can guess from the error...
Since the error is Undefined index, that must come from the line:
$remarks=$_GET['remarks'];
You should validate that $_GET['remarks'] is not null with isset($_GET['remarks']) before trying to get it's value.
Second, that line does not make any sence, since the $remarks can never be null and "":
if ($remarks==null and $remarks=="")
So I would write the code like this:
<?php
$remarks = "";
if ( isset($_GET['remarks']) ) {
$remarks = $_GET['remarks'];
}
if ( $remarks == 'success' ) {
echo 'Registration Success';
}
?>

error! got undefined variable if sent empty post

i got error message if sent empty post in my registration form
Notice: Undefined index: terms in /opt/lampp/htdocs/user/register.php on line 34
in the line 34 i have this
$terms = trim($_POST["terms"]);
and in the form i have this
<p>
<input type="checkbox" name="terms" id="terms"> I have read and accept the conditions of use
</p>
and this is all the validation form
if(!empty($_POST))
{
$errors = array();
$terms = trim($_POST["terms"]);
$captcha = md5($_POST["captcha"]);
$name = trim($_POST["name"]);
if($terms == "")
{
$errors[] = lang("ACCOUNT_SPECIFY_NAME");
}
//End data validation
if(count($errors) == 0)
{
//Construct a user object
$user = new User($username,$password,$email,$name,$lastname);
//Checking this flag tells us whether there were any errors such as possible data duplication occured
if(!$user->status)
{
if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
if($user->email_taken) $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
if($user->email_blocked) $errors[] = lang("ACCOUNT_EMAIL_BLOCKED");
}
else
{
//Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
if(!$user->userCakeAddUser())
{
if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
if($user->sql_failure) $errors[] = lang("SQL_ERROR");
}
}
}
}
?>
why i got this alert message?
thanks
Try to replace this:
if(!empty($_POST))
with this:
if(!empty($_POST["terms"]))
You need to make sure that array $_POST has a value with key terms
EDIT:
Try this:
$terms = empty($_POST["terms"]) ? null : trim($_POST["terms"]);
$captcha = empty($_POST["captcha"]) ? null : md5($_POST["captcha"]);
$name = empty($_POST["name"]) ? null : trim($_POST["name"]);
instead of this:
$terms = trim($_POST["terms"]);
$captcha = md5($_POST["captcha"]);
$name = trim($_POST["name"]);
It's not an error but a Notice and a notice can be ignored if it’s not critical..
if(isset($_POST['terms']))$terms = trim($_POST["terms"]);
isset() function in PHP determines whether a variable is set and is not NULL. It returns a Boolean value, that is, if the variable is set it will return true and if the variable value is null it will return false.
You can also turn off notices by using
error_reporting(E_ALL ^ E_NOTICE);
in the start of your script..

Troubleshooting "Undefined offset" error

It's show following error:
Notice: Undefined offset: 1 in C:\xampp\htdocs\evantechbd\secure\content\feature_link_process.php on line 28.
Why it's show the error...? Can anyone tell me... Here is the code:
$row = explode("|", $_POST['coun_name']);
$coun_id = $row[1]; // cat_id
$coun_name = $row[0];
if(isset($coun)){
$errors = array();
if(empty($coun))
$errors[] = 'Country Name require<br>';
}
$row = explode("|", $_POST['coun_name']);
$coun_id = $row[1]; // cat_id
$coun_name = $row[0];
if $_POST['coun_name] has no | than $row[1] is not defined.
You will probably want to do something like this, instead:
$errors = array();
if( empty($_POST['coun_name']) )
{
$errors[] = 'Country Name required.';
}
elseif( strpos($_POST['coun_name'], '|') === false )
{
$errors[] = 'Country Name invalid.';
}
else
{
list($coun_name, $coun_id) = explode("|", $_POST['coun_name'], 2);
}
The first condition checks to make sure the user submitted a value for $_POST['coun_name']. The second condition checks to make sure that the value contains a '|' character.
If both conditions are met, $coun_name and $coun_id are populated by explode()'ing $_POST['coun_name'] as normal (I dropped in a call to list() for brevity).

Categories