سؤال

I have a form build up in javascript, which i want to use for adding users and updating users.

The form holds some input elements like below. This works when i get the user data to build up the form for updating, but for adding a new user this property does not exists in the array so the script failed and stop running correctly, as expected.

The question is, what is the shortest way to suppress this error? Something php like @$params['user_email'].

  • i know about try, cache
  • i know about suppressing all errors on the window object.
  • type of, undefined etc.

I just looking for the shortest way to do this without adding multiple lines of code for validating each property in the params array.

<input name="user_email" type="text" value="'+params['user_email']+'"/>
هل كانت مفيدة؟

المحلول

NEVER suppress errors. It's a very bad habit and will give you serious headaches rather sooner than later. Always do a proper check. (like following)

          // if available           use it            otherwise echo empty string
value="'+(params['user_email'] ? params['user_email'] : '')+'"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top