Wordpress Post not getting updated in editor - php

I was building a custom plugin for one of my project and I am facing this issue with custom fields that I have created, but once I was trying to save the fields. I am not getting the data displayed in the admin editor. screenshot of issue page
I am adding the code below Please find it.
<?php
function apt_add_fields_metabox()
{
add_meta_box(
'apt_college_fields',
__('College Fields'),
'apt_college_fields_callback',
'college',
'normal',
'default'
);
}
add_action('add_meta_boxes','apt_add_fields_metabox');
//Display Fields Metabox Content
function apt_college_fields_callback($post)
{
wp_nonce_field(basename(__FILE__),'wp_college_nonce');
$apt_college_stored_meta = get_post_meta($post->ID);
?>
<div class="wrap college-form">
<div class="form-group">
<label for="issue_check"><?php esc_html_e('Issue Date Available','apt_domain'); ?></label>
<select name="issue_check" id="issue_check">
<?php
$option_value = array('Yes','No');
foreach($option_value as $key => $value)
{
if($value == $apt_college_stored_meta['issue_check'][0])
{ ?>
<option selected><?php echo $value; ?></option>
<?php
}
else
{
?>
<option ><?php echo $value; ?></option>
<?php
}
}
?>
</select>
</div>
<div class="form-group">
<label for="college-details"><?php esc_html_e('College Details','apt_domain'); ?></label>
<?php
$content = get_post_meta($post->ID,'college-details',true);
$editor = 'college-details';
$settings = array(
'textarea_rows' => 5,
'media_buttons' => true
);
wp_editor($content,$editor,$settings);
?>
</div>
<div class="form-group">
<label for="application_date"><?php esc_html_e('Application Available Date','apt_domain'); ?></label>
<input type="date" name="application_date" id="application_date" value="<?php if(!empty($apt_college_stored_meta['application_date'])) echo esc_attr($apt_college_stored_meta['application_date'][0]); ?>" />
</div>
</div>
<?php
}
function apt_college_save($post_id)
{
$is_autosave = wp_is_post_autosave($post_id);
$is_revision = wp_is_post_revision($post_id);
if(isset($_REQUEST['wp_college_nonce']) && wp_verify_nonce($_REQUEST['wp_college_nonce'],basename(__FILE__)))
{
$is_valid_nonce = true;
}
else
{
$is_valid_nonce = false;
}
if($is_autosave || $is_revision || !$is_valid_nonce)
{
return;
}
if(isset($_REQUEST['issue_check']))
{
update_post_meta($post_id,'issue_check',sanitize_text_field(['issue_check']));
}
if(isset($_REQUEST['college-details']))
{
update_post_meta($post_id,'college-details',sanitize_text_field(['college-details']));
}
if(isset($_REQUEST['application_date']))
{
update_post_meta($post_id,'application_date',sanitize_text_field(['application_date']));
}
}
add_action('save_post','apt_college_save');
?>

There is an error with your code block.you missed out $_REQUEST['issue_check']
update_post_meta($post_id,'issue_check',sanitize_text_field(['issue_check']));
Corrected Code
update_post_meta($post_id,'issue_check',sanitize_text_field($_REQUEST['issue_check']));

Related

How to Save the Checkbox Value Form to MySQL in One Field

How to Save the Checkbox Value Form to MySQL in One Field, can anyone help me..
Here is my code:
function student($param1 = '') {
if ($this->session->userdata('admin_login') != 1) {
redirect('login', 'refresh');
}
$running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
if ($param1 == 'create') {
$data['ekskul_id'] = $this->input->post('ekskul_id');
$this->db->insert('student', $data);
}
}
this is my display code
<div class="form-group">
<label for="field-2" class="col-sm-3 control-label">Ekstrakurikuler</label>
<div class="col-sm-5">
<select data-placeholder="Select" name="ekskul_id[]" multiple class="chosen-select" tabindex="8">
<?php
$ekstra = $this->db->get('ekstrakurikuler')->result_array();
foreach($ekstra as $row):
?>
<option value="<?php echo $row['ekskul_id'];?>"><?php echo $row['ekskul_name'];?></option>
<?php
endforeach;
?>
</select>
</div>
</div>
use below code to store multiple id in one field:
<?php
$ekskul_id= implode(",",$_POST['ekskul_id']);
?>
In your code :
if(isset($this->input->post('ekskul_id'))){
$data['ekskul_id'] = implode(",",$this->input->post('ekskul_id'));
}

Codeigniter Searchbar

Searchbar is working in codeigniter website but it does not load suggestion products. As I want if someone write 'dell' in searchbay he should get dell laptops suggestions below. and one another thing 'enter' button does not work for search one have to click the search icon to search the product.code is below
Controller
function text_search(){
if ($this->crud_model->get_settings_value('general_settings','vendor_system') !== 'ok') {
$search = $this->input->post('query');
$category = $this->input->post('category');
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}else{
$type = $this->input->post('type');
$search = $this->input->post('query');
$category = $this->input->post('category');
if($type == 'vendor'){
redirect(base_url() . 'index.php/home/store_locator/'.$search, 'refresh');
} else if($type == 'product'){
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}
}
}
Model
function get_type_name_by_id($type, $type_id = '', $field = 'name')
{
if ($type_id != '') {
$l = $this->db->get_where($type, array(
$type . '_id' => $type_id
));
$n = $l->num_rows();
if ($n > 0) {
return $l->row()->$field;
}
}
}
function get_settings_value($type, $type_name = '', $field = 'value')
{
if ($type_name != '') {
return $this->db->get_where($type, array('type' => $type_name))->row()->$field;
}
}
View
<div class="header-search">
<?php
echo form_open(base_url() . 'index.php/home/text_search/', array(
'method' => 'post'
));
?>
<input class="form-control" type="text" name="query" placeholder="<?php echo translate('what_are_you_looking_for');?>?"/>
<select
class="selectpicker header-search-select cat_select hidden-xs" data-live-search="true" name="category"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="0"><?php echo translate('all_categories');?></option>
<?php
$categories = $this->db->get('category')->result_array();
foreach ($categories as $row1) {
if($this->crud_model->if_publishable_category($row1['category_id'])){
?>
<option value="<?php echo $row1['category_id']; ?>"><?php echo $row1['category_name']; ?></option>
<?php
}
}
?>
</select>
<?php
if ($this->crud_model->get_type_name_by_id('general_settings','58','value') == 'ok') {
?>
<select
class="selectpicker header-search-select" data-live-search="true" name="type" onchange="header_search_set(this.value);"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="product"><?php echo translate('product');?></option>
<option value="vendor"><?php echo translate('vendor');?></option>
</select>
<?php
}
?>
<button class="shrc_btn"><i class="fa fa-search"></i></button>
</form>
</div>
<!-- /Header search -->

validate if checkbox has been checked

I'm trying to show an error message if a checkbox has not been selected. I've managed to get it done input field, but unsure of how to get it done with a checkbox. This is what I have so far:
<?php
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
<div class="control-group <?php if (!empty($author_error)){ echo 'error'; } ?>">
<label class="control-label">Author</label>
<div class="controls">
<select name="author" id="author">
<option value="">Select one</option>
<?php $sql2 = 'SELECT id, name FROM author';
foreach ($dbConnection->query($sql2) as $data2) { ?>
<option value="<?php echo $data2['id']; ?>"
<?php if(isset($author) && $author == $data2['id']) { echo 'selected'; } ?>>
<?php echo $data2['name']; ?>
</option>
<?php } ?>
</select>
<?php if (!empty($author_error)) { echo '<span class="help-inline">' . $author_error . '</span>'; } ?>
</div>
</div>
<div class="control-group <?php if (!empty($categories_error)){ echo 'error'; } ?>">
<fieldset>
<legend class="control-label">Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $data3) { ?>
<div class="controls">
<label for="category<?php echo($data3['id']);?>">
<input type="checkbox" name="categories" id="categories" value="<?php echo($data3['id']); ?>"
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($data3['name']); ?>
</label>
</div>
<?php } ?>
</fieldset>
<?php if (!empty($categories_error)) { echo '<span class="help-inline controls">' . $categories_error . '</span>'; } ?>
</div>
Where am I going wrong with the categories field?
Check it like:
isset($_POST['categories'])
On line #62 you are checking if $categories_error is empty but you are using variable $categories for storing error message on line #19

How can I create a php mail handler for a dynamically created form

I am creating a form builder plugin for wordpress allowing users to build their own custom forms.
I have managed to design the form builder, now I am looking at the form submit handler. I'm using a php handler, something similar to this:
<?php
// process.php
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (empty($_POST['name']))
$errors['name'] = 'Name is required.';
if (empty($_POST['email']))
$errors['email'] = 'Email is required.';
if (empty($_POST['superheroAlias']))
$errors['superheroAlias'] = 'Superhero alias is required.';
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// if there are no errors process our form, then return a message
// DO ALL YOUR FORM PROCESSING HERE
// THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}
// return all our data to an AJAX call
echo json_encode($data);
However, as mentioned, my forms are created dynamically, so I can't use hard coded variables.
I'm not sure of a good way forward with this, can anyone suggest a way I can use the dynamically created variables in my main form builder file as part of this handler file?
Here is my php builder code:
<form action="./includes/process.php" method="POST">
<?php foreach ( wp_parse_id_list( $widget[ 'form_builder_ids' ] ) as $form_builder_key ) {
....
<div class="media-body <?php echo ( isset( $item['design']['fonts'][ 'align' ] ) ) ? $item['design']['fonts'][ 'align' ] : ''; ?>">
<?php if( $this->check_and_return( $item, 'label') ) { ?>
<label>
<?php echo $item['label']; ?>
<?php if( $this->check_and_return( $item, 'required') ) { ?>
<span class="required" style="color:#c0392b;">*</span>
<?php } ?>
</label>
<?php } ?>
<?php if( $this->check_and_return( $item, 'input_type') ) { ?>
<?php
$input_type_array = array('select', 'textarea', 'checkbox', 'radio');
if( !in_array( $item['input_type'] ,$input_type_array ) ) {?>
<input type="<?php echo $item['input_type']; ?>" name="<?php echo $item['input_name']; ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>>
<?php } else if ($item['input_type'] == 'textarea') { ?>
<textarea name="<?php echo $item['input_name']; ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>></textarea>
<?php } else if ($item['input_type'] == 'select') { ?>
<select name="<?php echo $item['input_name']; ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>>
<?php foreach(explode("\n", $item['select_options']) as $select_option) { ?>
<option value="<?php echo preg_replace('/\s+/','', $select_option); ?>"><?php echo $select_option; ?></option>
<?php } ?>
</select>
<?php } else if ($item['input_type'] == 'checkbox') { ?>
<?php foreach(explode("\n", $item['select_options']) as $select_option) { ?>
<input type="checkbox" name="<?php echo $item['input_name']; ?>" value="<?php echo preg_replace('/\s+/','', $select_option); ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>><?php echo $select_option; ?>
<?php } ?>
<?php } else if ($item['input_type'] == 'radio') { ?>
<?php foreach(explode("\n", $item['select_options']) as $select_option) { ?>
<input type="radio" name="<?php echo $item['input_name']; ?>" value="<?php echo preg_replace('/\s+/','', $select_option); ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>><?php echo $select_option; ?>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
....
</form>
I'd achieve something like this by setting an incremental javascript global var:
var x = 0;
function addForm(){
var formHTML = '<form onsubmit = "submitForm(' + x + '); return false;"> <input id = "name' + x + '" ... </form>';
x = x+1;
}
function submitForm(formIdentifier){
var name = document.getElementById("name"+formIdentifier).value; //how you can get the form values from dynamic forms
//process form with ajax from here
}
Where addForm() is that code that would add the form to the page, and submitForm is where you would process the data from a dynamic form submit event.
Let me know if you need some more clarification.

How do i retrieve the value of dropdown when editing in codeigniter

OK, so I have looked everywhere for a solution to my problem but found none so far.My code looks like this.I have created a dynamic view page in which edit and add views are loaded dynamically.But the problem arises when i try to retain the value of select dropdown during editing.I would be grateful if someone could help me out.
View
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php if(#$patient_info){
echo '<h1 class="page-header">Edit Patient</h1>';
}else{
echo '<h1 class="page-header">Add Patient</h1>';
}
?>
<?php if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div>
<?php } ?>
<?php if($this->session->flashdata('erroredit')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('erroredit'); ?></div>
<?php } ?>
<?php //echo validation_errors('<div class="alert alert-danger">','</div>'); ?>
<form role="form" method="post" action="<?php echo isset($patient_info) ? site_url('home/patient/edit') .'/' .$patient_info->patientID : site_url('home/patient/new'); ?>">
<div class="form-group">
<?php echo form_error('pname', '<div class="alert alert-danger">', '</div>'); ?>
<label for="pname">Patient Name</label>
<input class="form-control" placeholder="Enter Patient Name" name="pname" value="<?php echo isset($patient_info) ? $patient_info->patientName : ''; ?>">
</div>
<!-- Dropdown menu for selecting clinic -->
<div class="form-group">
<label for="select">Select Clinic Name</label>
<select class="form-control" name="selectClinic">
<option value="none">Select Clinic Below</option>
<?php foreach($allclinic as $key=>$clinic){ ?>
<!--<?php //foreach($clinicByPatient as $clin): ?>-->
<option value="<?php $clinic->clinicID; ?>"
<?php if(isset($patient_info)){
echo 'selected="selected"';
}
?>
>
<?php echo $clinic->clinicName; ?>
</option>
<?php //endforeach; ?>
<?php } ?>
</select>
</div>
<!-- Select Clinic ends-->
<div class="form-group">
<label for="select">Select Dentist</label>
<select class="form-control" name="selectDentist">
<option value="">Select Dentist</option>
<?php foreach($dentistdet as $key=>$dentist){ ?>
<option value="<?php echo $did = $dentist->dentistID;?>"><?php echo $dentist->dentistName; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="<?php echo isset($patient_info) ? 'Update Patient' : 'Add Patient'; ?>" >
</div>
</form>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg btn-block" value="Cancel">
</div>
Edit controller
public function edit(){
$id = $this->uri->segment(4);
$this->load->library('form_validation');
$this->load->model('clinic_model');
$this->load->model('dentist_model');
$data['patient_info'] = $this->patient_model->getPatientById($id);
$data['clinicByPatient'] = $this->patient_model->getPatientByClinic();
$data['allclinic'] = $this->clinic_model->getAllClinics();
// $data['clinicdet'] = $this->patient_model->getPatientByClinic();
if($_POST){
$this->form_validation->set_rules('pname', 'Patient Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('paddress', 'Patient Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('pcontact', 'Patient Contact', 'trim|required|xss_clean');
if($this->form_validation->run()== FALSE){
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout', $data);
}else{
$patientname = $this->input->post('pname');
$patientaddress = $this->input->post('paddress');
$patientcontact = $this->input->post('pcontact');
$select = $this->input->post('selectClinic');
$option = $this->input->post('selectDentist');
$edited = $this->patient_model->editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option);
if($edited){
$this->session->set_flashdata('successedit', 'Successfully updated the record');
redirect('home/patient');
}
}
}else{
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout',$data);
}
}
Model looks like this
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Patient_model extends CI_Model{
public function countPatients(){
$countPatient = $this->db->count_all('patient');
return $countPatient;
}
public function getallpatients(){
$query = $this->db->get('patient');
if($query->num_rows()>0){
return $query->result();
}
else{
return FALSE;
}
}//getallpatients function ends
public function getPatientByClinic(){
$this->db->select('*');
$this->db->from('patient');
$this->db->join('clinic', 'patient.clinicID = clinic.clinicID', 'left');
$this->db->join('dentist', 'patient.dentistID = dentist.dentistID', 'left');
$query = $this->db->get();
if($query->num_rows>0){
return $query->result();
}
}
public function addPatientByClinic($patientname, $patientadd, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientadd,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
return $this->db->insert('patient',$data);
}// method ends
public function deletePatient($id){
$verifyID = array('patientID' => $id);
// $affRows = $this->db->affected_rows();
// $obj = new Patient_model;
if($verifyID){
$this->db->where($verifyID);
$this->db->delete('patient');
if($this->db->affected_rows()){
return TRUE;
}
}
}
public function editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientaddress,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
$query = $this->db->where('patientID', $id)
->update('patient', $data);
if($query){
return true;
}
}//method ends
public function getPatientById($id){
$query = $this->db->where('patientID', $id)
->get('patient');
return $query->row();
}
}//class ends
?>
In the code for your select box, you aren't actually echoing $clinic->clinicID. Thus, when the form is submitted, the value will be empty.
You also need to be careful with how you are choosing which select element will be selected by default - you aren't comparing with anything that will change within the loop. Should you be checking against $clinic->clinicID?

Categories