The elements in the third period of the periodic table has following electron configurations and they achieve their most stable ionic forms by gaining or losing indicated electrons (final electron configurations of each is also shown):
- $\ce{Na}$: $\mathrm{[Ne]3s^1} \quad \ce{ ->[-e^-] Na+}: \mathrm{[Ne]}$
- $\ce{Mg}$: $\mathrm{[Ne]3s^2} \quad \ce{ ->[-2e^-] Mg^2+}: \mathrm{[Ne]}$
- $\ce{Al}$: $\mathrm{[Ne]3s^23p^1} \quad \ce{ ->[-3e^-] Al^3+}: \mathrm{[Ne]}$
- $\ce{Si}$: $\mathrm{[Ne]3s^23p^2}$
- $\ce{P}$: $\mathrm{[Ne]3s^23p^3} \quad \ce{ ->[+3e^-] P^3-}: \mathrm{[Ne]3s^23p^6}\ce{#[Ar]}$
- $\ce{S}$: $\mathrm{[Ne]3s^23p^4} \quad \ce{ ->[+2e^-] S^2-}: \mathrm{[Ne]3s^23p^6}\ce{#[Ar]}$
- $\ce{Cl}$: $\mathrm{[Ne]3s^23p^5} \quad \ce{ ->[+e^-] Cl-}: \mathrm{[Ne]3s^23p^6}\ce{#[Ar]}$
- $\ce{Ar}$: $\mathrm{[Ne]3s^23p^6}\ce{#[Ar]}$
Briefly, the first element of 3rd period, $\ce{Na}$ can loose one electron to gain neon electron configuration (octet) than gaining 7 electrons to achieve argon electron configuration as shown in entry (1) above. Consequently, loosing one electron make it $\ce{Na+}$. Similarly, the seventh element of 3rd period, $\ce{Cl}$ can gain one electron to achieve argon electron configuration (also an octet) than loosing 7 electrons to achieve neon electron configuration as shown in entry (7) above. As a result of gaining one electron, $\ce{Cl}$ becomes $\ce{Cl-}$.
For $\ce{Si}$, it can gain 4 electrons or loose 4 electrons. However, either is very difficult to achieve so that its nature (along with that of carbon) is explained well using molecular orbital theory.
Your list contains compounds with three cations: $\ce{M+, M^2+,}$ and $\ce{M^3+}$. Those are $\ce{Na+, Mg^2+,}$ and $\ce{Al^3+}$, respectively. Even though it wasn't shown, all of these have octets in the last cell as explained above.
Also, your list contains compounds with only two anions: $\ce{X-}$ and $\ce{X^2-}$. Those are $\ce{Cl-}$ and $\ce{S^2-}$, respectively. Those also have octets in the last cell as explained above.
Since you know the cations and anions in the list, it's now easy to make the compounds.