Do not link w3schools.com on this forum, please. Their information is often inaccurate and outdated. See here for more info.
That said, you should be careful of how your site is set up in many, many ways. I believe the exploit you mention in your post can be understood as part of a broader family of exploits where you either a) fail to validate user input (namely, the query string) or b) your site is configured to report errors which end up just echoing user input.
When you fail to validate data before working with it (e.g., making sure the user input in question contains an email address), then you are going to run into problems trying to use the user input in a particular way (e.g., sending an email to some user). [man]filter_var[/man] is a very useful function for validating user input.
If your site is configured to just echo a user's input and show an error like this:
echo "Your query string, " . $_GET["q"] . " doesn't make sense to me.";
Then abusers can just formulate some kind of crazy query string that displays javascript on your site. You need to think more broadly about how user input can be abused to display information on your site that you do not want there. A good rule of thumb might be to never echo user input at all when displaying error messages.