issue for days now.
I have a form with multiple checkboxes, I will never know how many rows will be in the form, and I am aware that an empty checkbox will not post to the processing page. I have also tried providing a hidden value to the checkbox to no avail...
I know there is a straightforward method to this, please assist:
The following code is on the form:
echo "<td><center><input type='checkbox' checked='checked' name='jce_pnt_id[]' value='$jce_pnt_id' onclick='return false;' /><br>$jce_pnt_id</center></td>";
echo "<td title='Task Complete ?'><center><input type='hidden' name='tr_g[]' value='25'>";
if($jce_ins_complete < '100' || $jce_ins_act_complete == '0')
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100'>";
}
else
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100' checked='checked'>";
}
echo "</center></td>";
Here is a screenshot (look at the red circled column):
Here is the processing snippet (not working):
$jce_pnt_id = $_POST['jce_pnt_id'];
$tr_g = $_POST['tr_g']; // jce_pnt_act_complete
foreach ($tr_g as $id => $trg) {
//if(empty($trg)) { $trg = "0"; }
$queryg = "UPDATE `jce_pnt` SET `jce_pnt_act_complete` = '$trg' WHERE `jce_pnt_id` = '$jce_pnt_id[$id]'";
$resultg = mysqli_query($cxn,$queryg);
}
* The value writes to the wrong ID in the database.
So here's my code in getting an input from a user on how many questions (multiple choice) he/she would like to make:
Multiple choice: <input type = "text" name="MC"><br>
<input type = "submit" name = "confirm" value = "Confirm">
After that, this is the code of how many questions the system will generate:
<?php
if(isset($_POST['confirm'])){
$MC = $_POST['MC'];
echo "<form method = 'POST' name = 'items' action ='createquestions.php'>";
$items = 1;
for ($x = 1; $x <= $MC; $x++) {
echo "Question Number $items:"; echo "<input type = 'text' name = 'questions[]' style='width: 500px'><br><br>";
echo "A. "; echo "<input type = 'text' name = 'ans1[]'>";
echo "B. "; echo "<input type = 'text' name = 'ans2[]'><br>";
echo "C. "; echo "<input type = 'text' name = 'ans3[]'>";
echo "D. "; echo "<input type = 'text' name = 'ans4[]'><br>";
echo "Correct Answer: "; echo "<input type = 'text' name ='cans[]'><br><br>";
$items++;
}
echo "<input type ='submit' name = 'save' value = 'Save'>";
echo "</form>";
}
?>
<?php
The problem is that it will only save the last input of the user.
For example, I have inputted 2 in the Multiple choice: --textbox here--
This code will generate 2 questions, 8 choices, 2 cans = correct answer but it will only save the 2nd question, answers, and the correct answer. the system won't get the record of the 1st question, answer, and the correct answer.
Here is the code where I would insert it on the database:
<?php
if(isset($_POST['save'])){
$user_id = $_SESSION['id'];
$questions = $_POST['questions'];
$ans1 = $_POST['ans1'];
$ans2 = $_POST['ans2'];
$ans3 = $_POST['ans3'];
$ans4 = $_POST['ans4'];
$cans = $_POST['cans'];
foreach($questions as $q){
echo "<input type = 'hidden' value = '$q'>";
}
require_once('xcon.php');
$query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans)
VALUES ('NULL','$user_id','$q','$ans1','$ans2','$ans3','$ans4','$cans')";
$result = mysql_query($query);
if($result){
echo 'Insert Success!';
}
else{
echo 'Error';
}
}
?>
When you save you should be running through a loop again. Try this maybe?
<?php
if(isset($_POST['save'])){
$user_id = $_SESSION['id'];
require_once('xcon.php');
foreach ($_POST['questions'] as $key => $question){
$ans1 = $_POST['ans1'][$key];
$ans2 = $_POST['ans2'][$key];
$ans3 = $_POST['ans3'][$key];
$ans4 = $_POST['ans4'][$key];
$cans = $_POST['cans'][$key];
echo "<input type = 'hidden' value = '$question'>";
$query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans)
VALUES ('NULL','$user_id','$question','$ans1','$ans2','$ans3','$ans4','$cans')";
$result = mysql_query($query);
if($result){
echo 'Insert Success!<br>';
}else{
echo 'Error<br>';
}
}
}
?>
According to this post you should use:
echo "Question Number $items:"; echo "<input type = 'text' name = 'questions' style='width: 500px'><br><br>";
echo "A. "; echo "<input type = 'text' name = 'ans[]'>";
echo "B. "; echo "<input type = 'text' name = 'ans[]'><br>";
echo "C. "; echo "<input type = 'text' name = 'ans[]'>";
echo "D. "; echo "<input type = 'text' name = 'ans[]'><br>";
echo "Correct Answer: "; echo "<input type = 'text' name ='cans'><br><br>";
And this:
$ans1 = $_POST['ans'][0];
$ans2 = $_POST['ans'][1];
$ans3 = $_POST['ans'][2];
$ans4 = $_POST['ans'][3];
Explanation
You only need to post ans[] repeatedly , and not
ans1[], ans2[], ans3[]... in order to get an array like
$_POST['ans'][0], $_POST['ans'][1]...
or you can use
ans1, ans2, ans3
(without brackets []) to read as
$_POST['ans1'], $_POST['ans2'], $_POST['ans3']...
You are using element naming in a strange way, you are using array, but still use numbers. Try generating like this:
for ($x = 0; $x <= $MC; $x++) {
echo "<input type = 'text' name = 'questions[$i]'>";
echo "A. <input type = 'text' name = 'ans[$i][A]'>";
echo "B. <input type = 'text' name = 'ans[$i][B]'><br>";
echo "C. <input type = 'text' name = 'ans[$i][C]'>";
echo "D. <input type = 'text' name = 'ans[$i][D]'><br>";
echo "Correct Answer: <input type = 'text' name ='cans[$i]'><br><br>";
}
Then you'll get the following results in your $_POST:
[
"questions" => [
0 => "question1",
...
]
"ans" => [
0 => [
"A" => "answer A",
"B" => "answer B",
"C" => "answer C",
"D" => "answer D",
]
...
]
"cans" => [
0 => "A",
....
]
]
Which is easily processed with a foreach:
foreach ($_POST['questions'] as $key => $question) {
// $question == 'question1';
$answers = $_POST['ans'][$key]; // array of answers
$solution = $_POST['cans'][$key];
}
Is there anyone who knows to duplicate a IF CONDITION through looping? Please. I really need your help.. This is my first time to encounter thing like this? Help will be appreciated thanks.
<?php
for($x=0; $fetchImageDirectoryRow = mysqli_fetch_assoc($fetchImageDirectory); $x++){
$directory = $fetchImageDirectoryRow['Image_Path'];
list($c, $apache24, $htdocs, $sp, $uploadItem, $imageFilename) = explode('/', $directory);
$uploadItem = $uploadItem."/".$imageFilename;
$imageGet = glob($uploadItem);
foreach($imageGet as $imagePath){
$attName = uniqid();
echo '<td>';
echo "<img src= '".$imagePath."' class='readyToPostImage'/><br><br>";
echo "<input type='text' placeholder='Enter New Price (eg. 300)' class='newPriceInput'/><br><br>";
echo "<a class='addMore'>add more photos</a>";
echo "<input type='file' class='browseCustom' multiple/><br>";
echo "<input type='submit' name='".$attName."' class='btn btn-success' value='Post to PeĆ³nline'/>";
echo '</td>';
How can I duplicate this IF Condition? Is there a function to copy the statement through looping.
if(isset($_POST[$attName])){
mysqli_query($connection, "UPDATE collateral c LEFT JOIN loan_assignment a ON (a.item_id = c.item_id) LEFT JOIN pawnshop b ON (a.pawnshop_id = b.pawnshop_id) SET c.post_status = '1' where status_type = 'FORECLOSED' and b.email_address = '".$login_session."' and image_path = '".$directory."';");
mysqli_close($connection);
}
}
}
?>
I have 3 PHP files, in select.php file you have to select the number of forms that you want.. the form contain 3 input fields.
as below:
<form method="post" action="index.php" >
Continue insertion with <select name="counters" id="insert_rows">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
rows
<input type="submit" value="Go" />
</form>
After selecting the number of insertions, it will pass the number to index.php which is contain:
<?php
echo "<form method = 'POST' action = 'process.php'>";
for($counter = 0; $counter < $_POST['counters']; $counter++)
{
echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
echo "Service Version : <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
echo "Service type : <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
}
echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
?>
the third file process.php contain
<?php
$name = array();
$version = array();
$type= array();
$name = $_POST['name'];
$version = $_POST['version'];
$type= $_POST['type'];
for($counter = 0; $counter < sizeof($name); $counter++)
{
echo "service_name #".($counter + 1).": ".$name[$counter]."<br />";
echo "service_version #".($counter + 1).": ".$version[$counter]."<br />";
echo "service_type #".($counter + 1).": ".$type[$counter]."<br />";
}
?>
and after i pass the value to it, it shows the data correctly as below
service_name #1: noway
service_version #1: v1
service_type #1: Private
service_name #2: bandar
service_version #2: v2
service_type #2: Public
so my question is : how to create a function in 'process.php' file to insert all these values into the database.
I created a table called 'services' contain these columns "name,version,type"
I will be waiting for your support.
thank you
I would recommend to save the first submitted count value in an hidden input in the second form, then you will be able to loop through that and insert your data
<?php
echo "<form method = 'POST' action = 'process.php'>";
for($counter = 0; $counter < $_POST['counters']; $counter++)
{
echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
echo "Service Version : <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
echo "Service type : <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
}
echo '<input type="hidden" name="cntr" value="'.$_POST['counters'].'" />'
echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
?>
Now in your process.php you will have:
if (!empty($_POST['cntr'])) {
$query = 'INSERT INTO services (name, version, type) VALUES ';
for ($x = 0; $x < $_POST['cntr']; $x++)
{
$query .= "('".mysqli_escape_string($_POST['name'][$x])."','"
.mysqli_escape_string($_POST['version'][$x])."','"
.mysqli_escape_string($_POST['type'][$x]).
"'),";
}
$query = substr_replace($query, '', -1);
mysqli->query($query);
}
Whatever I prepared is just a sample you can modify it base on your changes
I am really having a hard time figuring out why if(isset($_POST['next'])) isn't working. How can I call something like that within a function?
function questions($result){
$options = array('A','B','C','D','E');
echo "Please click 'Next'";
echo "<p class = 'button'><input type='submit' name='next' value='Next'/></p>";
while($row = $result->fetch_assoc()){
if(isset($_POST['next'])) {
echo "<h4>".$row['question']."</h4>";
foreach($options as $option){
$text = $row[$option];
if($text)
echo "<br/><input type = 'radio' name = 'ans' value = '$option'>".$text."<br/>";
}
echo "<p class = 'button'><input type='submit' id='submit' class = 'answer' name='submitAns' value='Submit' /></p>";
if(isset($_POST['submitAns'])){
$answer = $row['answer'];
if($_POST('ans') == $answer) {
echo "<h4>Correct!</h4>";
} else {
echo "<h4>Incorrect. Correct answer is: ".$answer."</h4>";
}
echo "<p class = 'button'><input type='submit' id='next' name='next' class = 'answer' value='Next' /></p>";
}
}
}
if(isset($_POST['next'])){
echo "End of questions for this exam";
}
}
Checking to see if a variable exists (isset) and checking to see whether a variable actually contains something (empty) are two different things. Try this:
function questions($result){
$options = array('A','B','C','D','E');
echo "Please click 'Next'";
echo "<p class = 'button'><input type='submit' name='next' value='Next'/></p>";
while($row = $result->fetch_assoc()){
if($_POST && isset($_POST['next']) && !empty($_POST['next'])) {
// ....
}