CheckBox html-php-mysql - php

my problem sir/ma'am is in checkbox html-php-mysql, if box is check then the value that will save in MYSql is "COMPLETED" and if it is not check the box then it will also save the value of "NOT COMPLETED" in mysql, but in my case it will save still NOT COMPLETED even it is check
<h1><span class="label label-primary left">Requirements</span></h1><br><br>
<div class="checkbox ">
<label><input type="checkbox" name="Card" value="Complete">
<input type="hidden" name="Card" value="Not Complete">High School Card (Form 138)</label>
</div> <div class="checkbox b">
<label><input type="checkbox" name="MoralCertificate" value="Complete">
<input type="hidden" name="MoralCertificate" value="Not Complete">
Good Moral Certificate</label>
</div> <div class="checkbox bb">
<label><input type="checkbox" name="BirthCertificate" value="Complete">
<input type="hidden" name="BirthCertificate" value="Not Complete">
NSO Birth Certificate</label>
</div> <div class="checkbox bbb">
<label><input type="checkbox" name="IDPicture" value="Complete">
<input type="hidden" name="IDPicture" value="Not Complete">
2x2 Formal ID Picture</label>
</div> <div class="checkbox bbbb">
<label><input type="checkbox" name="HonorStudents" value="Complete">
<input type="hidden" name="HonorStudents" value="Not Complete">
Certification for Honor Students</label>
</div> <div class="checkbox bbbbb">
<label><input type="checkbox" name="Form137" value="Complete">
<input type="hidden" name="Form137" value="Not Complete">
Form 137</label>
</div>
$Elementary = $_POST['Elementary'];
$YearGraduated = $_POST['YearGraduated'];
$AwardsReceived = $_POST['AwardsReceived'];
$Year = $_POST['Year'];
$Place = $_POST['Place'];
$status = $_POST['Card'];
$MoralCertificate = $_POST['MoralCertificate'];
$BirthCertificate = $_POST['BirthCertificate'];
$IDPicture = $_POST['IDPicture'];
$HonorStudents = $_POST['HonorStudents'];
$Form137 = $_POST['Form137'];
mysqli_query($con,"INSERT INTO educationalbackground (Elementary, YearGraduated, AwardsReceived, Year, Place, Card, MoralCertificate, BirthCertificate, IDPicture, HonorStudents, Form137 )
VALUES ('$Elementary','$YearGraduated', '$AwardsReceived', '$Year', '$Place', '$status', '$MoralCertificate', '$BirthCertificate', '$IDPicture', '$HonorStudents', '$Form137')");
header("Location: ThirdForm.php");
mysqli_close($con);
enter code hereenter image description hereenter image description here

You should simplify this using one checkbox per item.
<div class="checkbox">
<label>
<input type="checkbox" name="Card" value=1>
High School Card (Form 138)
</label>
</div>
Then in php check the results like so....
if(isset($_POST['Card'])){
// Card Completed
}else{
// Card Not Completed
}

Related

cant get Value of radio buttons in php when form is submitted

So I am using a form multiple radio buttons, The purpose is for the user to rate different parts of the site with 10 questions.
The user presses "start rating" button and the 1st question appears, then the 2nd etc.
When he finishes all of the 10 questions a submit button appears.
The problem is that, when I'm trying to retrieve these values on PHP, I get nothing, its like the radio buttons hasn't been pressed at all.
Newbie in HTML.
HTML
<form name="f1" action="rate.php" method="POST">
<!--1η ερώτηση-->
<div class="question_div" id="question_div1">
<center><p class="questiontitle"><b>Ερώτηση 1 από 10</b></p></center>
<p>Νομίζω ότι θα ήθελα να χρησιμοποιώ αυτά τα παιχνίδια συχνά.</p>
<div class="answer_div">
<div class="answer_options">
<label class="opt_label">
<input type="radio" name="input1" id="input1_1" value="1" class="option">
<span class="opt_span"><b>1</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_2" value="2" class="option">
<span class="opt_span"><b>2</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_3" value="3" class="option">
<span class="opt_span"><b>3</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_4" value="4" class="option">
<span class="opt_span"><b>4</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_5" value="5" class="option">
<span class="opt_span"><b>5</b></span>
</label>
</div>
<div class="answer-options">
<div align="left" style="width:20%;
float:left;
margin-right:30px;">Διαφωνώ απολύτως</div>
<div align="right" style="width:20%;
float:right;
margin-left:30px;">Συμφωνώ απολύτως</div>
</div>
</div>
<button class="nextbtn" type="button" id="btn1" onclick="(function(){
document.getElementById('question_div1').style.display = 'none';
document.getElementById('question_div2').style.display = 'block';
return false;
})();return false;">Επόμενη Ερώτηση</button>
</div>
this is the start of the form and the 1st question, the other questions follow the same architecture.
PHP:
if(!empty($_POST["f1"])){
$q1 = $_POST['input1'];
$q2 = $_POST['input2'];
$q3 = $_POST['input3'];
$q4 = $_POST['input4'];
$q5 = $_POST['input5'];
$q6 = $_POST['input6'];
$q7 = $_POST['input7'];
$q8 = $_POST['input8'];
$q9 = $_POST['input9'];
$q10 = $_POST['input10'];
}
It's because you have the same <input type="radio" name="input1" id="input1_1" value="1" class="option"> with the same name value. Try removing the name a attribute in your <form> and do one radio first and in your php file do this:
HTML
<form method="POST" action='rate.php'>
<div> question </div>
<input type="radio" name="input1">
<input type="submit">
</form>
rate.php
<?php
if(isset($_POST['input1']) {
echo $_POST['input1'];
//do your code here
}

how to use checkbox and input type number together in php

I am creating a meeting room booking application using php. In that, when a person book a room there have an option to book tea or snacks and specify the number of items they need. For that, I use checkboxes to select items and input type number to specify the number of items.i.e, if tea selected you have to specify the no. of tea in the input field. My problem is that I can't store or display the checkbox value and associated number together.my code is
<form class="form-horizontal" method="post" id="bookroom" action="bookedreview.php">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[]">
</div>
<input type="submit" name="submit" value="value">
</form>
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
//$food=implode(',',$_POST['food']);
foreach($foodnum as $index =>$value){
$fud=$value;
$num=$foodnum['index'];
}
}
but when I display the variable I couldn't get the result. Can anyone help me how could store the checked items and the associated quantity together.
You can get with passing food name in textbox name array
<form class="form-horizontal" method="post" id="bookroom" action="">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[tea]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[coffee]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[snacks]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[water]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[nuts]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[breakfast]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[lunch]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[dinner]">
</div>
<input type="submit" name="submit" value="value">
</form>
<?php
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
echo "<table border='1' style='width:100%'><tr><th>Food Name</th><th>Count</th></tr>";
// output data of each row
foreach($food as $foo)
{
echo "<tr><td>".$foo."</td><td>".$foodnum[$foo]."</td></tr>";
}
echo "</table>";
}
?>
For Insert
foreach($food as $foo)
{
$fieldVal1=$foo;
$fieldVal1=$foodnum[$foo];
$query ="INSERT INTO foodcounts( foodsname, cnt) VALUES ('".$fieldVal1."','".$fieldVal2."' )";
mysqli_query($conn, $query);
}

How do I insert $_POST values in a database

I have a webpage with multiple questions which is being loaded from my database. Each question has a radio-box because users have to choose an answer from the value of 1 to 5.
Every time I hit the hit the submit button, it goes to a page with the link address showing the users answers (i.e. submit.php?12=1&14=2&15=3&16=4&17=5)
The first value representing the questionID and the second value is the score for that specific questionID.
How do I insert those in my database? I want to insert the questionID and the answer to a table which has a question field and an answer field.
<form action="../process/submit.php" >
<?php
$pin=$_SESSION['pin'];
$question = mysql_query("SELECT question.question, question.questionID FROM question");
$index=0;
while($row=mysql_fetch_array($question)){
?>
<div id="qpanel"><?=$row['question']?>
<div style="float:right;">
<div id="ratingbox" style="vertical-align:middle;">
<label>
<input type="radio" name="<?=$row['questionID']?>" value="1" >
<div id="img1" title="Poor">1</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="2">
<div id="img1" title="Average">2</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="3" >
<div id="img1" title="Good">3</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="4" >
<div id="img1" title="Very Good">4</div>
</label>
<label>
<input type="radio" name="<?=$row['questionID']?>" value="5" >
<div id="img1" title="Excellent">5</div>
</label>
</div>
</div>
</form>
Use name field in this format:
<label>
<input type="radio" name="questionID[<?=$row['questionID']?>][]" value="5" >
<div id="img1" title="Excellent">5</div>
</label>
You will return in POST array with questionID and selected elements.

PHP - Getting Multiple Radio Buttons Values from From Submission

I am a newbie in PHP. I have a form like below:
<div class="col-xs-2 col-xs-offset-1">
<input id="radio-ge-1" class="radio-custom" name="radio-ge" type="radio" value="1" checked>
<label for="radio-ge-1" class="radio-custom-label tooltips">1 <span>Impossible to get marks</span></label>
</div>
<div class="col-xs-2">
<input id="radio-ge-2" class="radio-custom" name="radio-ge" value="2" type="radio">
<label for="radio-ge-2" class="radio-custom-label tooltips">2 <span>You have to work hard to get marks</span></label>
</div>
<div class="col-xs-2">
<input id="radio-ge-3" class="radio-custom" name="radio-ge" value="3" type="radio">
<label for="radio-ge-3" class="radio-custom-label tooltips">3 <span>The usual, just like any other class</span></label>
</div>
<div class="col-xs-2 col-xs-offset-1">
<input id="radio-sl-1" class="radio-custom" name="radio-sl" value="1" type="radio" checked>
<label for="radio-sl-1" class="radio-custom-label tooltips">1 <span>Unbearable pressure</span></label>
</div>
<div class="col-xs-2">
<input id="radio-sl-2" class="radio-custom" name="radio-sl" value="2" type="radio">
<label for="radio-sl-2" class="radio-custom-label tooltips">2 <span>High pressure, But doable</span></label>
</div>
<div class="col-xs-2">
<input id="radio-sl-3" class="radio-custom" name="radio-sl" value="3" type="radio">
<label for="radio-sl-3" class="radio-custom-label tooltips">3 <span>Fair, just like any other class</span></label>
</div>
I'm trying to save both radio buttons value in PHP by using the code:
$g_Easiness = $_POST['radio-ge'];
$s_Load = $_POST['radio-sl'];
But in this case, I'me only getting the first button's value. Second button's value = on.
I have spend more than two hours to figure it out. But cannot resolve it. Can anyone please help me to solve the problem?
Please use below code for radio buttons
if input type="radio" then it should give value="" otherwise in value it will show "on"
e.g.
<input id="radio-must" class="radio-custom" name="radio-nns" value="absolutely" type="radio" checked>
output = [radio-nns] => on
instead put ( you can see value="1")
<input id="radio-must" class="radio-custom" name="radio-nns" value="absolutely" type="radio" checked value="1">
output = [radio-nns] =>1

Why isn't my form submitting data into my database?

Alright so I have a form and i'm trying to get it to submit data into my database but it's not working... i'm not sure if it's my form that is causing the issue or if it's a problem with connecting to the database?
My table names are correct, value fields, log-in info... but whenever I hit submit it is not inserting any data.
Could it be my form that is having the issue?? Would you guys mind taking a look?
<form action="form.php" method="POST">
<div class="row">
<div class="large-4 columns">
<span id="spryfirstname">
<input name="firstname" type="text" class="text" placeholder="First Name"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>
<div class="large-4 columns">
<span id="sprylastname">
<input name="lastname" type="text" class="text" placeholder="Last Name"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>
<div class="large-4 columns">
<div class="row collapse">
<div class="small-9 columns"><span id="spryemail">
<input name="email" type="text" placeholder="email#example.com"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>
</div>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Check all Products that you're interested in</label>
<div>
<input name="products[]" type="checkbox" value="all">
ALL PRODUCTS/SERVICES
<input name="products[]" type="checkbox" vallue="trade">Trade-in
<input name="products[]" type="checkbox" value="layaway">Layaway products
<input name="products[]" type="checkbox" value="theatre">Home Theatre Systems
<input name="products[]" type="checkbox" value="TV">HD TVs
<input name="products[]" type="checkbox" value="Games">Video Game Consoles</label> <br>
<input name="products[]" type="checkbox" value="laptops"> Laptops</label>
<input name="products[]" type="checkbox" value="monitors"> Monitors</label>
<input name="products[]" type="checkbox" value="phones"> Phones</label>
<input name="products[]" type="checkbox" value="cameras"> Cameras</label>
<input name="products[]" type="checkbox" value="acoustic"> Acoustic Guitars</label>
<input name="products[]" type="checkbox" value="electric"> Electric Guitars</label>
<input name="products[]" type="checkbox" value="drums"> Drums</label>
<input name="products[]" type="checkbox" value="wind"> Wind Instruments</label> <br>
<input name="products[]" type="checkbox" value="pianos"> Pianos</label>
<input name="products[]" type="checkbox" value="violins"> Violins</label>
<input name="products[]" type="checkbox" value="diamonds"> Diamonds
<input name="products[]" type="checkbox" value="neck"> Necklaces
<input name="products[]" type="checkbox" value="rings"> Rings
<input name="products[]" type="checkbox" value="ear"> Ear Rings</label>
<input name="products[]" type="checkbox" value="gold"> Gold Jewelry
<input name="products[]" type="checkbox" value="silver"> Silver Jewelry
<hr>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>How often would you like to have product updates? <select>
<option value="daily" name"Updates">Daily</option>
<option value="weekly" name"Updates">Weekly</option>
<option value="monthly" name"Updates">Monthly</option>
</select>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Tell us a little about yourself <textarea placeholder="Type here">
</textarea>
</label>
</div>
</div>
<div class="row">
<input class="button small large-3" type="submit" name"submit" />
</div>
</form>
Here's my connecting to the database part:
<?php
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","dxh6110","******");
if(!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("dxh6110",$con);
$sql = "INSERT INTO Signup (Firstname,Lastname,Email) VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[email]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
Also, if I want to enter data from the checkboxes and drop down how would I go about doing that? Will it be the same as textfields?
This answer for your originally posted code before your edit and without marking it as an edit.
This is the reason:
You have no equal sign in name"submit" for your submit button.
Change it to name="submit" which your code's execution is based on your conditional statement:
if (isset($_POST['submit']))
It took me a while to spot that one after staring at your code, wondering "why" it wasn't working.
The rest of your code does check out, however I must also state that your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.
As for your checkboxes, you can base yourself on the following:
$checkbox = $_POST['products'];
for($i=0;$i <sizeof($checkbox);$i++) {
$query="INSERT INTO your_table (col) VALUES('".$checkbox[$i]."')";
mysql_query($query,$con) or die(mysql_error());
}
For some added protection:
$firstname= stripslashes($_POST['firstname']);
$lastname= stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$firstname= mysql_real_escape_string($_POST['firstname']);
$lastname= mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$sql = "INSERT INTO Signup (Firstname,Lastname,Email)
VALUES('".$firstname."','".$lastname."','".$email."')";
or mysqli_ method:
<?php
$con = mysqli_connect("myhost","myuser","mypassw","mybd")
or die("Error " . mysqli_error($con));
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$firstname = mysqli_real_escape_string($con,$_POST['firstname']);
$lastname = mysqli_real_escape_string($con,$_POST['lastname']);
$email = mysqli_real_escape_string($con,$_POST['email']);
$sql = "INSERT INTO Signup (Firstname,Lastname,Email)
VALUES('".$firstname."','".$lastname."','".$email."')";
mysqli_query($con,$sql);
mysqli_close($con);

Categories