What is wrong in this code?

I’m creating a script which is meant to convert a value in bytes into kilobytes. But it showing this error :

Notice: Undefined index: ip in C:\xampp\htdocs\Php\converter\func.php on line 10
The value is : 0

Form :

<form action="func.php"  method="post" >
	<input type="text" placeholder="Enter value in bytes" name="ip">
    <input type="submit" value="Let's Go">
</form>

Func.php :

<?php 

$input = $_POST['ip'];

function converter($input, $extrastring = "Kilobytes") {
	$converted = $input/1000;
	echo("The value is  : $converted");
}

converter($input);
?>

The $_POST['id']; is not set. // typo - should ip and not id

Try this:

$input = isset($_POST['ip'] ) ? $_POST['ip'] : NULL;

function converter($input, $extrastring = "Kilobytes")
{
  $msg = '';
  if( $input ):
    $converted = $input/1000;
    $msg = "The value is  : $converted";
  endif;
  echo $msg;  
}

Edit:
Noted typo of id instead of ip

1 Like

You mean $_POST['ip'], typo. Meaning that if the user doesn’t fill in the form input field, that’s the message the user will see. I read it as the user seeing the problem even if they do fill out the form - after all, it wouldn’t be a surprise that the variable would be undefined if the user didn’t fill it out and the developer didn’t check for that.

But I’m confused about the error message, because it complains that the undefined index ip is on line 10 of func.php, but line ten is

converter($input);

which doesn’t refer to that index at all.

1 Like

If you’re converting bytes into kilobytes, I’m pretty sure you should be dividing by 1024, NOT 1,000.

1 Like

if you want to mess around with 1024, use the appropriate unit: kibibyte

1 Like

I was going to say that, but someone’s changed the rules and now all the numbers that we used to know that were based on powers of two seem to have been rounded down to make it easier for normal people to deal with. Not annoyed about it at all.

It’s not April 1st is it?

Anyway, I thought I was a normal person - I need a refund…

1 Like

I think it depends on if the OP is taking about KB or kB (kilobyte or kibibyte).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.