People change their minds. Is that illegal? Maybe they had the intention to only be in the US temporarily at first, but now they'd like to get permanent residence. Why shouldn't they be able to apply for it, from the US, while still on the temporary visa?
Then the administration can say yes or no, in the same way that they can say yes or no to someone applying from abroad.