How do I insert $_POST values in a database - php

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.

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
}

count html radio buttons value with php

I have a html form with grouped radio button. This form contains questions with yes or no options
<form action="results.php" method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
I know i can count the number of radio buttons with name q
$count_cbox = count($_POST['q'])
But is it possible that when the user makes a choice i count the radio button value that ="yes" or "no".
Please change your radio button names slightly. Otherwise the grouping doesn't work:
<form method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[0]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[0]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[1]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[1]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[2]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[2]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
The PHP code:
for($i = 0; $i < count($_POST['q']); ++$i) {
if($_POST['q'][$i] == 'yes') {
++$yes;
}
}
Now $yes contains the number of radio buttons with the value yes. In this case 0-3. Please pay attention to the fact that if no radio button is selected, it will return NULL, not 0.

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

CheckBox html-php-mysql

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
}

Not returning selected pciture name

I have a database with pictures that i can delete from or use them in different parts of the the website by choosing one of the radio buttons in the popup.Problem is everytime I click I click on a picture the name of the first picture in the database comes up not the one from the picture i clicked . What's wrong? I used PHP 5.3 and HeidiSQL
<?php
// ...
$result=mysql_query("SELECT * FROM imagini WHERE menu_id=6");
while($data=mysql_fetch_row($result)){ ?>
<div class="tag">
<div id='container_poze_originale'>
Alege alt rol
<div id="my_popup" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
close
<form action="popup.php" method="post" >
<input type="text" name="alt-rol" value="<?php echo $data[1];?>" /> //always returns the name of the first picture in the database not the one i select
<input type="submit" value="Adauga imagine" class="buton_imagine" />
<div class="radio">
<input type="radio" name="tip_imagine" value="0"/><label for="tip_imagine" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1"/><label for="tip_imagine" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2"/><label for="tip_imagine" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3"/><label for="tip_imagine" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4"/><label for="tip_imagine" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5"/><label for="tip_imagine" class="radio2">Background</label>
</div>
</form>
</div>
<div class="imagine_originala">
<img src= "../upload/original/<?php echo $data[1];?>" /></a>
</div>
<div class="Btag" style="display:none;">
<div id="buton_slide4" >
Sterge</td>
</div>
</div>
</div>
</div>
<?php
}
?>
I think I understand your question. You are clicking on
Alege alt rol
and you are surprised that the same <div> always appears?
Well, it is simply because an id should be unique. And if you cycle through your images you create a new <div> each time with the same id: my_popup. When clicking on the link (no matter which one), JavaScript will then just take the first <div> it finds with the id my_popup. So you should just assign an unique id to each div so your JavaScript knows which one to open. You could do this by appending your image id to the div id (assuming you have a unique image id in $data[1]).
So change it to (shortened):
<? while($data=mysql_fetch_row($result)) { ?>
<div class="tag">
<div id="container_poze_originale">
Alege alt rol
<div id="my_popup<?=$data[1]?>" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
...
</div>
</div>
</div>
<? } ?>
Oh and it might be good to put all those huge inline CSS attributes into an external stylesheet, so it will be easier to maintain/change your webpage later on. And it makes things a bit more readable... :-)
I think your form is ok when you click on the radio but not when you click on the label.
The for attribute of the label need an ID as reference like this :
<input type="radio" name="tip_imagine" value="0" id="tip0" /><label for="tip0" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1" id="tip1"/><label for="tip1" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2" id="tip2"/><label for="tip2" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3" id="tip3"/><label for="tip3" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4" id="tip4"/><label for="tip4" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5" id="tip5"/><label for="tip5" class="radio2">Background</label>
With your actual code, the FOR is always the same so you get always the first.

Categories