Note that there are some explanatory texts on larger screens.

plurals
  1. POValidating checkbox fails (Form > jQuery validation > PHP)
    text
    copied!<p>I have been seriously trying to solve this issue for hours now, have read plenty of responses here but it just won't work. </p> <p>I mark a post as important in my database by setting a single column to either 0 (not important) or 1 (important). The state should be determined by checking (or not checking) a text form within the form on my site. While this works all fine when I send the form straight to the php processing script, it doesn't work when validating the form with jQuery. For some reason in this case it ALWAYS marks the post as important, whether the checkbox is checked or not. </p> <p>Check out the code:</p> <p>FORM</p> <pre><code>&lt;form method="POST" action=""&gt; &lt;fieldset&gt; &lt;legend&gt;Form Title&lt;/legend&gt; &lt;label&gt;Title *&lt;/label&gt; &lt;input type="text" name="headline" placeholder="..."&gt; &lt;label&gt;Contentn *&lt;/label&gt; &lt;textarea rows="10" cols="40" name="content"&gt;&lt;/textarea&gt; &lt;label class="checkbox"&gt; &lt;input type="checkbox" name="important" id="important" value="checked"&gt; Important &lt;/label&gt;&lt;br&gt; &lt;input type="submit" class="btn btn-success submit-post" value="Send"&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>Validation Part</p> <pre><code>$(document).ready ( function() { $('.submit-post').click(function() { // VALIDATION PART var form = $(this).closest('form'); var headline = form.find('input[name=headline]'); var content = form.find('textarea[name=content]'); if( $('input[type=checkbox]:checked') ) { var important = 1; } else { var important = 0; } var passed = true; if (headline.val()=='') { headline.addClass('highlight'); passed = false; } else { headline.removeClass('highlight'); } if (content.val()=='') { content.addClass('highlight'); form.find('.errorMessage').fadeIn(); passed = false; } else { content.removeClass('highlight'); } if (!passed) { return false; } //organize the data properly var data = 'headline=' + headline.val() + '&amp;content=' + content.val() + '&amp;important=' + important.val(); //disabled all the text fields form.find('.text').attr('disabled','true'); //show the loading sign $('.loading').show(); //start the ajax var request = $.ajax({ type: "POST", url: "process.php", // data: data, cache: false, success: function (html) { if (html == "true") { form.find('.sendMessage').attr('disabled','true'); form.find('.successMessage').fadeIn(); } else { alert("There was an error with your entry. Please try again later while we fix it! :)"); } }, error: function(jqXHR, textStatus, error) { // Fail alert("Form Error: " + error); } }); //cancel the submit button default behaviours return false; }); </code></pre> <p>});</p> <p>PHP</p> <pre><code> &lt;?php include 'functions.php'; // Check if form is submitted if ( $_SERVER['REQUEST_METHOD'] == "POST" &amp;&amp; !empty($_POST['headline']) &amp;&amp; !empty($_POST['content']) ) { // Get job details $title = ($_POST['headline']); $content = ($_POST['content']); $compensation = ($_POST['compensation']); $category = ($_POST['category']); // Check whether job was marked as 'important' if ( isset($_POST['important']) ) { $important = 1 ; } else { $important = 0; } // Get author details $author_email = $_SESSION['email']; $author_id = $conn-&gt;prepare("SELECT id FROM users WHERE email = ? "); $author_id-&gt;bindParam(1, $author_email, PDO::PARAM_STR); $author_id-&gt;execute(); $get_author_id = $author_id-&gt;fetch(PDO::FETCH_ASSOC); $get_author_id = $get_author_id['id']; // Here I add the information to the database echo json_encode(true); } else { echo "Error: Error submitting data to database"; exit(); } ?&gt; </code></pre> <p>Anyone an idea what is going on? I tried in all possible ways but I can't get it working! I would really appreciate your help. </p> <p>Many thanks!!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload