Let $X$ be an affine variety, with coordinate ring $A(X)$, and take an open set $U\subset X$. Is it true that any morphism of varieties $U\to \mathbb P^{n-1}$ is of the form $x\mapsto (g_1(x):\dots:g_n(x))$, for $g_1,\dots,g_n\in A(X)$ such that $X-Z(g_1,\dots,g_n)\supset U$?
The converse implication seems easily true; instead the direction of the part in italics is surely true if $X=\mathbb A^1$, or $X=\mathbb A^1-\{0\}$, and $n=2$; but if $X=\mathbb A^2$ or $X=\mathbb A^2-\{0\}$, I already don't understand how to extend the proof. Am I trying to prove something true at least? Thank you
I'll explain Sasha's comments in a bit more detail, since they comes from a more sophisticated understanding of morphisms into projective space. Overall, what you've written is generally incorrect since it usually gives a strict subset of the set of maps from a given variety to $\mathbb{P}^n$.
Rather than regular functions, we recognize the $g_i$ as sections of a certian line bundle on $U$(ie. an algebraic vector bundle of rank one). With the tensor product, the isomorphism classes of line bundles form a group called the Picard group. When the Picard group is trivial, every line bundle is isomorphic to the trivial bundle, so we see that sections correspond to regular functions. This should explain Sasha’s comments.
Conceptually let's understand why line bundles come up here. For simplicity assume we're working over $\mathbb{C}$, and let $V$ be any variety. Note that the data of a map $f: V \to \mathbb{CP}^n$ can be contained in the set $L = \{(v, x) \in V \times \mathbb{C}^{n + 1}\;|\;x \in \langle f(v) \rangle \}$. Now, we have a natural projection $\pi: L \to V$ which is in fact regular, and turns $L$ into an algebraic line bundle. Although it's natural, this line bundle isn't as useful as its dual, which lets us recover $f$ from its sections.
In this setup, instead of taking $L_v \subset \mathbb{C}^{n + 1}$ we take its dual to be the quotient $\mathbb{C}^{n + 1} \to L_v^*$ to get a global map of vector bundles $V \times \mathbb{C}^{n + 1} \to L^*$. Now $V \times \mathbb{C}^{n + 1}$ has $n + 1$ global sections $e_0, \dots, e_n$ corresponding to the coordinates, and we denote $s_0, \dots, s_n$ the induced sections of $L^*$. Then, it follows from the preceding construction that $f(v) = [s_0(v), \dots, s_n(v)]$, so we do in fact recover a similar formula to the one you mentioned. Conversely, given an algebraic line bundle and $n+1$ sections which don’t simultaneously vanish, we can define a morphism $f: V \to \mathbb{P}^n$ as above.
With some work you can actually show that this defines a universal property for projective space. It also lets us give a cases when what you stated is true.
Theorem: Let $X$ be a normal affine variety. If $A(X)$ is a UFD, then every line bundle on $X$ is trivial.
From this, we see that any map from $\mathbb{A}^n \to \mathbb{P}^m$ will be given by regular functions which don't all simultaneously vanish. I based the discussion of line bundles and projective morphisms on chapter 5 of the paper 'The Structure of Algebraic Threefolds' by Kollár. This theorem can be found in more generality in Hartshorne II.6.