javascript - Validate onsubmit and save form with ajax - important -
please me.. want validate form , send ajax.
validate without ''onsubmit="return validateform(this);"'' work.
but when form correct, send form (page refresh..) please patient , me..
this page on php. must validate js/jquery , send ajax.
it's important me, page new goob..
form:
<form action="index.php?page=insertcontact" id="ajax_form" enctype="multipart/form-data" method="post" onsubmit="return validateform(this);"> <div> <input type="text" id="name" name="name" placeholder="imię nazwisko:" /> <ul class="error"></ul> <input type="text" id="mail" name="mail" placeholder="adres e-mail:"/> <ul class="error"></ul> <div> <input type="text" id="phone_area" name="phone_area" placeholder="nr. kier.:"/> <div class="clear"></div> <ul class="error phone_area"></ul> </div> <div> <input type="text" id="phone" name="phone" placeholder="telefon kontaktowy:"/> <div class="clear"></div> <ul class="error phone"></ul> </div> <input type="text" id="title" name="title" placeholder="tytuł wiadomości:"/> <ul class="error"></ul> <textarea id="content_area" name="content_area" placeholder="treść wiadomości:"></textarea> <ul class="error"></ul> <input type="submit" name="submit" class="submit" value="wyślij formularz"/> <a href="#" id="send_form" title="wyślij">wyślij</a> </div> </form>
and js:
function validatename(name) { var reg = /^([a-za-z\-\. ]{1,50})$/; if (reg.test(name) == false) { return false; } else { return true; } } function validateemail(address) { var reg = /^([a-za-z0-9_\-\.])+\@([a-za-z0-9_\-\.])+\.([a-za-z]{2,4})$/; if (reg.test(address) == false) { return false; } else { return true; } } function validatephonearea(phone_area) { var reg = /^[0-9]{4}$/; var reg2 = /^[+]{1}[0-9]{2}$/; if ((reg.test(phone_area) || reg2.test(phone_area)) == false) { return false; } else { $('#ajax_form ul.phone_area').find('li').remove(); return true; } } function validatephone(phone) { var reg = /^[0-9]{7}$/; var reg2 = /^[0-9]{9}$/; if ((reg.test(phone) || reg2.test(phone)) == false) { return false; } else { $('#ajax_form ul.phone').find('li').remove(); return true; } } function validatetitle(title) { if ((title.length) > 100) { return false; } else { return true; } } function validatecontent(content_area) { if ((content_area.length) > 1000) { return false; } else { return true; } } function validateform(aform) { var tekst = ''; if (aform.name.value == "") { $('#ajax_form #name').next('ul').children().remove(); $('#ajax_form #name').next('ul').append('<li>wpisz swoje imię nazwisko</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } else if (aform.name.value != "") { $('#ajax_form #name').next('ul').children().remove(); if (!validatename(aform.name.value)) { $('#ajax_form #name').next('ul').children().remove(); $('#ajax_form #name').next('ul').append('<li>wpisz poprawnie swoje imię nazwisko</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } } if (aform.mail.value == "") { $('#ajax_form #mail').next('ul').children().remove(); $('#ajax_form #mail').next('ul').append('<li>wpisz swój adres e-mail</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } else if (aform.mail.value != "") { $('#ajax_form #mail').next('ul').children().remove(); if (!validateemail(aform.mail.value)) { $('#ajax_form #mail').next('ul').children().remove(); $('#ajax_form #mail').next('ul').append('<li>wpisz poprawnie swój adres e-mail</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } } if (aform.phone_area.value == "") { $('#ajax_form ul.phone_area').find('li').remove(); $('#ajax_form ul.phone_area').append('<li>wpisz numer kierunkowy</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } else if (aform.phone_area.value != "") { $('#ajax_form ul.phone_area').find('li').remove(); if (!validatephonearea(aform.phone_area.value)) { $('#ajax_form ul.phone_area').find('li').remove(); $('#ajax_form ul.phone_area').append('<li>poprawny nr. kier. np. +48 lub 0048</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } } if (aform.phone.value == "") { $('#ajax_form ul.phone').find('li').remove(); $('#ajax_form ul.phone').append('<li>wpisz swój numer telefonu</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } else if (aform.phone.value != "") { $('#ajax_form ul.phone').find('li').remove(); if (!validatephone(aform.phone.value)) { $('#ajax_form ul.phone').find('li').remove(); $('#ajax_form ul.phone').append('<li>wpisz poprawnie swój numer telefonu</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } } if (aform.title.value == "") { $('#ajax_form #title').next('ul').children().remove(); $('#ajax_form #title').next('ul').append('<li>wpisz tytuł wiadomości</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } else if (aform.title.value != "") { $('#ajax_form #title').next('ul').children().remove(); if (!validatetitle(aform.title.value)) { $('#ajax_form #title').next('ul').children().remove(); $('#ajax_form #title').next('ul').append('<li>tytuł za długi</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } } if (aform.content_area.value == "") { $('#ajax_form #content_area').next('ul').children().remove(); $('#ajax_form #content_area').next('ul').append('<li>wpisz treść wiadomości</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } else if (aform.content_area.value != "") { $('#ajax_form #content_area').next('ul').children().remove(); if (!validatecontent(aform.content_area.value)) { $('#ajax_form #content_area').next('ul').children().remove(); $('#ajax_form #content_area').next('ul').append('<li>treść za długa</li>').css('display', 'block'); tekst = "nieprawidłowe dane"; } } if (tekst != "") { return false; } else { alert('send'); // return false; $(function () { // $('#ajax_form').on('click', '.submit', function () { var $form = $(this).parents('form'); var senddata = $form.serialize(); var action = $form.attr('action'); // validateform(); $.ajax({ type: "post", url: 'index.php?page=insertcontact', data: senddata, datatype: 'html', cache: false, success: function (response) { alert(1); } }); return false; }); // }); // return true; } }
update validate , ajax work (page no refresh), form no post. meybe have bug in php..
<?php $title = ""; if (isset($_get['page']) && $_get['page'] != ''){ $page = $_get['page']; switch ($page){ case 'contact' : include "page.php"; break; case 'insertcontact': $name = $_post['name']; $mail = $_post['mail']; $phone = $_post['phone']; $title = $_post['title']; $content = $_post['content_area']; $lol = $name."\t".$mail."\t".$phone."\t".$title."\t".$content."\n"; $file = "baza.txt"; $fp = fopen($file, "a+b"); flock($fp, 2); fwrite($fp, $lol ); flock($fp, 3); fclose($fp); mysql_query("insert pages (pname, pmail, pphone, ptitle, pcontent) values ( '$name', '$mail', '$phone', '$title', '$content')"); header("location: index.php"); break; } } else { include "page.php"; } ?>
correct answer
1. john s post correct
2. correct :
if (tekst != "") { return false; } else { return true; }
tnx :)
first, not add on-click handler form's submit button. instead, add on-submit handler form. second, not inside validateform()
function.
place following code outside validateform()
function:
$(function() { $('#ajax_form').submit(function(e) { e.preventdefault(); var $form = $(this); if (validateform($form[0])) { $.ajax('index.php?page=insertcontact', { type: 'post', data: $form.serialize(), datatype: 'html', cache: false, success: function(html) { alert('response: ' + html); } }); } }); }
you have make validateform()
function returns true
or false
, depending on whether or not form valid.
Comments
Post a Comment