Jump to content

handling undefined index


Adamhumbug

Recommended Posts

I have a from that posts when i click submit, one of the inputs is created if you create a new client rather than picking one from a select.

If creating a new client, an id will not be passed in and therefore will be undefined.

I thought i had handled it with the following but i am getting a notice of undefined index on "clientNameNew"

	//if createing a new client name
	if ($_POST['clientNameNew']) {
		$clientName = $_POST['clientNameNew'];
		$clientId = createNewClient($pdo, $clientName);
	} else {
		$clientId = $_POST['clientName'];
	}

Is there a better way of handling this than the way i have above?

Link to comment
Share on other sites

10 minutes ago, Adamhumbug said:

an id will not be passed in

i/we would assume that's the id, auto-increment primary index for an existing record in the database table, passed in a hidden form field, not one of the user entered fields. you would then use the existence or absence of the id to control if you are editing/updating existing data or inserting new data.

Edited by mac_gyver
Link to comment
Share on other sites

If we are understanding you, you are saying that when the user creates a new record there will not be an id provided, since it is unknown until you add it to the database. 

SO in that case, why are you looking for it in the post data when you know it may not there?  What does a good programmer do when he is not sure of something?  He CHECKS.  In your case, you need to do an 'isset' check on the id element and if it is not there you are probably supposed to assume that you need to do an add.  Of course we don't know anything about your app so perhaps you will assume something else but this is how you can avoid the error message and do something more appropriate.

Link to comment
Share on other sites

if ($_POST['clientNameNew'])

This will check if the value is a truthy value or not, but the key still needs to exist or you'll get an undefined index notice.

You can avoid the notice in various ways.  In modern PHP versions, the simplest is to use the null coalescing operator.

if ($_POST['clientNameNew'] ?? '')

Other ways to check are with isset, empty, or array_key_exists.  Alternatively, you could just force them to exist.

 

Link to comment
Share on other sites

17 hours ago, ginerjm said:

If we are understanding you, you are saying that when the user creates a new record there will not be an id provided, since it is unknown until you add it to the database. 

SO in that case, why are you looking for it in the post data when you know it may not there?  What does a good programmer do when he is not sure of something?  He CHECKS.  In your case, you need to do an 'isset' check on the id element and if it is not there you are probably supposed to assume that you need to do an add.  Of course we don't know anything about your app so perhaps you will assume something else but this is how you can avoid the error message and do something more appropriate.

So if the user selects a client from a dropdown (named clientName) then there will be an id available, if the user selects add new client, the dropdown is replaced with an input named clientNameNew.  So one of the two will be posted but there is no way of knowing which it will be.

But ultimately, as set up, i dont know if there will be a post value of clientNewName and need to do something different if it is posted from if an existing client is selected from the list.


The reading that i have done suggests that isset is the way to go and shouldnt produce a warning, but i am still getting one:

Quote

This is from - https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce


<?php
// Example usage for: Null Coalesce Operator
$action = $_POST['action'] ?? 'default';

// The above is identical to this if/else statement
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else {
    $action = 'default';
}
?>

The expression (expr1) ?? (expr2) evaluates to expr2 if expr1 is null, and expr1 otherwise.

In particular, this operator does not emit a notice or warning if the left-hand side value does not exist, just like isset(). This is especially useful on array keys.

 

 

Link to comment
Share on other sites

1 hour ago, Adamhumbug said:

So if the user selects a client from a dropdown (named clientName) then there will be an id available, if the user selects add new client, the dropdown is replaced with an input named clientNameNew.

why are you using two different names for the same meaning data? besides the existence or absence of the id, what's different about the edit vs create form?

can you not just always have a hidden id field that has a value if editing an existing entry or is empty when creating a new entry?

your post method form processing logic should be -

detect if a post method form was submitted
perform common tasks, such as trimming all the input data at once, validating the common inputs between both processes
test if the id is empty or not
if the id is empty, run the unique code for creating a new entry
else, run the unique code for updating an exiting entry

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.