Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ajax check to allow checkbox completion 1

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
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.';
}

}

?>
 
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.
 
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
 
Sorry, was having a brain meltdown when posting. Can a mod move it to Javascript please?
 
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Thanks southbeach, I have got it working thanks to your logic suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top