Question

I have an HTML 5 form page. I know the method attribute action in the form tag determines what to do once submit button his hit.

So far I have something like this: <form action="form_action.php" method="post". I know some form's in HTML5 have a required attribute, but I understand that the PHP code should also do the required checking for security purposes.

If a person hits submit, I would like the required messages to pop up on the same html5 form page rather than completely redirecting to my php code. Can someone please explain how to do this or provide references?

I read something about the superglobal _SERVER(["PHP_SELF"]) but if I use this, how do I pass variables to my PHP script? I will be doing some SQL in PHP too.

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

Any help appreciated. Thanks.

Was it helpful?

Solution

If you want to process the contents of a form before they are sent to the server (e.g. on the client side), you have to do it using JavaScript, not PHP.

For an introduction on how to do that, try to google "javascript form validation" or something similar. A good starting point could be this tutorial. You may also want to look at jQuery which makes writing JavaScript easier and has become the standard way of writing JavaScript code that works in all browsers.

OTHER TIPS

You could use header("Location: first_page.php") on your action.php page to redirect to your first page (eventually with some options) if the inputs are not correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top