mercredi 3 mai 2017

A single checkbox input incorrectly passing the value

I've spent a few hours looking at all the relevant answers here and I'm still not able to figure out why this is happening.

I'm trying to capture a yes/no type of answer from the users.

I'm using following checkbox input. I could use radio buttons and possibly make it work, but there is a reason I want it to be a single check box user interface.

PROBLEM: No matter whether I check or uncheck the checkbox a '0' is always passed to the server. When I check the checkbox I expect '1'.

html :

<input type="hidden" name="hardtofind" id="hardtofindField" value="0">
<input type="checkbox" name="hardtofind" id="hardtofindField" value="1">I'm having hard time finding information I'm looking for<br>
<input type="button" id="submitButton" class="rbutton" value="Submit">

jquery:

<script>
jQuery(document).ready(function(){
jQuery("#submitButton").click(function(event){
var hardtofind = jQuery("#hardtofindField").val();
jQuery.ajax({
url : "../send_form_email.php",
data : {
"hardtofind" : hardtofind,     
},
method : "GET",
success : function(){
jQuery("#submitButton").hide();
jQuery("#successMsg").show();
}
});
event.preventDefault();
});
});
</script>

php:

session_start();
if(isset($_GET['hardtofind']) && !empty($_GET['hardtofind']) ){
$hardtofind = $_GET['hardtofind']; 
}

Thank you so much for your time.




Aucun commentaire:

Enregistrer un commentaire