×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

ajax check to allow checkbox completion

ajax check to allow checkbox completion

ajax check to allow checkbox completion

(OP)
Hi

I only want users to be able to tick a checkbox if other areas of the system are complete. I can check whether the other areas have been completed when the checkbox is ticked and pass a message back if the other areas haven't been completed but the checkbox stays selected. How do I make the checkbox unselected if the check fails?

Here's what I have

CODE

<script type="text/javascript">
function checkCompleted() { 
$.ajax( {
type: 'POST',
url: 'checkCompleted.php',
data: { checked_box : $('input:checkbox:checked').val()},

success: function(data) {
$('#response').html(data);
}
} );
}
</script>

<input type="checkbox" name="checked_box" value="1" onclick="checkCompleted()">
<div class='#response'></div>

checkCompleted.php:

<?php
include "config.php";

if (isset($_POST['checked_box'])) {

//connect to database and test for completion of other fields

if (!$dataCompleted) {
echo 'You have not completed the other areas, please complete the other areas before ticking this box.';
}

}

?> 

RE: ajax check to allow checkbox completion

Not with PHP.

Only if the entire document (or the 'part' that includes the element you want to affect) is being reloaded via AJAX, can PHP have an effect on an already served document.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.

RE: ajax check to allow checkbox completion

This is not really a PHP question. PHP cannot affect your form fields form an ajax call.

You will have to use Javascript to change the status of the checkbox. $('input:checkbox:checked').val(false)

I would check the completion of the other fields before actually firing off the ajax request. but if you must do the check in PHP, send back a value from the PHP script to the ajax request you can check for, and then alter the checked property of the checkbox accordingly.




----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: ajax check to allow checkbox completion

(OP)
Sorry, was having a brain meltdown when posting. Can a mod move it to Javascript please?

RE: ajax check to allow checkbox completion

As other have said, this is a JS question not a PHP question. The simple answer may be, to add something like

CODE

$('#check_box_id').prop('checked')=false; 
once you figure the FAIL state.

That said, I usually set a class in fields that must be filled prior to submitting a form and call a method to check if they are all filled in

CODE

function mustFillCheck() {
  var OK=true; $('.forceMustFill').switchClass('forceMustFill','mustfill');
  $('.mustfill').each(function(i, obj) {
    if($(obj).val() === '') 
    { $(obj).switchClass('mustfill','forceMustFill'); OK=false; }
  });
  return(OK);
} 
The code checks for fields with class="mustfill" and if empty, they are set to "forceMustFill" and a FALSE value is returned so that the calling routine can take action accordingly.

The CSS are defined any way I want to easily and clearly identify the fields that must be filled. For example

CODE

.mustfill { background-color: #FFCCCC; }
.forceMustFill { background-color: #FFCCCC; border: 1px solid #red; } 

So, when required fields are left blank, the code changes the property of the fields by placing a "red" border around them ... thus making it easy for user to identify what was left incomplete ...

To call the method, you will use something like this

CODE

onclick="javascript: if(!mustFillCheck()) { return false; } else { processForm(); } 

Hope this points you in the right direction!

--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: ajax check to allow checkbox completion

(OP)
Thanks southbeach, I have got it working thanks to your logic suggestions.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close