Jump to content

[SOLVED] validate form with checkbox


elmas156

Recommended Posts

Hey guys,

 

I'm trying to validate a form using javascript and I have everything working except for one checkbox.  What I would like to happen is, if the checkbox is not checked then pop up an alert to say that it has to be checked before continuing.  I've tried several different ways with javascript but the form goes through with no alerts regardless of whether the checkbox is checked or not.  Now I'm wondering if there is any way to accomplish what I need to do with php and if so, where do I start?  If not what would make this work the way I'm trying it?

 

Here's what I've tried:

<head>

<script language="javascript">

function validate(form) { 

if (form.accept.value == "true")
{

    alert("You must accept the Terms of Service before continuing.");
	form.accept.focus();
	return false;
}

} 
</script>

</head>

<body>

<?php

echo "<form action=\"signup.php\" method=\"POST\">";
echo "<input name=\"accept\" type=\"checkbox\" id=\"accept\" value=\"true\">";
echo "<input name=\"submit\" type=\"submit\" value=\"I accept, continue to my account.\" onClick=\"return validate(form)\">";
echo "</form>";
</body>

Link to comment
https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/
Share on other sites

I understand that this is not the javascript forum.  My question is how can this same effect be accomplished using php?  Though it's not common, users can disable javascript on their browsers and get around a javascript form validation, making it obsolete.  I figured out how to make it work with javascript but I would rather use something that wouldn't be so easy to disable.  Thanks for your help though.

I understand that this is not the javascript forum. My question is how can this same effect be accomplished using php? Though it's not common, users can disable javascript on their browsers and get around a javascript form validation, making it obsolete. I figured out how to make it work with javascript but I would rather use something that wouldn't be so easy to disable. Thanks for your help though.

 

The only way I know of is to check if it's set on the signup page.

 

if (!isset($_POST['checkboxname']))

I misread your question, I have a habit of skipping last sentences, sorry.

 

In the simplest form...

if(empty($_POST['accept'])) {
  echo "Check box not checked!";
}

 

That should give some sort of a starting point. Google "php form validation" for tons of info about various types of validation.

Hi,

As now your code is good, but you need to do some change in the Javascript code like below

 

Solution:

You just change the condition from

 

    if (form.accept.value == "true")

to,

    if (!form.accept.checked)

 

This is enough to meet your requirment.

 

Please try and let me know if any question  :)

 

Example: I just tried this as normal html page (test.html)

<html>

<head>

 

<script language="javascript">

 

function validate(form) {

 

if (!form.accept.checked)

{

 

    alert("You must accept the Terms of Service before continuing.");

form.accept.focus();

return false;

}

 

}

</script>

 

</head>

 

<body>

<form action="signup.php" method="POST">

<input name="accept" type="checkbox" id="accept" value="true">

<input ame="submit" type="submit" value="I accept, continue to my account." onClick="return validate(form)">

</form>

</body>

</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.