Show Form Submitted Data Values With Php Validation Without Refresh
I'm trying to figure out how to make some data available for editing right after submission when it is submitted via HTML form and stored as a cookie... On top, form validation: &l
Solution 1:
You can do this only via jquery, ajax
to get cookie value -
Jquery var ckVal= $.cookie("cookiename")
javascript -
functionreadCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
returnnull;
}
Solution 2:
1st : As you mentioned in comment
Both form and php
code in same page
so simple submit the form
like this . No need ajax
.
2nd : if any error means you simple show the error message
and place the submited value
in value attribute
like this
value="<?phpif(isset($_POST['myname'])){ echo$_POST['myname']; } ?>"
PHP :
<?phpif(isset($_POST['send'])){
if(strlen($_POST['myname']) > 2){
// here you can do whatever you want // header('Location:next_page.php');
}else{
$error[] = 'Your name is required! and it should be minimum three character ';
}
}
if(isset($error)){
foreach($erroras$error){
echo'<p style="background: red;">'.$error.'</p>';
}
}
?>
The HTML form with PHP:
<formmethod="post"><inputname="myname"type="text"value="<?phpif(isset($_POST['myname'])){
echo$_POST['myname']; } ?>"placeholder="Your Name"><inputname="send"type="submit"value="send"></form>
Solution 3:
In Web2, We can do this by using AJAX. Simply hit ajax on form sumbit and
$('#submit_btn').on('click', function (e) {
e.preventDefault();
var $this = $('#form_id');
$.ajax({
url: 'setcookie.php',
data: $this.serialize(),
dataType: 'html',
type: 'POST',
beforeSend: function () {
// show loader
},
success: function (html) {
// Show success message// Set value of input elements by reading cookie in JS
},
error: function () {
// Show ValidationError
}
}); });
In cookie.php put server side validation on form input.You can make ajax on same page also if you wanna put code on same page.
Post a Comment for "Show Form Submitted Data Values With Php Validation Without Refresh"