I think I understand your question.
Visitors to your web page complete a number of data fields, one of which is a select control. However, if there is a problem submitting the data to the database, then the values are erased when the page is re-displayed to the user. Is this correct?
If so, there are a couple of ways to solve it.
(1) Client-side field validation: Before the form is submitted, test the values of the fields to be sure they are valid before allowing the form submission to continue. Something like:
javascript/jQuery:
$(document).ready(function(){
$('#myFormID').submit(function(e){
var first_name = $('#firstname').val();
var last_name = $('#lastname').val();
if (firstname=="" || lastname==""){
alert('Please complete all fields')
e.preventDefault();
}
});
}); //END document.ready
(2) AJAX. Ajax is a method that allows you to submit data to the server (similar to using a form) without leaving/refreshing the page. If there is a problem, the page can be updated telling the user what to fix -- and all the user's changes are preserved. You can use a <form>
container with AJAX, but it is not necessary. You can just use DIVs and AJAX will work the same. In fact, when using a form, you always must use event.preventDefault()
to stop the form from trying to leave the page (its default behaviour).
javascript/jQuery:
$(document).ready(function(){
$('#myFormID').submit(function(e){
var first_name = $('#firstname').val();
var last_name = $('#lastname').val();
if (firstname=="" || lastname==""){
alert('Please complete all fields')
e.preventDefault();
}
});
$('#myFormID').submit(function(e){
var first_name = $('#firstname').val();
var last_name = $('#lastname').val();
if (firstname=="" || lastname==""){
alert('Please complete all fields')
e.preventDefault();
}
$.ajax({
type: 'post',
url: 'add_the_data.php',
data: 'fn=' +firstname+ '&ln=' +lastname,
success: function(d){
if (d == 1){
//Success
$('#firstname').val('');
$('#lastname').val('');
alert('Form submission was successful');
}else
//Fail
alert('Data was not submitted. Please try again.')
$('#firstname').focus();
}
}
});
}); //End Submit
}); //END document.ready
add_the_data.php
<?php
$fn = $_POST['fn'];
$ln = $_POST['ln'];
//Do your database entry here
$out = add_the_data_function($fn, $ln); //returns 1 or 0
echo out;
References:
More about AJAX, some simple examples