Multiple choice question skewing the total points tally - php

I've created a step by step test (fiddle example below) that doesn't quite tally up the total correctly. It works fine if all questions have one answer but when there's multiple answers for a single question it gives a strange tally.
(All answers to the questions are the first checkbox except for "Question 2", the multiple choice question, which is the first 2 checkboxes)
For the multiple choice question - the progress squares at the bottom change green when only one of the two answers is correct (when you click next), it should only be green when both are checked correctly, if not correct then it should be red.
The second issue is that if you answer all questions correctly then the final score is 7... when it should be 5 (multiple choice questions should add 1 point per answer). (If you inspect the fiddle, you can see the total being updated when the next button is clicked in the hidden field at the bottom)
Where am I going wrong?
http://jsfiddle.net/rob_teamworks/vebcsjw0/
jQuery(function($) {
$(document).ready(function() {
// hide all form-rows, but not the first one
$('.form-row').not(':first').hide();
// hide on last step
$('button.next').last().hide();
var score = 0;
$('button.next, input.check').click(function(e) {
// prevent the next buttons from submitting the form
e.preventDefault();
var item = $(this).parents('div.form-row').find('.correct:first').data('progress');
if ($(this).parents('div.form-row').find('.correct:first').is(":checked") && $(this).parents('div.form-row').find('.correct:first').hasClass('correct-answer')) {
$('.quiz-progress-circle[data-progress="' + item + '"]').removeClass("progress-incorrect");
$('.quiz-progress-circle[data-progress="' + item + '"]').addClass("progress-correct");
score += Number(score+1);
} else {
$('.quiz-progress-circle[data-progress="' + item + '"]').removeClass("progress-correct");
$('.quiz-progress-circle[data-progress="' + item + '"]').addClass("progress-incorrect");
}
// hide this form-row, and show the next one
$(this).parents('div.form-row').hide().next('div.form-row').show();
$('.finalscore').val(score);
});
// add the submit button to the last form-row
$('<input>').addClass('check').prop('type', 'submit').appendTo($('.form-row:last'));
});
});
jQuery(function($) {
$(document).ready(function () {
$("input[type=checkbox].correct").click(function (e) {
if ($(e.currentTarget).closest("div.question").length > 0) {
toggleInputs($(e.currentTarget).closest("div.question")[0]);
}
});
});
function toggleInputs(questionElement) {
if ($(questionElement).data('max-answers') == undefined) {
return true;
} else {
maxAnswers = parseInt($(questionElement).data('max-answers'), 10);
if ($(questionElement).find(".correct:checked").length >= maxAnswers) {
$(questionElement).find(".correct:not(:checked)").attr("disabled", true);
} else {
$(questionElement).find("input[type=checkbox].correct").attr("disabled", false);
}
}
}
});
.quiz-progress-circle {
height:5px;
width:5px;
background-color:grey;
display:inline-block;
margin-right:10px;
}
.progress-correct {
background-color:green!important;
}
.progress-incorrect {
background-color:red!important;
}
.progress-current {
background-color:blue!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="row test">
<div class="columns">
<div class="entry">
<form class="form" method="POST" action="http://example.com/test-insert.php">
<input type="hidden" value="teamworks" name="test-user">
<input type="hidden" value="Introduction" name="test-name">
<div class="form-row">
<h1>Quiz | Introduction</h1>
<div class="clear"></div>
<div id="module-area">
<div id="modules-top"></div>
<div id="modules-repeat">
<h2 class="training">1. Question 1</h2>
<div class="question" data-max-answers="1">
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct correct-answer" data-progress="1" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 1">
</div>
<div style="display:table-cell;">
<p>
Answer 1 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct " data-progress="1" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 2">
</div>
<div style="display:table-cell;">
<p>
Answer 2 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct " data-progress="1" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 3">
</div>
<div style="display:table-cell;">
<p>
Answer 3 </p>
</div>
</div>
</div>
<div class="inner"></div>
<button class="next">Next >></button>
<div class="clear"></div>
</div>
<div id="modules-bottom"></div>
</div>
</div>
<div class="form-row" style="display: none;">
<h1>Quiz | Introduction</h1>
<div class="clear"></div>
<div id="module-area">
<div id="modules-top"></div>
<div id="modules-repeat">
<h2 class="training">2. Question 2</h2>
<div class="question" data-max-answers="2">
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct correct-answer" data-progress="2" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 1">
</div>
<div style="display:table-cell;">
<p>
Answer 1 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct correct-answer" data-progress="2" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 2">
</div>
<div style="display:table-cell;">
<p>
Answer 2 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct " data-progress="2" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 3">
</div>
<div style="display:table-cell;">
<p>
Answer 3 </p>
</div>
</div>
</div>
<div class="inner"></div>
<button class="next">Next >></button>
<div class="clear"></div>
</div>
<div id="modules-bottom"></div>
</div>
</div>
<div class="form-row" style="display: none;">
<h1>Quiz | Introduction</h1>
<div class="clear"></div>
<div id="module-area">
<div id="modules-top"></div>
<div id="modules-repeat">
<h2 class="training">3. Question 4</h2>
<div class="question" data-max-answers="1">
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct correct-answer" data-progress="3" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 1">
</div>
<div style="display:table-cell;">
<p>
Answer 1 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct " data-progress="3" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 2">
</div>
<div style="display:table-cell;">
<p>
Answer 2 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct " data-progress="3" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 3">
</div>
<div style="display:table-cell;">
<p>
Answer 3 </p>
</div>
</div>
</div>
<div class="inner"></div>
<button class="next">Next >></button>
<div class="clear"></div>
</div>
<div id="modules-bottom"></div>
</div>
</div>
<div class="form-row" style="display: none;">
<h1>Quiz | Introduction</h1>
<div class="clear"></div>
<div id="module-area">
<div id="modules-top"></div>
<div id="modules-repeat">
<h2 class="training">4. Question 5</h2>
<div class="question" data-max-answers="1">
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct correct-answer" data-progress="4" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 1">
</div>
<div style="display:table-cell;">
<p>
Answer 1 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct " data-progress="4" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 2">
</div>
<div style="display:table-cell;">
<p>
Answer 2 </p>
</div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<input class="correct " data-progress="4" style="width: 20px;" type="checkbox" name="answer[]" value="Answer 3">
</div>
<div style="display:table-cell;">
<p>
Answer 3 </p>
</div>
</div>
</div>
<div class="inner"></div>
<button class="next" style="display: none;">Next >></button>
<div class="clear"></div>
</div>
<div id="modules-bottom"></div>
</div>
<input class="check" type="submit"></div>
<div class="quiz-progress">
<div class="quiz-progress-circle" data-progress="1"></div>
<div class="quiz-progress-circle" data-progress="2"></div>
<div class="quiz-progress-circle" data-progress="3"></div>
<div class="quiz-progress-circle" data-progress="4"></div>
</div>
<input type="hidden" value="236" name="test-id">
<input class="finalscore" type="hidden" value="" name="test-score">
<input type="hidden" value="2" name="test-pass">
</form>
<div class="clear"></div>
</div>
</div>
</section>
This is the php file it calls on submit. The variable $score comes from the hidden field with the name test-score that is tallied up by the jquery variable score.
<?php
$score = $_POST["test-score"];
$pass_msg = $_POST["test-pass"];
if ($score >= $pass_msg) {
$pass_txt = "Pass";
} else {
$pass_txt = "Fail";
}
// Opens a connection to a MySQL server
$username="root";
$password="root";
$database="local";
$host="localhost";
$con = mysqli_connect($host, $username, $password);
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($con, "local");
$user = $_POST["test-user"];
$testid = $_POST["test-id"];
$sql = mysqli_query($con, "INSERT INTO test (`name`, `testname`, `score`, `pass-fail`) VALUES ('".$user."', '".$testid."', '".$score."', '".$pass_txt."')");
if (!$sql)
{
die('Error: ' . mysqli_error());
}
mysqli_close($con);
header('Location: http://example.com/training/introduction/');
?>
Thanks to #viorel for the answer, it's caused an additional problem:
It should only show the second submit button on the last question... it should submit the form but it's not counting the question towards the score.

I've looked a little bit at the code and simplified the following bit:
$('button.next, input.check').click(function (e) {
// prevent the next buttons from submitting the form
e.preventDefault();
var correctAnswers = $(this).siblings().find('.correct-answer').length;
var totalUserCorrectAnswers = $(this).siblings().find('.correct-answer:checked').length;
var totalCheckedAnswers = $(this).siblings().find('input:checked').length;
var item = $(this).parents('div.form-row').find('.correct:first').data('progress');
var resultBubble = $('.quiz-progress-circle[data-progress="' + item + '"]');
if(correctAnswers === totalUserCorrectAnswers && totalUserCorrectAnswers === totalCheckedAnswers) {
resultBubble.removeClass("progress-incorrect");
resultBubble.addClass("progress-correct");
score += 1;
} else {
resultBubble.removeClass("progress-correct");
resultBubble.addClass("progress-incorrect");
}
// hide this form-row, and show the next one
$(this).parents('div.form-row').hide().next('div.form-row').show();
$('.finalscore').val(score);
});
I've added three control variables: the total number of correct answers expected (correctAnswers), the number of correct answers the user selected (totalUserCorrectAnswers) and the total number of checked answers (totalCheckedAnswers). You may not need this last check since you are disabling the checkboxes.
For each steps, there's a simple check to see if the total number of answers selected equals the number expected. If they match, the score is incremented by one and the progress squares get the appropriate color. I couldn't figure out exactly the problem before, but it seems that you were only selecting the first correct checked answer

To answer your second question,
score += Number(score+1);
This will add 2 each time, not 1. Change it to:
score++;
For your first question, your if function when button.next is clicked is true when only one answer is correct. A fix could be to define a variable (green = 1). And then run a jQuery Each for every .correct. In the Each Loop, if it has class correct answer but is not ticked, then change green to 0. Also, if a 'correct' with no 'correct-answer' is ticked then also update variable green to 0.
After the Each Loop, then use the value of the variable 'green' to determine if the progress square should be green or not.

Related

submit form when checkbox is checked AJAX / PHP

Hi I wanting to submit the filter form when any of the checkboxes' are checked. Have looked at multiple forums and cant seen to get the answer. Also with the current code when i uncheck the input box it passes empty value. All in the same file. Go easy i am new :) . I want to see the parameter in the url , so i can pass value to this page from other pages and select the appropriate checkbox on load
**TEST.PHP**
<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<br />
<h2 align="center">Advance Ajax Product Filters in PHP</h2>
<br />
<form action="" id="filter">
<div class="col-md-3">
<div class="list-group">
<h3>LEVEL</h3>
<div style="height: 180px; overflow-y: auto; overflow-x: hidden;">
<div class="list-group-item checkbox">
<?php
$sql = "SELECT DISTINCT(carcolor) FROM car";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
foreach($result as $row)
{
?>
<input type="checkbox" onclick="filtercolor()" class="color" name="color" value="<?= $row['carcolor']; ?>"/>
<label><?php echo $row['carcolor']; ?></label>
<?php
}
}
else {
echo "No data Found";
}
?>
</div>
</div>
</div>
</div>
</form>
<div class="col-md-9">
</div>
</div>
</div>
<script>
function filtercolor() {
var color = $('input:checkbox[name="color"]:checked').map(function(){return this.value;}).get().join('|');
$.ajax({
url: 'test.php',
data:"color=" + color,
type: "GET",
success: function(html) {
alert('form submited');
},
});
}
</script>
</body>
</html>
Try this
function filtercolor(checkBox) {
let checkBox_val = checkBox.value;
alert(checkBox_val);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span> 0 </span>
<input type="checkbox" onclick="filtercolor(this)" class="color" value="0"/> <br>
<span> 1 </span>
<input type="checkbox" onclick="filtercolor(this)" class="color" value="1"/> <br>
<span> 2 </span>
<input type="checkbox" onclick="filtercolor(this)" class="color" value="2"/> <br>
<span> 3 </span>
<input type="checkbox" onclick="filtercolor(this)" class="color" value="3"/> <br>
<span> 4 </span>
<input type="checkbox" onclick="filtercolor(this)" class="color" value="4"/>

Is there a best way i can select all the topics and questions on each topics and display them onec in php

I have two tables "setTearmlyExam.php" and "setTopics.php", what i want to do is this:
in the "setTearmlyExam.php" i have a form with two select options, a radio button and a start button. each select option has an option from the database.
what am trying to do here is: when the start button is cliked, its will redirect to the "setTopics.php" and there display all the topics and question using the selected information on the "setTearmlyExam.php".
I have two table where am selecting the data that will be displayed in the "setTopics.php"
The olevelguestion:
Topics:
"setTearmlyExam.php"
<form action="setTopics.php " method="POST" class="col s12" center>
<!-- setTopics.php -->
<div>
<label>Select Term</label>
<select name="terms" class="browser-default" required>
<option value="" disabled selected>Select Term</option>
<?php
// Selecting terms from database
$select_terms = $db->query("SELECT * FROM term");
if (!$select_terms) {
die('QUERY FAILED' .mysqli_error($db));
}
// Ends
// Using while loop tp fatch and loop term titles
while ($row = $select_terms->fetch_assoc()) {
$termId = $row['termId'];
$terms = $row['terms'];
echo "<option value='{$termId}'>{$terms}</option>";
}
?>
</select>
<label>Select Subject</label>
<select name="subjects" class="browser-default" required>
<option value="" disabled selected>Select Subject</option>
<?php
// Selecting subjects from database......
$Select_subjects = $db->query("SELECT *
FROM subjects
ORDER BY subj ASC");
if (!$Select_subjects) {
die("QUERY FAILED" . mysqli_error($db));
}
// Ends..
// Using while loop to fetch the subjects title
while ($rw = $Select_subjects->fetch_assoc()) {
$subjId = $rw['subjId'];
$subj = $rw['subj'];
echo "<option value='{$subjId}'>{$subj}</option>";
}
?>
</select>
</div>
<div class="ST">
<div class="col 6">
<label>
<input class="with-gap" name="Examtype" value="O" type="radio" required/>
<span>Objective</span>
</label>
</div>
<div class="col 12">
<label>
<input class="with-gap" name="Examtype" value="T" type="radio" />
<span>Theory</span>
</label>
</div>
</div>
</div>
</div>
<div class="card-action centerTxt">
<button class="btn waves-effect waves-light " type="submit" name="start">Start
<i class="material-icons right">send</i>
</button>
</div>
</div>
</form>
"setTopics.php"
<form action="">
<div class="card-content black-text">
<span class="card-title centerTxt">
<h4>Subject Title</h4>
</span>
<div>
<!-- Dropdown Trigger -->
<label for="">
<h6 class="" style="font-size: 15px; color:black;">CLICK ON EACH TOPIC TO SELECT QUESTIONS. <span style="color:red; font-size: 13px;">Don't Select more than 50 Questions.</span></h6>
<?php
$terms = 'terms';
$subjects = 'subjects';
// $Examtype = $_GET['Examtype'];
if ($Examtype = 'O') {
$topics = $db->query('SELECT * FROM topics WHERE ');
}
?>
<h5 class='dropdown-trigger ' style="color:#149fd5;" data-target='dropdown1'>Topics Here</h5>
</label>
<!-- Dropdown Structure -->
<ul id='dropdown1' class='dropdown-content'>
<p>
<label>
<input type="checkbox" value="" />
<span>Yellow</span>
</label>
</p>
</ul>
</div>
</div>
<div class="card-action centerTxt">
<button class="btn waves-effect waves-light " type="submit" name="start">Next
<i class="material-icons right">send</i>
</button>
</div>
</form>

Undefined indexes apeeared after submitting the form

I have a two step form. create.php and create2.php. Both pages are forms. After first form is filled user presses "Continue" and proceeds to the second form where i pass and store value from first form in hidden inputs. After filling and submitting second form, i want pop up window which means successful submission of the form and inserting all the data into database. It seems that everything is working fine and all data is in database, however i dont get pop up window, what i get is undefined index warning about my hidden inputs.
Ok here is code for create.php:
<form class="formcss" method="post" action="create2.php" id="reportform" enctype="multipart/form-data">
<fieldset style="background-color:white;">
<legend style="font-size: 20px;">New Project</legend>
<br>
<div class="row">
<div class="small-8 large-8 columns">
<label>Project Code: <small style="color:red;">*</small>
<input type="text" name="code" maxlength="155" id="code" class="input input1 name" onkeyup="limitTextCount('code', 'divcount0', 155, 0);" onkeydown="limitTextCount('code', 'divcount0', 155, 0);" <?php if (isset($code)) echo 'value="'.$code.'"' ?>/>
<label class="tool tool1" for="name" style="margin-top:-8px;">Code of the project</br>e.g. ASD001</label>
</label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<label>Project Name: <small style="color:red;">*</small>
<input type="text" name="title" maxlength="155" id="title" class="input input1 name" onkeyup="limitTextCount('title', 'divcount0', 155, 0);" onkeydown="limitTextCount('title', 'divcount0', 155, 0);" <?php if (isset($title)) echo 'value="'.$title.'"' ?>/>
<label class="tool tool1" for="name" style="margin-top:-8px;">Title of the project</br>e.g. Leon</label>
</label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<label>Process
<div class="multiselect">
<div class="selectBox">
<select onclick="showCheckboxes()" class="input input1 name">
<option>-- Select an option --</option>
</select>
<div class="overSelect"></div>
</div>
<div class="scrollable" id="checkboxes">
<?php
while ($row = mysql_fetch_array($result))
{
$row[0] = cleanOutputData($row[0]);
?>
<div class="row">
<div class="small-12 large-12 columns">
<label style="height: 37px; width:80%; float:left;">
<input type="checkbox" class="checkbox" style="margin-left:5%; width:15%;" name="process[]" id=<?php echo $row[0] ?> value=<?php echo $row[0]?> /><?php echo $row[0] ?>
</label>
<label style="width:40%; margin-left:60%;"><input type="text" class="field" disabled style="width:40%;" name="numberpl[]" id=<?php echo $row[0] ?> />
</label>
</div>
</div>
<?php
}
mysql_free_result($result);
?>
</div>
</div>
</label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<label>Comments
<textarea style="resize:none;" class="input input1 name" name="remark" rows="8" cols="50" maxlength="255" id="remark" onkeyup="limitTextCount('remark', 'divcount5', 255, 0);" onkeydown="limitTextCount('remark', 'divcount5', 255, 0);"><?php if (isset($remark)) echo $remark ?></textarea>
<label class="tool tool1" for="name" style="left:-140px;">Further comments</label>
</label>
</div>
<div class="small-6 large-6 columns">
<label> <?php if (isset($remark)){ echo "<label id='divcount5'></label>"; echo "<script>limitTextCount('remark', 'divcount5', 255, 0);</script>";} else echo "<label id='divcount5'>255 characters remaining</label>";?></label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<input type = "submit" name ="submit" style="margin-left:600px; width:150px;" class="button" onclick="userSubmitted = true;" value = "Continue"/>
</div>
</div>
<br/><br/>
</fieldset>
</form>
And for the second form create2.php:
<?php
session_start();
//if user haven't sign in will redirect user to login page
if(empty($_SESSION['login_user'])){
session_destroy();
header("Location: login.php");
}
$proc = isset($_POST['process'])?$_POST['process']:'';
//$proc=$_POST['process'];
$len = count($proc); // getting length of ur array that u need to condition ur loop
$num = isset($_POST['numberpl'])?$_POST['numberpl']:'';
//$num=$_POST['numberpl'];
//$len2 = count($num); // getting length of ur array that u need to condition ur loop
include 'verification/verify_form_details2.php';
require_once('inc/config.php');
//include 'verification/verify_form_details.php';
ob_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<form class="formcss" method="POST" name="checkoutForm" action="create2.php#err" id="reportform" enctype="multipart/form-data">
<?php
$res = verifyFormFields();
?>
<!-- hidden inputs from first form(create.php) -->
<input type="hidden" name="holdcode" value="<?php echo isset($_POST['code'])?$_POST['code']:'';?>">
<input type="hidden" name="holdtitle" value="<?php echo isset($_POST['title'])?$_POST['title']:'';?>">
<?php
//an array of inputs
for($y=0;$y<$len;$y++)
{
?>
<input type='hidden' name='holdprocess[]' value="<?php echo $proc[$y]?>">
<input type='hidden' name='holdnumber[]' value="<?php echo $num[$y]?>">
<?php
}
?>
<!-- hidden inputs from first form(create.php) -->
<br>
<fieldset style="background-color:white;">
<legend style="font-size: 20px;">Add Stuff</legend>
<br><br><br>
<div class="small-3 large-3 columns">
<label>Choose username</label>
<input placeholder="Search Me" id="box" type="text" />
<div id="myAccordion">
<?php for($i=321; $i<347; $i++)
{
echo "<h3>".chr($i)."</h3>";
echo '<ul class="source">';
$sql = "SELECT username FROM user WHERE username LIKE '".chr($i+32)."%' ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$name= $row["username"];
echo"<li>". $name ."</li>";
}
} else
{
echo "0 results";
}
echo '</ul>';
}
?>
</div>
</div>
<div id="project" class="small-9 large-9 columns">
<label style="font-size: 40px; margin-left:10%;">Project <?php echo isset($_POST['code'])?$_POST['code']:''; ?></label>
<div class="rowone">
<div id="leader">
<label>Leader:</label>
<div class="ui-widget-content">
<div id="projLeader">
<ol>
<li class="placeholder" name="leader" <?php if (isset($leader)) echo 'value="'.$leader.'"' ?>>Add leader here</li>
<input type="hidden" name="leader" id="hiddenListInput1" />
</ol>
</div>
</div>
</div>
<div id="checker">
<label>Checker:</label>
<div class="ui-widget-content">
<div id="projChecker">
<ol>
<li class="placeholder" name="checker" <?php if (isset($checker)) echo 'value="'.$checker.'"' ?>>Add checker here</li>
<input type="hidden" name="checker" id="hiddenListInput2" />
</ol>
</div>
</div>
</div>
<div id="info">
<label>Information:</label>
<div class="ui-widget-content">
<ol>
<li>Total:</li>
<li>Total:</li>
<li>Total:</li>
<li>Total:</li>
<li>Total:</li>
</ol>
</div>
</div>
</div>
<div class="row">
<input type = "submit" id="savebutton" style="margin-left:300px; width:150px;" name ="submit" class="button" value = "Create Project" onclick="userSubmitted = true;" />
</div>
</div>
<div id="formModal" class="reveal-modal small" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog" data-options="close_on_background_click:false">
<h2 id="modalTitle">Success!</h2>
<div style="font-weight: 400;font-size: 1.5em; font-family: 'Raleway', Arial, sans-serif;">Your Accident Report was Successfully Submitted!</div>
<div class="right">
Ok
</div>
</div>
<?php
if($counta==1)
{
if($res=="")
{
$testing = JSON_encode($_SESSION['role']);
echo '<script>userSubmitted = true;</script>';
insertRecord();
echo "<script type ='text/javascript'>callShowAlert();</script>";
}
else{
echo "
<br><br><a style='color:red';>
$res
</a>
";
}
}
?>
<script>var testing = JSON.parse('<?= $testing; ?>');</script>
</fieldset>
</form>
Here is what i got after submitting second form:
Notice:
Undefined index: process in C:\xampp\htdocs\Projects\1ver\create2.php
on line 9
Notice:
Undefined index: numberpl in C:\xampp\htdocs\Projects\1ver\create2.php
on line 12
Notice:
Undefined index: code in
C:\xampp\htdocs\Projects\1ver\create2.php on line 256
Notice:
Undefined index: title in
C:\xampp\htdocs\Projects\1ver\create2.php on line 257
Notice:
Undefined index: title in C:\xampp\htdocs\Projects\1ver\create2.php on
line 302
I'm using another php page to insert data into database. I just don't get what can be a problem. Thanks for any help
Since POST array is not available for first time. Therefore, you are getting this error. If you really need to use those post variables, you should apply a condition in this case to avoid these errors.
I'm here adding for one, you can do the same for remaining.
Try this:
<?php echo isset($_POST['code'])?$_POST['code']:''; ?>

Getting only half of an array after passing the form

I have two forms. After submitting first form it comes to a second form where i need to get values from first form. Here is the first part of form:
<?php $result2 = getProcessID();?>
<form class="formcss" method="post" action="create2.php" >
<div class="row">
<div class="small-8 large-8 columns">
<label>Process: <small style="color:red;">*</small>
<div class="multiselect">
<div class="selectBox">
<select onclick="showCheckboxes()" class="input input1 name">
<option>-- Select a process -- Add number of people reqired --</option>
</select>
<label class="tool tool1" for="name" style="top:-15px; margin-left:-14px; left:-207px; ">Choose process and number <br> of people required</label>
<div class="overSelect"></div>
</div>
<div class="scrollable" id="checkboxes">
<?php
while ($row = mysql_fetch_array($result2))
{
$row[0] = cleanOutputData($row[0]);
?>
<div class="row">
<div class="small-6 large-6 columns">
<label style="height: 38px;">
<input type='checkbox' style="width:20%;" name='process[]' id=<?php echo $row[0] ?> value=<?php echo $row[0] ?>/><?php echo $row[0] ?>
</label>
</div>
<div class="small-6 large-6 columns">
<label><input type='text'style="margin-left:50%; width:20%;" name='number[]'/>
</label>
</div>
</div>
<?php
}
mysql_free_result($result2);
?>
</div>
</div>
</label>
</div>
</div>
</form>
It seems fine to display the values in the second form except two problems:
1) Only half of the process is displayed
2) I can the number only for first 3 processes, but for other processes it stays an empty field.
Here is my second form:
$_SESSION['process'] = $_POST['process'];
$_SESSION['number'] = $_POST['number'];
$proc=$_SESSION['process'];
$len = count($proc); // getting length of an array
$num=$_SESSION['number'];
<form class="formcss" method="post" action="create.php#err" id="reportform" enctype="multipart/form-data">
<div id="project" class="small-9 large-9 columns">
<label style="font-size: 40px; margin-left:10%;">Project <?php echo $_SESSION["title"]; ?></label>
<label><?php for($y=0;$y<$len;$y++)
{
echo "Process: "."$proc[$y]"."<br />";
echo "People required: "."$num[$y]"."<br />";
?>
<ol>
<li class="placeholder" <?php if (isset($leader)) echo 'value="'.$leader.'"' ?>>Add people here</li>
</ol>
<?php
}
?>
</label>
<br><br><br>
<div class="row">
<input type = "submit" style="margin-left:300px; width:150px;" name ="submit" class="button" value = "Create Project" onclick="userSubmitted = true;"/>
</div>
</div>
</form>
As i said only half of the process is shown, for example, I have 'ANM KEY' process, and when i pass the value and display it on another form it displays only 'ANM'. Also I want to display number of people required for each process, but it works only for first three rows. So how i can get the wholename of the process, and display number of people required. Thank you!

php insert radio button value only working from mobile

I have a form which was working the day before, and now it's only working from my iphone. I have 4 columns in a mySQL, id, submissiondate, housenumber, answer, hostname
they all work when submitting from my iphone.
If you try from a desktop, the 'answers' which are 3 radio buttons, don't work, and insert a blank record.
wondering if anyone has encountered this before.
if (isset($_POST["submit"])) {
$Answer = $_POST['Answer'];
if ($_POST['housenumber'] == "") {
echo "<div class='container' style='text-align:left'><div role='alert' class='alert alert-danger'>
<p class='lead'><strong>ERROR!</strong> All fields are required</p></div></div>";
} else {
$hostname='XXXXXXXXXXX';
$username='XXXXXXXXXXX';
$password='XXXXXXXXXXX';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=qlfence",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO qlfence (id, SubmissionDate, housenumber, Answer, hostname)
VALUES ('id', NOW(),'".$_POST["housenumber"]."','".$_POST["Answer"]."','".$_POST["hostname"]."')";
if ($dbh->query($sql)) {
echo "<div class='container' style='text-align:left'><div class='alert alert-success' role='alert'><p class='lead'><strong>Awesome!</strong> Thanks for your input.</p></div></div>";
}
else{
echo "<script type= 'text/javascript'>alert('Opps, something went wrong. We will loo into');</script>";
}
header('Location: /index.php');
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
<form action="#" method="post" class="" id="sendmessage" name="sendmessage" >
<input type="hidden" class="form-control" id="hostname" name="hostname" readonly value="<?php echo $hostname; ?>" >
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><center><input type="text" class="form-control" id="housenumber" name="housenumber" style="width: 35%; border: 2px solid black;"></center></div>
<div class="col-md-6"><label for="housenumber"><span class="lead" >Quarrie Lane #</span></label> </div>
<div class="col-md-2"> </div>
<br>
<br><br>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a1" name="Answer" class="form-control" value="I am Willing to do the fence as soon as everyone is ready."></div>
<div class="col-md-6"><label for="a1"><p class="lead">Willing to do the fence as soon as everyone is ready.</p></label></div>
<div class="col-md-2"> </div>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a2" name="Answer" class="form-control" value="I would rather wait until next summer."></div>
<div class="col-md-6"><label for="a2"><p class="lead">I would rather wait until next summer.</p></label></div>
<div class="col-md-2"> </div>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a3" name="Answer" class="form-control" value="I am not the owner."></div>
<div class="col-md-6"><label for="a3"><p class="lead">Not the owner.</p></label></div>
<div class="col-md-2"> </div>
</div>
<input name="submit" id="submit" type="submit" value="Submit Answer" class="btn btn-lg btn-primary btn-block"></input>
</form>
The only reason why you doesn't recieve $_POST['Answer'] is that you doesn't check any radios of answers before submitting your form.
Besides security issues, your code is working. But your should consider to add more security to your sql-queries.
As a workaround, you can check one of radios by default, like this:
<input checked="true" type="radio" id="a3" name="Answer" class="form-control" value="I am not the owner." />
in this case you will always receive your $_POST['Answer'].

Categories