0

I currently have a basic form setup with a controller, and when the user submits the form, this function is ran:

$scope.create = function(email, username, password) {
MyService.get({
    action: 'create',
    email: email,
    username: username,
    password: password
}, function(data) {
    // Success callback
});
};

The problem is that if the user submits the form with an empty field, the php receiving this request throws an undefined index error for that field.

Ex: User doesn't fill in email and presses submit. The php throws an error because $_GET['email'] is an undefined index.

How can I get it so that the resource just sends an empty value if the field is empty? (I'd prefer this to adding array_key_exists checks or the likes for everything in the php)

1 Answer 1

1

Just assign empty strings as fallback values, like this:

MyService.get({
    action: 'create',
    email: email || '',
    username: username || '',
    password: password || ''
}), // ..

But let me tell you this: relying on client-side validation only is usually a bad practice. I hope this is some internal project which will never have some evil-doers preying upon the requests led astray. Otherwise you'd better use isset|empty in your server code.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that did it. And this is actually just some middleware that's between this server and an api server so I don't have to do anything with jsonp and can edit some stuff before sending it to the api. The api does all the checking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.