This is the code for add action, This action adds subjectName(from subjects Table), totalMarks and Description (from SubjectMarks Table)
public function addAction() {
$postArray = $this->params()->fromPost();
$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
This if condition is set true if data is entered and addbutton is clicked
if (isset($postArray['submit']) && $postArray['subjectName']) {
$subjectMarks = new SubjectMarks();
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectName'])
);
$subjectMarks->setClasses(
$entityManager->find('Application\Entity\Classes', $postArray['classId'])
);
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectId'])
);
$subjectMarks->setTotalMarks($postArray['totalMarks']);
$subjectMarks->setDescription($postArray['subjectMarksDesc']);
$entityManager->persist($subjectMarks);
$entityManager->flush();
return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/subjectmarks/totalmarks/index');
}
First else is executed that runs the query and sends data to 'add' view in form of 'data'
else
{
$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$qb = $entityManager->createQueryBuilder();
$qb->select(array(
'subjectMarks.subjectMarkId as subjectMarkId',
'subjectMarks.totalMarks as totalMarks',
'subjectMarks.description as subjectMarksDesc',
'subject.subjectId as subjectId',
'subject.name as subjectName',
'class.classId as classId',
))
->from('Application\Entity\SubjectMarks', 'subjectMarks')
->leftJoin('subjectMarks.subject', 'subject')
->leftJoin('subjectMarks.classes', 'class');
$data = $qb->getQuery()->getScalarResult();
return new ViewModel(array(
'data' => $data,
)
);
}
}
This is the code for add.phtml 'FORM' which is called when 'else' part executes and query selects the data required, and send the written data back, where it is received by if() condition.
<form name="myForm" action="<?php echo $this->url('subjectmarks/totalmarks', array('action'=>'add'));?>" method="POST">
<input type="hidden" name="classId" value="<?php echo $this->data[0]['classId'];?>">
<input type="hidden" name="subjectId" value="<?php echo $this->data[0]['subjectId'];?>">
<input type="text" name="subjectName" >
<input type="text" name="totalMarks" >
<input required="required" type="text" name="subjectMarksDesc" >
<button type="reset" class="btn btn-primary" > <a style="color: white" href="<?php echo $this->basePath('subjectmarks/totalmarks/index');?>">Cancel</a></button>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
The Problem is that totalMarks and subjectMarkDesc are added correctly, while the subjectName is taken id[0] of subject table, for any name that is entered.
The answer is simple, as we can see that subjectName is inserted as input and subjectId is hidden, so there is no direct relation i.e, that clarify which subjectName is assigned to the subjectId.
So putting a select HTML tag will solve the problem, e.g
<select name="addSubjectMarks">
<?php foreach ($this->data as $item):?>
Here we can see subjectId and subjectName are related, unlike the question
<option value="<?php echo $item['subjectId'];?> "> <?php echo $item['subjectName'];?> </option>
<?php endforeach; ?>
</select>
Similarly there in the controller, inside the if (isset($postArray['submit']) && $postArray['subjectName']) condition, we will only replace this code
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['addSubjectMarks'])
);
with this code
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectName'])
);
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectId'])
);
and it will perfectly. The select tag will output only those subjectNames which are already present in the subject Table.
Related
I have an html form (and accompanying php code) that allows user to input task names, which are stored in a PHP array $task_list[].
I also have, below the task list, a dropdown from which users can select a desired value. I want to create a button modify which will allow users to change the value they have selected.
For instance, if the task list is:
Get up
Take a bath
Go for a jog
Go to school
Come back home
A user should be able to select "Go to school", and having done so, click Modify and change that to "Go to work", or something else.
I've made the select dropdown list work and can access the selected value and store it in a variable $selected. But I am completely flummoxed as to how I can allow users to change the value in the $task_list array.
My code–
task_list.php:
<form action="." method="post" >
<!//select dropdown>
<select name='taskid'>
<?php foreach( $task_list as $id => $task ) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<br />
<label> </label>
<input type="hidden" name="action" value='modify'>
<input type=submit value="Modify Task">
<br />
<label> </label>
<input type="hidden" name="action" value='promote'>
<input type=submit value="Promote Task">
</form>
index.php:
//get selected value
function getSelected()
{
if(isset($_POST['taskid']))
{
$myvalue = $_POST['taskid'];
}
else
{
$myvalue = $task_list[0];
}
return $myvalue;
}
//skipping ahead to modify field (all other buttons work perfectly):
case 'modify':
$myval = getSelected(); //$myval returns index of selected item
$selected = $task_list[$myval]; //selected item
break;
I just want to allow the user to click modify and change the value they selected in the $task_list array.
Thank you.
I read a lot of posts talking about issues concerning redirection after form submit but I didn't find any that seems to solve my problem.
I create a quiz website with wordpress.
Each single.php template contains a question.
I use a form to post the data (in the database) which are chosen by the user.
I use the action attribute of the form to redirect to the next post (next question). So far everything works properly, data are stored in the database and users are redirected to the next post.
But when I reach the last post I've set a condition to redirect to a specific page. The redirection works nicely but the data of this last question are not submitted in my database...
Here is my code :
<?php $redirect_url = 0;
if ($currentID !== $nextID):
// NEXT POST
$redirect_url = get_permalink($nextID);
else:
// SPECIFIC PAGE (END QUIZ)
$redirect_url = get_permalink(24);
endif;
?>
<form method="post" action="<?php echo $redirect_url; ?>" id="stat_form" class="hidden">
<input type="radio" name="choice" id="c1" value="1">
<input type="radio" name="choice" id="c2" value="2">
<input type="number" name="replay" id="replay" value="<?php echo strip_tags($_POST['replay']); ?>">
<input type="number" name="question_id" id="question_id" value="<?php echo $currentID; ?>"/>
<input type="submit" name="submit_form" value="submit" />
</form>
<?php
// SAVE INFO IN DATABASE
// ---------------------
if ( isset( $_POST["submit_form"] ) ) {
$table = $wpdb->prefix."stats_questions";
$question_ID = strip_tags($_POST['question_id']);
$choice = strip_tags($_POST['choice']);
$replay = strip_tags($_POST['replay']);
$wpdb->insert(
$table, array(
'question_ID' => (int) $question_ID,
'choice' => (int) $choice,
'replay' => (int) $replay,
)
);
}
// ---------------------
?>
A 'quiz' currently contains a quiz id, name, description and topic (from a topic table).
I am wanting to setup a simple 'Edit Quiz' page.
The problem is - if a quiz is called 'Quiz1' and I change the quiz name on the edit page to 'Quiz2', once the save button is clicked it will revert back to 'Quiz1' and not stored.
I have setup an echo as shown in the code to check that they are actually getting stored, after the save button is clicked it would show 'Quiz1' but this value is not stored in my database. The SQL Has been tested on PhpMyAdmin and seems to work too.
PHP Code:
<? if (!empty($_POST)) {
$id = $_POST['qzid'];
$qzname = $_POST['qzname'];
$qzdesc = $_POST['qzdesc'];
$ctname = $_POST['ctname'];
$checkQuiz = $db->prepare("SELECT qz_name FROM quizzes WHERE qz_name = :qz_name");
$checkQuiz->execute(array(':qz_name' => $qzname));
$qzChanged = "Quiz details updated successfully";
$sql = "UPDATE quizzes SET `qz_name` = :qzname, `qz_desc` = :qzdesc WHERE `quizzes`.`id` = :qzid";
$q = $db->prepare($sql);
$q->execute(array(':qzname' => $qzname, ':qzdesc' => $qzdesc, ':qzid' => $id));
echo $qzname, $qzdesc; //THIS RETURNS THE CHANGED VALUES
}?>
HTML Code:
<form action="edit_quiz.php?id=<?php echo $row['id'] ?>" method="POST">
<input type="hidden" name="qzid" id="qzid" value=""/>
<!-- selection box -->
<p>Topic Name:
<select class="form-control" name="ctname" id="ctname">
<?php
while ($tresult = $stmt->fetch()) {
echo "<option>" . $tresult["ct_name"] . "</option>";
}
?>
</select>
</p>
<p>Quiz Name: <input type="text" name="qzname" value="<?php echo $row['qz_name']; ?>"/></p>
<p>Quiz Description: <textarea name="qzdesc" value=""/><?php echo $row['qz_desc']; ?> </textarea></p>
<input type="submit" class="btn btn-success" value="Save"/> <a class="btn" href="quizzes.php">Back</a>
Please note this code has been simplified.
Any help I would appreciate very much thanks!
You are not posting the id it looks like.
Is your database named localhost, otherwise remove that part.
Basically I am trying to delete database data when a use presses a delete button, the way this works is I have the data in the table, I collect it via an array of arrays (the arrays inside the array represent each record), pass it into the view, display each record (some of it), the user decides whether to delete it, if they press delete then the field data is posted back into my controller and passed into a model where it runs a query to delete the data based on the UID it has, this should work, but what is actually happening is that everything works and I don't get any errors, but the data isn't deleting, I suspect it is because the form isn't posting the data back properly, do some of you deep CI users know why? Here is relevant code:
Controller:
public function clist() { //function to show the client list and edit/delete buttons
$this->load->model('list_model');
$fields = $this->list_model->listcliname();
$data = array();
$data['fields'] = $fields;
$this->load->view('clientlist', $data);
if ($_POST['del'] === 1) { //if the user selects the delete button then call model to delete the fields for the button they selected
$fields = $_POST['fields'];
$this->list_model->delcli($fields);
}
Model:
class list_model extends CI_Model {
public function listcliname()
{
return $this->db->get('clients')->result_array(); //get an array of arrays (one array for each record in table)
}
public function delcli($fields)
{
$this->db->where('UID', $fields['UID']);
$this->db->delete('clients', $fields);
}
}
view:
<html>
<body>
<?php
foreach($fields as $field) {
?>
<ul>
<li>
<?php echo $field['name']; ?>
</li>
<li>
<?php echo $field['contact']; ?>
</li>
<li>
<form action="clistedit" method="post">
<button name="UID" type="submit" value="<?php echo $field['UID']?>">edit</button>
<input type="hidden" name="sub" value="1" />
</form>
</li>
<li>
<form action="clist" method="post">
<input type="hidden" name="fields" value="<?php $field ?>" />
<button name="del" type="submit" value="1">delete</button>
</form>
</li>
</ul>
<?php
}
?>
(you can probably ignore the form with clistedit form action, that is working correctly)
any help is appreciated, thanks!
EDIT: It may be because what I am posting back is an array? I could split up the array and add multiple hidden submits to submit each value and then add them to an array again in my controller but I would like to avoid this.
I think you missed to print the value of field in following line
<input type="hidden" name="fields" value="<?php $field ?>" />
change it to
<input type="hidden" name="fields" value="<?php echo $field ?>" />
You can inspect element to check if hidden field has proper value or not.
So basically it was the fact that I was trying to post back an array! There were also a few other bugs (I will post the revised code) but I was under the impression I would need all of the data to actually delete a record but because the query is something like "DELETE FROM table WHERE UID = someint" then I actually only needed to post back the UID so I echoed the UID index from my field array and fixed the other bugs and it works!
Controller:
public function clist() { //function to show the client list and edit/delete buttons
$this->load->model('list_model');
$fields = $this->list_model->listcliname();
$data = array();
$data['fields'] = $fields;
$this->load->view('clientlist', $data);
if ($_POST['del'] == 1) { //if the user selects the delete button then call model to delete the fields for the button they selected
$fields = $_POST['fields'];
$this->list_model->delcli($fields);
}
Model:
public function delcli($fields)
{
$this->db->where('UID', $fields);
$this->db->delete('clients');
}
}
View:
<html>
<body>
<?php
foreach($fields as $field) {
?>
<ul>
<li>
<?php echo $field['name']; ?>
</li>
<li>
<?php echo $field['contact']; ?>
</li>
<li>
<form action="clistedit" method="post">
<button name="UID" type="submit" value="<?php echo $field['UID']?>">edit</button>
<input type="hidden" name="sub" value="1" />
</form>
</li>
<li>
<form action="clist" method="post">
<input type="hidden" name="fields" value="<?php echo $field['UID']?>" />
<button name="del" type="submit" value="1">delete</button>
</form>
</li>
</ul>
<?php
}
?>
I am trying to figure our how to modify this code so that it creates a checkbox for each entry in the meta_key.
It is a meta box that will be used on the menu page.
The meta_key has multiple entries that I want to appear in the check list of the meta box. I would also like for the URL inputbox to contain the same meta value that was checked as well. If someone could point me in the right direction I would really appreciate it.
Here is the code I used to echo a list of the meta values from the key. I need to use it to create a checkbox for each value.
<?php echo esc_attr( get_post_meta($post->ID, 'newtheme_section_id', true) ); ?>
Here is the code I am using to create the meta box.
if ( !class_exists('JMO_Custom_Nav')) {
class JMO_Custom_Nav {
public function add_nav_menu_meta_boxes() {
add_meta_box(
'wl_login_nav_link',
__('WishList Login'),
array( $this, 'nav_menu_link'),
'nav-menus',
'side',
'low'
);
}
public function nav_menu_link() {?>
<div id="posttype-wl-login" class="posttypediv">
<div id="tabs-panel-wishlist-login" class="tabs-panel tabs-panel-active">
<ul id ="wishlist-login-checklist" class="categorychecklist form-no-clear">
<li>
<label class="menu-item-title">
<input type="checkbox" class="menu-item-checkbox" name="menu-item[-1][menu-item-object-id]" value="-1"> This is the code for checkbox
</label>
<input type="hidden" class="menu-item-type" name="menu-item[-1][menu-item-type]" value="custom">
<input type="hidden" class="menu-item-title" name="menu-item[-1][menu-item-title]" value="Login">
<input type="hidden" class="menu-item-url" name="menu-item[-1][menu-item-url]" value="<?php echo esc_attr( get_post_meta($post->ID, 'newtheme_section_id', true) ); ?>
<input type="hidden" class="menu-item-classes" name="menu-item[-1][menu-item-classes]" value="wl-login-pop">
</li>
</ul>
</div>
<p class="button-controls">
<span class="list-controls">
Select All
</span>
<span class="add-to-menu">
<input type="submit" class="button-secondary submit-add-to-menu right" value="Add to Menu" name="add-post-type-menu-item" id="submit-posttype-wl-login">
<span class="spinner"></span>
</span>
</p>
</div>
<?php }
}
}
$custom_nav = new JMO_Custom_Nav;
add_action('admin_init', array($custom_nav, 'add_nav_menu_meta_boxes'))
You are telling get_post_meta to bring a single value with the third parameter, try with something like:
$sections = get_post_meta($post->ID, 'newtheme_section_id', false);
foreach( $sections as $section ) {
// do something with $section
}