0

I'm in need of help with some javascript handling a form. I'm quite new to this and don't know what to do next. I have two functions with a form: one to check if data is ok and another to merge checkboxes into one record. Is there a way to merge them into one fuction?

Form code:

<form id="formularz_wspolpraca"   name="Zapis na poradnik" method="post" target="_top" onsubmit="return SprawdzFormularz(this)">
<input type="text" id="email" name="email"/>
<input type="text" id="imie" name="imie"/>
<input type="text" id="nazwisko" name="nazwisko"/>
<input type="text" maxlength="12" size="12" id="pole_1" name="pole_1"/>
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3a" value="polecajacy"> 
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3b" value="projektant"> 
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3c" value="instalator"> 
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3d" value="ekspert"> 
<input type="hidden" name="pole_3" id="pole_3">
<input id="pp" type="checkbox" name="pp" checked=""/>
<input type="submit"  value="Wyślij">
</form>

Check data function:

function SprawdzFormularz(f) {
  if (f.email.value == "") {
    alert("Nie poda\u0142e\u015b/a\u015b adresu e-mail.");
    return false;
  }
  if (((f.email.value.indexOf("@", 1)) == -1) || (f.email.value.indexOf(".", 1)) == -1) {
    alert("Poda\u0142e\u015b/a\u015b b\u0142\u0119dny adres e-mail.");
    return false;
  }
  if (f.imie.value == "") {
    alert("Wype\u0142nij pole Imi\u0119. ");
    return false;
  }
  if (f.nazwisko.value == "") {
    alert("Wype\u0142nij pole Nazwisko. ");
    return false;
  }
  if (f.pole_1.value == "") {
    alert("Wype\u0142nij pole Nr telefonu. ");
    return false;
  }
  if ((f.pole_3a.checked == false) && (f.pole_3b.checked == false) && (f.pole_3c.checked == false) && (f.pole_3d.checked == false)) {
    alert("Wybierz zakres wsp\u00f3\u0142pracy");
    return false;
  }
  if (f.pp.checked == false) {
    alert("Musisz zgodzi\u0107 si\u0119 z Polityk\u0105 Prywatno\u015bci.");
    return false;
  }
  form.submit();
  return true;
}

Merge checkboxes function:

var form = document.getElementById('formularz_wspolpraca');
try {
  form.addEventListener("submit", mergeFuntion, false);
} catch (e) {
  form.attachEvent("onsubmit", mergeFuntion);
}

function mergeFuntion(event) {
  event.preventDefault();
  var boxes = document.getElementsByClassName('checkbox_wspolpraca');
  var checked = [];
  for (var i = 0; boxes[i]; ++i) {
    if (boxes[i].checked) {
      checked.push(boxes[i].value);
    }
  }
  var checkedStr = checked.join(' ');
  document.getElementById('pole_3').value = checkedStr;

  return false;
}

Looking forward to your responses, I'm quite lost.

1 Answer 1

1

You can simply call both functions in the onsubmit:

onsubmit="return SprawdzFormularz(this) && mergeCheckboxes(this)"

If you want to run the merge function even if the checking function fails, you can set variables:

onsubmit="check = SprawdzFormularz(this); merge = mergeCheckboxes(this); return check && merge"
Sign up to request clarification or add additional context in comments.

10 Comments

If I do that, checking function works fine but merging doesn't work. Should I change something in code of the function?
It only runs the merging code if the checking function succeeds, because && is a short-circuiting operator.
Sure I get it but even if checking suceeds merging wouldn't work. It sends empty field. Maybe there is a way to merge this functions into one?
I just looked closer at your code. Why does the merge function always return false?
I used function from response to this topic stackoverflow.com/questions/17935869/… . It worked alone but combined with other one created some problems. Im totally green in javascript, could be some rookie mistake.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.