I am trying to submit this radio buttons but it shows this
Notice: Array to string conversion in C:...
i don't really know what to do to make it work.
I've to try some other ways but its not working
<?php
public function userQuestion($type)
{
$option = $this->con->prepare("SELECT * FROM stem_questions WHERE category = :type");
$option->bindParam(':type', $type);
$option->execute();
$id = array();
while ($row = $option->fetch()) {
$id[] = $row["id"];
$que[] = $row["question"];
$o1[] = $row["opt1"];
$o2[] = $row["opt2"];
$o3[] = $row["opt3"];
$o4[] = $row["opt4"];
$cat[] = $row["category"];
$isa[] = $row["is_anwer"];
}
$count = count($id);
if ($count < 1) {
echo "<span class='alert alert-info fa fa-warning col-lg-12' style='color:orange; font-size:14px; color: white'> You have not Set any Question Yet.</span>";
} else {
$k = 0;
echo "";
for ($i = 1, $j = 0; $i <= $count; $i++, $j++) {
$k++;
echo "<form method='POST'> <p><strong> Question $i : $que[$j]</strong></p> <div class='form-group'> <input type='hidden' name ='qust' value='$que[$j]'> <input type='hidden' name ='cat' value='$cat[$j]'><input type='hidden' name ='ans' value='$isa[$j]'> <input type='radio' name ='opt[$k]' value='$o1[$j]'> <label class='form-check-label'>  $o1[$j]</label><br> <input type='radio' name='opt[$k]' value='$o2[$j]'><label class='form-check-label'>  $o2[$j]</label><br> <input type='radio' name='opt[$k]' value='$o3[$j]'><label class='form-check-label'>  $o3[$j]</label><br> <input type='radio' name ='opt[$k]' value='$o4[$j]'> <label class='form-check-label'>  $o4[$j]</label><br> </div> </form> ";} } }
public function submitQuiz()
{
//FOR THE QUIZ SYSTEM
if (isset($_POST["submit"])) {
for ($i = 1; $i <= 5; $i++) {
$userid = $_SESSION['id'];
$qid = $_POST['qust'];
$uans = $_POST['opt'] . $i;
$ans = $_POST['ans'];
$category = $_POST['cat'];
$insert = $this->con->prepare("INSERT INTO user_question_answer (user_id, question_id, user_ans, is_right, type)VALUES (:userid, :qid, :uans, :ans, :category)");
$insert->bindParam(':userid', $userid);
$insert->bindParam(':qid', $qid);
$insert->bindParam(':uans', $uans);
$insert->bindParam(':ans', $ans);
$insert->bindParam(':cat', $cat);
if (!$insert->execute()) {
$atry = $insert->errorInfo();
echo $atry[2];
exit();
} else {
// this just my person function #return $this->just_notify("<strong>Successfully uploaded </strong>", 1);} } } }
it was suppose to submit but its just bringing up errors
Error is with your name tag radio option in form.
Replace your echo "" part --
with
echo "<form method='POST'>
<p><strong> Question $i : $que[$j]</strong></p>
<div class='form-group'>
<input type='hidden' name ='qust' value='$que[$j]'>
<input type='hidden' name ='cat' value='$cat[$j]'>
<input type='hidden' name ='ans' value='$isa[$j]'>
<input type='radio' name ='opt1['.$k.']' value='$o1[$j]'> <label class='form-check-label'>  $o1[$j]</label><b`enter code here`r>
<input type='radio' name='opt2['.$k.']' value='$o2[$j]'><label class='form-check-label'>  $o2[$j]</label><br>
<input type='radio' name='opt3['.$k.']' value='$o3[$j]'><label class='form-check-label'>  $o3[$j]</label><br>
<input type='radio' name ='opt4['.$k.']' value='$o4[$j]'> <label class='form-check-label'>  $o4[$j]</label><br>
</div>
</form>"
and
$insert->bindParam(':uans', $uans); can bind only single value not the array data.
> <input type="radio" name="radio_name[]"
> value="id_of_row_from_database_or_identity_of_this_radio_button" />
>
> ex:-
>
> <input type="radio" name="age[]" value="G`enter code here`G101" />12 <input
> type="radio" name="age[]" value="GG102" />23
Related
I have the following code, the variable 'name' and 'project' need to go to session variables on submit, in this case the submit happens through an 'onchange'event. The 'name' variable is POSTed but not the 'project' variable. Any ideas where my issue is?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
</form>
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name[]' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
How to access:
$names = $_POST['name'];
for($i=0;$i<count($names);$i++)
{
echo $names[$i]."<br>";
}
I'm creating a quiz-like structure. For that i'm trying to get the answer of each question and insert it into my database with the type of question that was answered. However i'm having trouble putting these data in variables and i'm getting:
Notice: Undefined index: question-0-answer
in the commented part of the code. Any help..
$options = '';
$filter=mysql_query("select afnumber from employees WHERE Status='Employed '");
while($row = mysql_fetch_array($filter)) {
$options .="<option >" . $row['afnumber'] . "</option>";
}
$menu="<form id='filter' name='filter' method='post' action=''>
AFNumber : <select name='SelectAF' id='filter' style='color:grey;'>" . $options . "</select>
Evaluation Test Type : <select name='Type' id='type' style='color:grey;'><option selected='selected'></option><option value='loyalty'>Loyalty</option><option value='performance'>Performance</option></select>
<input type='submit' name='submit1' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
</form>
<br>
";
echo $menu;
if(isset($_POST['submit1']))
{
$type = $_POST['Type'];
$mysqli = new mysqli("localhost", "root", "Js", "jr");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if ( $result = $mysqli->query( "SELECT questiontext FROM questioninfo WHERE type='$type'" ) ) {
$html=array();
$html[]="
<form action='' method='post' id='quiz'>
<ol>";
$counter=1;
while( $row = $result->fetch_array() ) {
$question=$row['questiontext'];
$answerA=1;
$answerB=2;
$answerC=3;
$answerD=4;
$answerE=5;
$html[]="
<br/>
<h3>Question {$counter}: {$question}</h3>
<li>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersA' value='A' />
<label for='question-{$counter}-answers-A'> {$answerA} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersB' value='B' />
<label for='question-{$counter}-answers-B'> {$answerB} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersC' value='c' />
<label for='question-{$counter}-answers-C'> {$answerC} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersD' value='D' />
<label for='question-{$counter}-answers-D'> {$answerD} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersE' value='E' />
<label for='question-{$counter}-answers-E'> {$answerE} </label>
</li>";
$counter++;
}
$html[]="
</ol>
<input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
<input type='hidden' name='type' value='{$type}' />
</form>";
echo implode( PHP_EOL, $html );
$result->close();
}
}
/*
if( isset( $_POST['submit'] ) ){
$numQuestions=10;
for( $counter=0; $counter < $numQuestions; $counter++ ){
$answer=$_POST['question-'.$counter.'-answers'];
$sql="insert into `question` (`type`,`value`) values ('".$type."','".$answer."')";
$db->query( $sql );
}
}
*/
The $counter for outputting the radiobuttons is initialized with the value 1 but when reading the values of radiobuttons at the end of your script, you are starting with $counter = 0;.
Please change
for( $counter=0; $counter < $numQuestions; $counter++ ){
to
for( $counter=1; $counter <= $numQuestions; $counter++ ){
You are running loop statically 10 times in comment and what if database have less than 10 records? You need to run loop a many times as db having records or you can put condition before insert by isset($_POST['question-'.$counter.'-answers'])
I have a loop which generates a couple of fields which could be field in. I want to POST all the values from these fields into another php page.
The first page:
for ($i = 1; $i <= 10; $i++) {
print "
<form action='2.php' method='post' id='test_form'>
<input name='value-$i' type='text' />
<input id='send' name='send' type='submit' value='Sent' />
</form>
";
}
The second page:
while ( ) {
$valuer= $_POST['value'];
}
<input name='value[$i]' type='text' />
^--^---- note the []
Then
$fifth_value = $_POST['value'][5];
I am getting the value for the single tsid for each record, however the checked radio button value is not returned in the array, I just get 0? Any help is appreciated.
PHP:
// Set the timesheets to set status approved/rejected
// find out how many records there are to update
$size = count($_POST['tsid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$tsid = intval($_POST['tsid'][$i]);
$personnelid = intval($_POST['personnel'][$i]);
print "TSID: " . $tsid . "<br>";
print "TSuser: " . $personnelid . "<br>";
if ($tsid > 0 && $personnelid > 0) {
// do the update and print out some info just to provide some visual feedback
$query = "Update timesheets set status='1' where id= '$tsid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
}
++$i;
}
mysql_close();
HTML:
<input type="hidden" name="tsid[]" value="<?PHP echo $row['id']; ?>">
<li data-role="fieldcontain">
<a href="tsapprove.php?id=<?PHP echo $row['id']; ?>"><p><?PHP echo $row['personnel']; ?></p>
<p><?PHP echo $row['name']; ?></p>
<p class="ui-li-aside"><strong><?PHP echo $row['totalhrs']; ?> H</strong></p>
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="radio" name="personnel[]" id="1" value="<?PHP echo $row['personnel']; ?>" />
<label for="1">Approve</label>
<input type="radio" name="personnel[]" id="2" value="<?PHP echo $row['personnel']; ?>" />
<label for="2">Reject</label>
</fieldset>
</a>
View Details
</li>
Hopefully you can piece this together:
<?
$size = count($_POST['tsid']);
echo "<pre>";print_r($_POST);echo "</pre>";
$i = 0;
while ($i < $size) {
$tsid = $_POST['tsid'][$i];
$pid = $_POST['personnel_'.$i];
print "TSID: " . $tsid . "<br />";
print "TSuser: " . $pid . "<br />";
$i++;
}
?>
<form method="post">
<!--tsid[0] - $i = 0-->
<input type="hidden" name="tsid[]" value="1" />
<input type="radio" name="personnel_0" value="111" />
<input type="radio" name="personnel_0" value="222" />
<!--tsid[1] - $i = 1-->
<input type="hidden" name="tsid[]" value="2" />
<input type="radio" name="personnel_1" value="333" />
<input type="radio" name="personnel_1" value="444" />
<input type="submit" />
</form>
Basically, the way you are returning personnel doesnt really work. So I simplified this a bit, and basically set the radios for personnel differently. Instead of an array, its setting the actual name with the TSID value.
I hard coded 1 and 2, but you would replace with your $row['id'], as well has the number after "personnel_" would be 0..1..2 etc
Hopefully this helps
edit: few issues with code
OK this is my script:-
<form action="results.php" method="post">
<?php mysql_select_db($database, $databasename) or die("Opps some things went wrong");
$sqlQueryTestDisplay = mysql_query("SELECT * FROM questions WHERE test_id='$testtaken_id' ORDER BY question_id ASC");
$i = 0;
while($DisplayItems = mysql_fetch_array($sqlQueryTestDisplay))
{
$i = $i + 1;
$question_id = $DisplayItems['question_id'];
$question = $DisplayItems['question'];
$opta = $DisplayItems['opta'];
$optb = $DisplayItems['optb'];
$optc = $DisplayItems['optc'];
$optd = $DisplayItems['optd'];
$answer[$i] = $DisplayItems['answer'];
$thisAnswer = $answer[$i];
echo '<li>'.$question.'</li>';
echo '<p>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_0" />'.$opta.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_1" />'.$optb.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_2" />'.$optc.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_3" />'.$optd.'</label>';
echo '<input name="ans'.$i.'" type="hidden" value="'.$thisAnswer.'" />';
echo '</p>';
}
echo '<input name="total_questions" type="hidden" value="'.$i.'" />';
echo '<input name="test_id" type="hidden" value="'.$TestID.'" />';
?>
<input name="submittest" type="submit" />
</form>
As you can see i am using array to store values in different fields. Now on the next page i.e on my result.php page I am writing this:-
<?php
if(isset($_POST['submittest']))
{
global $ans1;
$TotalQuestions = $_POST['total_questions'];
$TestID = $_POST['test_id'];
$TestResult = 0;
for ($i=1; $i<=$TotalQuestions; $i++)
{
$ansValue = 'ans'.$i;
$optansValue = 'optans'.$i;
$ans = $_POST[$ansValue];
$optans = $_POST[$optansValue];
if ($ans == $optans)
{
$TestResult = $TestResult + 1;
}
}
$st_id = $row_Recordset1['id'];
mysql_select_db($database, $databasename) or die("Opps some things went wrong");
$sqlQueryInsertResult = mysql_query("INSERT INTO results (student_id, test_id, test_result) VALUES ('$st_id', '$TestID', '$TestResult')");
header('location:results.php');
}
?>
Now my script is not reading ans1, ans2.....and so on AND even quesans1, quesans2.....and so on.
I think problem is in the way i am calling the array using the $_POST method.
Is the syntax correct, how can i FIX it... Please help :|
All of your radio buttons are returning "radio" as their value. Make them return the answer and you should be OK
Change your radio definitions to:
'<INPUT type="radio" name="optans'.$i.'" value="'.$opta.'" >'.$opta.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optb.'" >'.$optb.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optc.'" >'.$optc.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optd.'" >'.$optd.'</INPUT>'
Might be worth not putting the answer onto the page as a hidden field - makes a quiz quite easy. Do another SQL query to check the answers when they come back
Use some more loops to display the radio buttons and you can shorten your code a bit.
0Change your test options to this:
echo '<label><input type="radio" name="optans'.$i.'[]" value="1" id="RadioGroup'.$i.'_0" />'.$opta.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="2" id="RadioGroup'.$i.'_1" />'.$optb.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="3" id="RadioGroup'.$i.'_2" />'.$optc.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="4" id="RadioGroup'.$i.'_3" />'.$optd.'</label>';
and your processor to this:
<?php
if(isset($_POST['submittest']))
{
global $ans1;
$TotalQuestions = $_POST['total_questions'];
$TestID = $_POST['test_id'];
$TestResult = 0;
for ($i=1; $i<=$TotalQuestions; $i++)
{
$ans = $_POST['ans'.$i];
$optans = $_POST['optans'.$i];
for ($j=0;$j<count($optans);$j++) {
if ($optans[$j]==$ans) {
$TestResult = $TestResult + 1;
}
}
}
This is a very insecure way to compare test answers. Someone could easily View Source and see the correct answers. You should validate the test answers after the $_POST