I am making a quiz backend process. Where backend user will needed to update questions and answers that has been posted or saved.
Right now I have a problem in the update process I cannot update the answers
An example of my UI
See UI here
All data in answers textboxes has this element.
<input type="text" name="choice[1]" class="form-control" value="Sydney" ?="">
My update in Controller
public function update()
{
$this->post_model->update_post();
redirect('/');
}
My update in Model - $answer is null after I var_dump why?
public function update_post(){
$answer = $this->input->post('choice');
foreach($medicine as $key=>$val)
{
$data[] = array(
'answer' => $answer[$key]
);
$this->db->where('id', $val);
$updated = $this->db->update('answers');
}
}
View
<?php echo form_open('posts/update'); ?>
<input type="hidden" name="id" value="<?php echo $posts['id']; ?>">
Question:
<input type="text" name="question" class="form-control" value="<?php echo $posts['question']; ?>" ?><hr>
Answers:
<?php foreach($questions as $question): ?>
<input type="text" name="choice[<?php echo $question['id'] ?>]" class="form-control" value="<?php echo $question['answer']; ?>" ?><hr>
<?php endforeach; ?>
<hr>
<input type="submit" class="btn btn-success" value="Save Changes">
</form>
Related
I'm doing a short quiz and i would like to know how can i pass over the quiz answers to the next page?
My Quiz page
<?php
$qtspool =
array( 1=> array('qts'=>'What is my name?', 'ans'=>'Lu'),
2=> array('qts'=>'What is the module code?', 'ans'=>'307'),
3=> array('qts'=>'What is missing char abde?', 'ans'=>'c'),
4=> array('qts'=>'What is missing number 1345?', 'ans'=>'2')
);
$qtspick_key = array_rand($qtspool,3);
for ($i=0; $i<3; $i++)
{
$qtspick_key[$i];
}
$pickqts = array();
$i = 0;
foreach ($qtspick_key as $key)
{
$pickqts[$i] = $qtspool[$key];
$i++;
}
?>
<form method="get" action="abc.php" >
<br>
<?php
foreach($pickqts as $qtsno=>$value)
{
?>
<?php echo $value['qts']; ?>
<input name="user_ans" type="text" >
<input type="hidden" name="answers" value="<?php echo htmlspecialchars($value['ans']); ?>" />
<br>
<?php
}
?>
<input type="submit"/>
</form>
I tried <input type="hidden" name="answers" value="<?php echo htmlspecialchars($value['ans']); ?>" /> but it could only pass over the last answer of the question to the next page.
P.S: when I do an echo $value['ans'];, I'm able to return all the answers of the randomized questions on the quiz page.
You have to send variables by array so change the lines:
<input name="user_ans" type="text" >
<input type="hidden" name="answers" value="<?php echo htmlspecialchars($value['ans']); ?>" />
to:
<input name="user_ans[]" type="text" >
<input type="hidden" name="answers[]" value="<?php echo htmlspecialchars($value['ans']); ?>" />
I am trying to store the values obtained from the form into the database. I am stuck where I do not know how to obtain the values individually from foreach($questions as $row) and store them into the database.
My database table has the following columns:
variantid | stepid | questionid | newquestion | radiovalues | description
When I click on save, I am trying to save all the details obtained from the view into the database. every value obtained with each loop of foreach($questions as $row) needs to get stored in a new row. I think I should be using insert_batch, but I do not know how I can put all the data into the array.
How can I proceed with this? The following is the code that I have written. I have not written anything in model as I am stuck.
<form id="theform">
<label for="variants">Variants:</label><br>
<?php
foreach($variants as $row)
{?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" id="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
<?php }
?>
<div id='steplist'>
<?php
foreach($substeps as $row){
?>
<div id='img_div'>
<input type="radio" name="stepid[]" checked="true" id="stepid<?php echo $row->stepid; ?>" value="<?php echo $row->stepid; ?>"/><?php echo $row->stepname ?><br/>
</div>
<?php
foreach($questions as $each){
?><br><br>
<input type="text" name="questions[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionname; ?>" readonly/><br/>
<input type="hidden" name="questionid[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionid; ?>"/>
<a id="addq">Add question</a>
<input type="text" class="form-control" name="newquestion[]" style="font-weight:bold;width:100%;height:5%" id="new_questions<?php echo $each->questionid; ?>" /><br>
<div class="radio" id="answer">
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="no" required>no</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably no" required>probably no </label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="unknown" required>unknown</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably yes" required>probably yes</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="yes">yes</label>
</div>
<textarea name="question1_text[]" id="description<?php echo $each->questionid; ?>" rows="3" cols="73"></textarea><br>
<?php
}
?>
<?php
}
?>
</div>
</form>
<input type='button' value='Save' class='save' id='save' />
<script>
$(document).ready(function(){
$("#save").click(function(e){
e.preventDefault();
$.ajax({
type:"POST",
url:"<?= base_url() ?>index.php/task/perform",
data:$('#theform').serialize(),
success:function(response){
alert(response);
}
});
});
});
This is my controller:
public function perform(){
var_dump($_POST);
$variantid = $this->input->post('variants');
$stepid = $this->input->post('stepid');
$questionid = $this->input->post('questions');
$newquestion = $this->input->post('newquestion');
$radioquestions = $this->input->post('radioquestions');
$question1_text = $this->input->post('question1_text');
$this->load->model('task_model');
$result= $this->task_model->performdata();
if($result){
echo "Data saved successfully";
}
}
Update
Thank you for the quick reply. I changed the code according to the suggestion. How can I get these values into the controller and send them into the database?
The HTML inside the foreach is invalid as it is missing a closing div tag and your input does not have the correct name attribute, for multiple inputs you should use variants[] and not the same ID. Do it in this manner:
<?php foreach ($variants as $row) { ?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" class="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
</div>
<?php } ?>
<form action="" method="post">
<?php
$abc = 10;
for($key=1;$key<=30;$key++)
{
?>
<input type="hidden" name="hidden_id" value="<?php echo $key;?>" />
<textarea name="vids" rows="4" cols="50"><?php echo $abc; ?> </textarea>
<?php
$abc++;
}
?>
<input type="submit" name="abc" />
</form>
The above code is returning only the last value of the textarea and hidden field.
I want to get the the hidden field value for the textarea updated along with the updated value of the textarea.
Please help me with the solution.
First you have to change name="hidden_id" to name="hidden_id[]" and name="vids" to name="vids[]" . Then them can hold multiple values. When you submit the form hidden_id[], vids[] will return arrays. Therefor you have to loop them for access one by one.
<?php
if(isset($_POST['abc'])){
// $_POST['hidden_id'] return as a array
if(!empty($_POST['hidden_id'])){
foreach ($_POST['hidden_id'] as $id) {
echo $id;
}
}
// access textarea values
if(!empty($_POST['vids'])){
foreach ($_POST['vids'] as $text) {
echo $text;
}
}
}
?>
<form action="" method="post">
<?php
$abc = 10;
for($key=1;$key<=30;$key++)
{
?>
<input type="hidden" name="hidden_id[]" value="<?php echo $key;?>" />
<textarea name="vids[]" rows="4" cols="50"><?php echo $abc; ?></textarea>
<?php
$abc++;
}
?>
<input type="submit" name="abc" value="submit" />
</form>
the problem lies on the input's name which are name="hidden_id" and name="vids".
You should change them into name="hidden_id[]" and name="vids[]" in order for them to hold multiple values. Please see the code below
<input type="hidden" name="hidden_id" value="<?php echo $key;?>"/>
<textarea name="vids" rows="4" cols="50"><?php echo $abc; ?></textarea>
I want to view data from database.there are three table to join.in two table have same field name. when i try to view data as follows.but there was an error called
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$project_name1
Filename: views/boq_doc.php
Line Number: 12
Controller
class Project_list extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('project_list_model');
}
function show_project_id() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['single_project'] = $this->project_list_model->show_project_id($id);
$this->load->view('boq_doc', $data);
}
}
Model
class Project_list_model extends CI_Model {
// Function To Fetch All Students Record
function show_projects(){
$this->db->select("project.project_name AS project_name1 , project.id AS id, client.firstname AS firstname1, client.lastname AS lastname1,staff.firstname AS firstname2, staff.lastname AS lastname2, project.location,project.category, project.start_date, project.end_date");
$this->db->from('project');
$this->db->join('client', 'project.client_id = client.client_id');
$this->db->join('staff', 'staff.id = project.staff_id');
$query = $this->db->get();
return $query->result();
}
// Function To Fetch Selected Student Record
function show_project_id($data){
$this->db->select('*');
$this->db->from('project');
$this->db->where('id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
View
<?php foreach ($projects as $project): ?>
<li><?php echo $project->project_name1; ?></li>
<?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project): ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?php endforeach; ?>
</div>
Your foreach loop syntax seems wrong try this instead
<?
foreach ($projects as $project)
{
?>
<li><?php echo $project->project_name1; ?></li>
<?
}
?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project){ ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?
}
?>
</div>
I have the following code where I am trying to get the 'Finalize Draft Order' submit button to only appear if the 'Create Draft Order' button has been pressed/set. Right now, the button does not show up after I hit the Create Draft Order button. It only displays if I take it out of the if(isset function.
What am I doing wrong?
<form method="POST" name="form">
<input type="submit" value="Create Draft Order" name="shuffle">
</form>
Shuffled results: <br>
<div class="main-bag">
<div class="shuffle_results" id="results"></div>
<form method="post">
<?php
$count = 0;
foreach ($array as $result) :
$count++;
$shuffle_count = $count;
$shuffle_firstname = htmlentities($result['firstname']);
$shuffle_lastname = htmlentities($result['lastname']);
$shuffle_id = htmlentities($result['id']);
$shuffle_username = htmlentities($result['username']);
$shuffle_email = htmlentities($result['email']);
?>
<input type="hidden" name="count[]" value="<?php echo $shuffle_count; ?>">
<input type="hidden" name="firstname[]" value="<?php echo $shuffle_firstname; ?>">
<input type="hidden" name="lastname[]" value="<?php echo $shuffle_lastname; ?>">
<input type="hidden" name="id[]" value="<?php echo $shuffle_id; ?>">
<input type="hidden" name="username[]" value="<?php echo $shuffle_username; ?>">
<input type="hidden" name="email[]" value="<?php echo $shuffle_email; ?>">
<?php
endforeach;
// only show this button if we have done a shuffle
if ( isset($_POST['shuffle'] ) ) :
echo '<input type="submit" value="Finalize Draft Order" name="insert">';
endif;
?>
UPDATE:
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = array(
'id' => $row['id'],
'firstname' => $row['firstname'],
'lastname' => $row['lastname'],
'username' => $row['username'],
'email' => $row['email']
);
if (isset($_POST['shuffle'])) {
}
}
shuffle($array);
echo json_encode($array);
I don't think $_POST['shuffle'] is set. instead of using the submit button for it change your code to:
<form method="POST" name="form">
<input type="hidden" name="shuffle" value="true">
<input type="submit" value="Create Draft Order">
</form>
I'm pretty sure submit's name doesn't count for the POST values.
I don't know what version of PHP meame69 and ScottyMcGready are using, but you very much CAN check to see if the button was clicked by checking for the isset($_POST["submit"]) is true.
http://www.learningaboutelectronics.com/Articles/How-to-check-if-the-submit-button-is-clicked-in-PHP.php
I cannot comment yet, but I will edit this once I figure out what's wrong with Becky's code.
Here's you're issue. Your input button is named shuffle, which is not passed when submitting. If you add a new hidden input element with the name "shuffle" that will pass it through.
edit:
You've said in your question its after the create draft button is pressed. Therefore as per your example:
<form method="POST" name="form">
<input type="submit" value="Create Draft Order" name="form">
<input type="hidden" name="shuffle" value="1">
</form>
Shuffled results: <br>
<div class="main-bag">
<div class="shuffle_results" id="results"></div>
<form method="post">
<?php
$count = 0;
foreach ($array as $result) :
$count++;
$shuffle_count = $count;
$shuffle_firstname = htmlentities($result['firstname']);
$shuffle_lastname = htmlentities($result['lastname']);
$shuffle_id = htmlentities($result['id']);
$shuffle_username = htmlentities($result['username']);
$shuffle_email = htmlentities($result['email']);
?>
<input type="hidden" name="count[]" value="<?php echo $shuffle_count; ?>">
<input type="hidden" name="firstname[]" value="<?php echo $shuffle_firstname; ?>">
<input type="hidden" name="lastname[]" value="<?php echo $shuffle_lastname; ?>">
<input type="hidden" name="id[]" value="<?php echo $shuffle_id; ?>">
<input type="hidden" name="username[]" value="<?php echo $shuffle_username; ?>">
<input type="hidden" name="email[]" value="<?php echo $shuffle_email; ?>">
<?php
endforeach;
// only show this button if we have done a shuffle
if ( isset($_POST['shuffle'] ) ) :
echo '<input type="submit" value="Finalize Draft Order" name="insert">';
endif;
?>