/    Sign up×
Community /Pin to ProfileBookmark

Form validation without refresh

Right now, I have a HTML form… it inserts text boxes into a MySQL database after clicking a button which executes a seperate php file to do these inserts for the form “action.” I want to make sure that some of the required text box fields are not empty before it proceeds to execute the script. I can’t have php echo or alert because I do not want the user losing all of the info he typed in because he forgot a required field. (because of the refresh) // is there anyway to do something like this:

form action=”foo();”

javascript…
foo() {
// check that text boxes arent empty
// if they arent… execute php insert script
}

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Nov 24.2008 — Below is a link that tells you how to do form validation with javascript. You still want to validate the info with your php code because users can turn off javascript. Also to prevent users from entering in their info again after the page refreshes you can set the form values equal to the post are get values so it won't clear the info.

http://www.w3schools.com/js/js_form_validation.asp
Copy linkTweet thisAlerts:
@NogDogNov 24.2008 — Another option is to have the form display and the form validation/execution all run through the same controller script. In this way the user-submitted values can be retained if there is an error. At a somewhat simplified level:

[code=php]
<?php
if(isset($_POST['some_field'])) // form was submitted
{
$errors = array();
require_once 'validation_script.php';
if(form_is_valid()) // function in the required script; populates $errors on error
{
require_once 'processing_script.php';
if(process_form() == true) // function in the required script
{
include "success_page.php";
exit; // all done here
}
else
{
$errors[] = ("Sorry, the system was not able to process your request.");
}
}
}
// display the form:
require 'form_page.php';
[/code]

Then in form_page.php you can populate fields from the post data if it exists, plus output any errors if they exist:

form_page.php:
[code=php]
<html><head><title>Example</title></head><body>
<?php
if(!empty($errors))
{
foreach($errors as $err)
{
echo "<p class='error'>$err</p>n";
}
}
?>
<form action="" method="post">
<p>Example: <input type="text" name="example" value="<?php
if(isset($_POST['example']))
{
echo htmlspecialchars($_POST['example'], ENT_QUOTES);
}
?>" /></p>
<p><input type="submit" value="Submit" />
</form>
</body></html>
[/code]
Copy linkTweet thisAlerts:
@SyCoNov 24.2008 — I've taken to using a PHP function to validate the form, using xajax to call the PHP function onclick of a submit button and returning the submit false with jS. If the user has jS enabled they all the benefits of validation without page reload. If they disable javascript they still get validated by the php function after the page reloads and there is only one validation function to maintain.
Copy linkTweet thisAlerts:
@NogDogNov 24.2008 — I've recently taken to using the CodeIgniter framework and making use of its Form_validation class. ?
×

Success!

Help @Beakman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...