I want to display the message when user clicks on Others checkbox. There are other checkboxes too so I want to compare checkbox value and if Others checkbox is selected then I want to display the message. This is my code:
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
php part:
if(isset($_POST['submit']))
{
if(!empty($_POST['subject']) && $_POST['subject']=="Others") {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
This code is not working. What changes should I make??
As subject[] is an array in
<input type="checkbox" name="subject[]" value="Others">
is an array type. So you should validate subject field as array as follows:
if (isset($_POST['submit'])) {
if (!empty($_POST['subject']) && in_array("Others", $_POST['subject'])) {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
You can try something like given below. you are free to use in_array() but if you want to use the second condition with it use foreach like this.
if (in_array("Others", $_POST['subject'])) {
echo 'selected ';
}
else{
//do something
}
OR
if(isset($_POST['submit']))
{
$results=$_POST['subject'];
foreach ($results as $result) {
if ($result==='Others') {
echo 'selected ';
}
else{
//do something
}
}
}
<?php
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
if(isset($_POST['subject'])){
$subject = $_POST['subject'];
if(in_array('Others', $subject)){
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}else{
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}
?>
<form method="POST" action="">
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
</form>
Related
I cant seem to get my php script to recognize that the checkbox in my form is checked.
I want to accomplish this:
If checkbox is checked, the php script should submit to my DB.
PHP:
<?php
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
//submit
} else {
//do nothing
}
}
?>
FORM:
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
NOTES:
Im using Jquery to keep checkbox checked during session.
Im using CSS to customize checkbox.
I dont know if these two might corrupt anything.
Hope u can help.
Your code is fine it is working
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
echo "Approved";
} else {
echo "Not Approved";
}
}
<form action="index.php" method="post">
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
</form>
If the checkbox isn't checked then the value shouldn't be coming through in the first place, so this should work;
if(isset($_POST["testvariabel"])){
//whatever you wanna do here
}
or if it's always coming through...
Change the markup to something like this, setting a value property
<input type="checkbox" class='form' onclick="this.value=!this.value" value=true name="checkbox[]" />
and to get the values submitted, use a simple loop
if($_POST['checkbox'] == 0){
echo $checkbox . ' ';
}
So I made a registration form such that the user must select one of the three radio options before continuing. However if the user didn't choose an option and clicked submit I want to display at least a message that says "please choose an option" or something. Here is my code so far:
<?php
$reg_type = "";
function setclick()
{
if(isset($_POST["Submit"]))
$clicked=1;
else
$clicked=0;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Reg_type"])) {
$clicked=1;
header('Location: Registration_1.php');
}
else {
$reg_type = $_POST["Reg_type"];
header('Location: Registration_2.php');
}
}
echo $clicked;
?>
<form name="frmtype" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
<?php
setclick();
if($clicked)
echo "Please select an option";
?>
I'm having a real hard time getting the logic down to display the error message if they didn't choose an option and clicked submit.
Your setclick() function will always set $clicked to true if the form has been posted. Since you then call setclick() after your main php block, it will be true whether or not the rest of the logic has changed it. Try this:
if(isset($_POST['Reg_type']) && $_POST['Reg_type'] != ''){
$reg_type = $_POST['Reg_type'];
header('Location: Registration_2.php');
} else $clicked = true;
and then down the bottom of the page under the form:
if($clicked) echo "Please select an option";
<?php
$reg_type = $_POST['Reg_type'];
if($reg_type == "1"){
//action 1
} else if($reg_type == "2"){
//action 2
} else if($reg_type == "3") {
//action 3
} else {
// no radio button is checked. display the message
$message = "Please select an option";
}
?>
Try this
<script>
function checkform(){
var checked = false;
var buttons = document.getElementsByTagName('input')
for (var i = 0; i < buttons.length ; i++) {
if (buttons[i].checked) {
checked = true;
break;
}
}
return checked;
}
</script>
<form onsubmit="return checkform()">
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
Check the fiddle
This should get you started. Please be aware that this script is selecting elements by tag name, which means it will pick up any input in the form. If you are OK with using jQuery I'd highly recommend it, it will make this kind of tasks trivial.
I am not very good at programming, I try to make the following radio buttons:
<input type="radio" name="yesno1" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check1">*please select</div>
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check2">*please select</div>
<input type="radio" name="yesno3" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno3" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check3">*please select</div>
<input type="radio" name="yesno5" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno5" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check5">*please select</div>
<input type="button" value="submit" id="submit">
here is my javascript:
$("#submit").click(function(){
if (!$("input[name='yesno1']):checked").val()) {
$("#check1").show();
}
if ($("input[name='yesno1']):checked").val()) {
$("#check1").hide();
}
if (!$("input[name='yesno2']):checked").val()) {
$("#check2").show();
}
if ($("input[name='yesno2']):checked").val()) {
$("#check2").hide();
}
if (!$("input[name='yesno3']):checked").val()) {
$("#check3").show();
}
if ($("input[name='yesno3']):checked").val()) {
$("#check3").hide();
}
if (!$("input[name='yesno5']):checked").val()) {
$("#check5").show();
}
if ($("input[name='yesno5']):checked").val()) {
$("#check5").hide();
}
});
I know I am doing a clumsy way, what I want is that, if only one radio button is checked, the other three error messages should show, if two is checked, the other two should show, if three check , the other one should show, if no one checked, all of them show. However only the last case works at the moment, I don't know where I am doing wrong, can any of you help me, thanks in advance.
In addition, I am going to pass the data to the php file, so is it possible if I do:
$yesno1 = $_POST['yesno1'];
$yesno2 = $_POST['yesno2'];
$yesno3 = $_POST['yesno3'];
$yesno5 = $_POST['yesno5'];
Thanks for the kind help:)
Try this using jQuery is method. Also you can use else block instead of checking for if every time.
$("#submit").click(function(){
if (!$("input[name='yesno1']").is(':checked')) {
$("#check1").show();
}
else{
$("#check1").hide();
}
if (!$("input[name='yesno2']").is(':checked')) {
$("#check2").show();
}
else{
$("#check2").hide();
}
if (!$("input[name='yesno3']").is(':checked')) {
$("#check3").show();
}
else{
$("#check3").hide();
}
if (!$("input[name='yesno5']").is(':checked')) {
$("#check5").show();
}
else{
$("#check5").hide();
}
});
I'm a complete newbee in php (started just last week)
Issue is like this:
Basically, I was trying to ensure that once a sub-form is filled, then it is not altered. So, I used !isset to display the sub-form (i.e. if !isset is true) and if !isset is false, then it hides that sub-form and shows the next sub-form (the individuals form has only been designed).
<?php include($_SERVER['DOCUMENT_ROOT'].'/officespace/includes/functions.php');
echo'<html>
<head>
<title> Create </title>
</head>
<body>';
if(!isset($_POST["Category"])){
/* if no category is selected, then this code will display the form to select the category*/
Echo "Pls Select Category before clicking on Submit Category";
/* Breaking out of PHP here, to make the form sticky by using a php code inside form action*/
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Select the Category of Person: </legend><br />
<input type="radio" name="Category" value="Individual" /> Individual<br /><br />
<input type="radio" name="Category" value="Company, Pvt Ltd" /> Company, Pvt Ltd<br /><br />
<input type="radio" name="Category" value="Company, Ltd" /> Company, Ltd<br /><br />
<input type="radio" name="Category" value="Partnership Firm" /> Partnership Firm<br /><br />
<input type="radio" name="Category" value="LLP Firm" /> LLP Firm<br /><br />
<input type="radio" name="Category" value="HUF" /> HUF<br /><br />
<input type="submit" name='Submit Category' value="Submit Category" /><br />
</fieldset>
</form>
<?php
} Else {
$Category = $_POST["Category"];
Echo "$Category";
Echo "<br />";
/* Using swich statement to test the value of Category, and accordingly echo appropriate forms*/
switch ($Category) {
case "Individual":
if(!isset($_POST['Submit_Data_for_Individual'])){
//if no data for individual is submitted,
//then this code will display the form to enter and submit data for Individual
Echo /*displays message*/
"Pls Enter the Data for the Individual";
?>
<form action="<?php Echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<br />
First Namee: <input type="text" name="Individual_First_Name" />
<br />
Middle Name: <input type="text" name="Individual_Middle_Name" />
<br />
Last Name: <input type="text" name="Individual_Last_Name" />
<br />
Date of Birth: <input type="text" name="date_of_birth/incorporation" />
<br />
Gender:
<br />
<input type="radio" name="Gender" value="male" /> Male
<br />
<input type="radio" name="Gender" value="female" /> Female
<br />
Email 1: <input type="text" name="email_1" />
<br />
<input type="submit" name="Submit_Data_for_Individual" value="Submit Data for Individual" />
</fieldset>
</form>
<?php
}Else
{
$email_1 = $_POST["email_1"];
$Gender = $_POST["Gender"];
validate_email($email_1); // this is a custom function which i made
Echo $Gender; // just to see if value has been passes. problem lies here because its not showing anything
// run other validations here
// and if all valid then run mysqli insert query for individuals record
}
break;
case "Company, Pvt Ltd":
echo "Company, Pvt Ltd";
break;
case "Company, Ltd":
echo "Company, Ltd";
break;
case "Company, Ltd":
echo "Company, Ltd";
break;
case "Partnership Firm":
echo "Partnership Firm";
break;
case "LLP Firm":
echo "LLP Firm";
break;
case "HUF":
echo "HUF";
break;
case NULL:
echo "Error: nothing selected";
break;
}
}
echo '</body>
</html>';
?>
Is see one problem immediately.
You are checking for a form input called Submit Data for Individual, but that is the value of a submit button which has no name attribute. Set a name='submit-data' attribute on the submit button and change the conditional to check for the name instead of its value:
// This will never match.
if(!isset($_POST["Submit Data for Individual"])){
// Change it to
if(!isset($_POST["submit-data"])){
// Then change this
<input type="submit" value="Submit Data for Individual" />
// To this:
<input type="submit" name='submit-data' value="Submit Data for Individual" />
Additionally, the default case in a switch statement uses a default keyword:
// You may safely change this:
case NULL:
echo "Error: nothing selected";
break;
// To this:
default:
echo "Error: nothing selected";
break;
Addendum:
The following code is never reachable, since the form posts to another script, create.php. If you change the <form> action attribute to post back to <?php $_SERVER['PHP_SELF'];?> instead of to create.php, you should see the else case. Right now, it doesn't work because your if tests that $_POST["submit-data"] is set. It can only be set if the form has been submitted, but the form submits to an external script.
// This else case can never be reached...
}Else
{
validate_email($_POST["email_1"]); // this is a custom function which i made
Echo $_POST["Gender"]; // just to see if value has been passes. problem lies here because its not showing anything
// run other validations here
// and if all valid then run mysqli insert query for individuals record
}
To fix this and see your Gender echoed out, temporarily change
<form action="create.php" method="post">
// change to
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
Addendum 2
You are checking if Category is set, but after posting the user form, it will not be:
// Change
if(!isset($_POST["Category"])){
// To check that the user form was not submitted
if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {
Then you need to test if the user form was submitted. Before the Else { $Category = $_POST['Category']; section, add an else if to process the user form.
if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {
// Show the Category form...
}
// Process the user form...
else if (isset($_POST['submit-data'])) {
validate_email($_POST["email_1"]); // this is a custom function which i made
Echo $_POST["Gender"];
}
// Now process the categories or show the user form...
else {
$Category = $_POST['Category'];
// etc...
}
Finally, remove the whole Else block from your individual case, as it cannot be used there.
How do I check in PHP whether a checkbox is checked or not?
If the checkbox is checked, then the checkbox's value will be passed. Otherwise, the field is not passed in the HTTP post.
if (isset($_POST['mycheckbox'])) {
echo "checked!";
}
you can check that by either isset() or empty() (its check explicit isset) weather check box is checked or not
for example
<input type='checkbox' name='Mary' value='2' id='checkbox' />
here you can check by
if (isset($_POST['Mary'])) {
echo "checked!";
}
or
if (!empty($_POST['Mary'])) {
echo "checked!";
}
the above will check only one if you want to do for many than you can make an array instead writing separate for all checkbox try like
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo htmlspecialchars($aDoor[$i] ). " ";
}
}
Try this
index.html
<form action="form.php" method="post">
Do you like stackoverflow?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
form.php
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['like']))
{
echo "<h1>You like Stackoverflow.<h1>";
}
else
{
echo "<h1>You don't like Stackoverflow.</h1>";
}
?>
</body>
</html>
Or this
<?php
if(isset($_POST['like'])) &&
$_POST['like'] == 'Yes')
{
echo "You like Stackoverflow.";
}
else
{
echo "You don't like Stackoverflow.";
}
?>
If you don't know which checkboxes your page has (ex: if you are creating them dynamically) you can simply put a hidden field with the same name and 0 value right above the checkbox.
<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">
This way you will get 1 or 0 based on whether the checkbox is selected or not.
I love short hands so:
$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";