0

I have html form, javascript ajax code and php code that sends email.

I have one problem.

My checkboxes are made with span (see code). How to check if this span with class checkmark are checked.

My html:

<form id="obrazec" class="form-inline"> <input class="okno" type="email" id="email" placeholder="Email" name="email" required> <input type="checkbox" id="trgovina" value="" name="trgovina">Spletna trgovina <span class="checkmark"></span> <input type="checkbox" id="oglasevanje" value="" name="oglasevanje">Oglaševanje <span class="checkmark"></span> <input type="checkbox" id="trgovina" value="" name="strani">Spletna stran <span class="checkmark"></span> <div class="submitdiv"> <button class="submit" id="potrdi" type="button" onclick>POŠLJI</button> </div> </form> 

My javascript:

<script type="text/javascript"> $(document).ready(function(){ $("button#potrdi").click(function(){ event.preventDefault(); var formData = $('#my_form').serialize(); var email = $("#email").val(); var boolMAIL = email.includes("@"); var dataString = 'email=' + email; if(boolMAIL == true ){ $.ajax({ url:"action_page.php", type: "POST", data: dataString, success:function(result){ $( ".response" ).show(); $( ".response2" ).hide(); $( "#obrazec" ).hide(); $( ".prijavatext" ).hide(); } }); } else { $( ".response2" ).show(); } }); }); </script> 

My php:

 $email = $_POST['email']; $visitor_email = $_POST['email']; $email_from = '[email protected]'; $email_subject = "Novo povprasevanje"; $email_body = "Novo povprasevanje: $email.\n ". $headers = "From: $email_from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; $to = "[email protected]"; mail($to,$email_subject,$email_body,$headers); ?> 

It works ok (email) and i get mail but i don't know how to check if checkbox is checked because it is not check box actually but field:

<span class="checkmark"></span> 
4
  • Your checkboxes have names and ids. Commented Jun 10, 2019 at 11:09
  • Well if i add: $trgovina = isset($_POST['trgovina']) ? 1 : 0; to php and Commented Jun 10, 2019 at 11:10
  • var trgovina = $("#trgovina").val(); and .... + '&trgovina=' + trgovina; to javacript i allways get 1 Commented Jun 10, 2019 at 11:10
  • Do not write code in comments. Update your question. Commented Jun 10, 2019 at 11:10

2 Answers 2

1

To get the value of a specific property using Jquery you need to utilize the .prop() method. Since you want to know if a checkbox is checked or not the related property is checked.

var trgovina = $("#trgovina").prop("checked"); console.log(trgovina); 
Sign up to request clarification or add additional context in comments.

Comments

0

As you already use jquery you can get all fields from form (except buttons/type=submit) with serialize() method (and you already have this method in your code). Your javascript will look like:

$("button#potrdi").click(function(){ event.preventDefault(); var email = $("#email").val(); var boolMAIL = email.includes("@"); if(boolMAIL == true ){ $.ajax({ url:"action_page.php", type: "POST", data: $('#obrazec').serialize(), success:function(result){ $( ".response" ).show(); $( ".response2" ).hide(); $( "#obrazec" ).hide(); $( ".prijavatext" ).hide(); } }); } 

On server side your checked checkboxes will be available via their name attributes, $_POST['trgovina'] for example. And not checked checkboxes will not be in POST array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.