Stack One
Stack One

Reputation: 95

Undefined error in PHP

I've tried using following htmlentities for a text box and I got the following undefined variable error inside the textbox. I've tried changing my php to <?= htmlentities [$_POST['invo_val'] ?> but still get the same message. Any idea where I have gone wrong?

<td><input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php htmlentities($_POST['invc_no']) ?>"></td>

and the error says : <br /><b>Notice</b>: Undefined index: invc_no in <b>E:\xampp\htdocs\ss\docs\addInvo.php</b> on line <b>394</b><br />

Upvotes: 0

Views: 261

Answers (4)

j.w.r
j.w.r

Reputation: 4239

Another option is to suppress the Notice with the error control operator: @

<?= @$_POST['invc_no']; ?>

Upvotes: 0

pkachhia
pkachhia

Reputation: 1932

You have to check first that the post variable is set or not. Use below code:

if(isset($_POST['invc_no'])
{
$inv_no=$_POST['invc_no'];
}
else
{
$inv_no='';
}

and now use this $inv_no into your code

Upvotes: 0

noetix
noetix

Reputation: 4923

PHP gives this notice when you try to access a variable that is not defined.

You should check if the variable exists first (using isset) before you use it.

For example:

<?php $value = isset($_POST['invc_no']) ? htmlentities($_POST['invc_no']) : ''; ?>

<td><input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php echo $value; ?>"></td>

Upvotes: 0

Sergey
Sergey

Reputation: 5207

Try do not get data from unset post

$invc_no = isset($_POST['invc_no']) ? $_POST['invc_no'] : '';

<td><input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php htmlentities($invc_no) ?>"></td>

Upvotes: 2

Related Questions