fckediter removing formatting in codeigniter - php

I am using FCKEditer with codeigniter and when I submit data to database, it remove formatting before submitting data.
for example, if I submit
<span style="color: rgb(255, 0, 0);">know-how in the field of Water</span>
the data store in table will be
<span>know-how in the field of Water</span>
I have form like below
<form action="#" method="post" id="frm" name="frm" class="form_class">
<label id="validateeditor" >Description</label>
<?
$dataf = array(
'name' => 'page_description',
'id' => 'page_description',
'toolbarset' => 'Default',
'basepath' => base_url().'application/plugins/fckeditor/',
'width' => '700',
'height' => '400'
);
echo form_fckeditor( $dataf, #$result['page_description']);
</form>
I am using following jquery code to save data in database
<script type="text/javascript">
$(document).ready(function(){
jQuery("#frm").validationEngine('attach',{
promptPosition :'centerRight',
onValidationComplete: function(form, status){
if(status)
{
insert_update();
}
}
})
});
function insert_update()
{
var fckvalue = FCKeditorAPI.GetInstance('page_description').GetXHTML();
$("#page_description").val(fckvalue);
var formdata = $("#frm").serialize();
$.ajax({
type: 'POST',
url: '<?php echo site_url(ADMIN_DIR."cms/insert_update"); ?>',
data: formdata,
success:function (data){
var msg;
if(jQuery.trim($("#<?php echo $u_column;?>").val())!='')
{
msg = "Record Updated Successfully.";
}
else
{
msg = "Record Inserted Successfully.";
}
jQuery.noticeAdd({text:msg});
$("#list").trigger("reloadGrid");
editDialog.dialog("close");
}
});
}
</script>
I don't know what is wrong but when I save data it remove formatting as above.

This is happening as a result of XSS filtering on your input.
Here are a few things you should check to ensure this field doesn't get cleaned:
You might have a validation rule filtering the input: $this->form_validation->set_rules('page_description', 'Description', 'required|xss_clean');
Check if $config['global_xss_filtering'] = TRUE; in your config.php file.
Your input will be filtered if you pass TRUE as a second parameter on the input: $this->input->post('page_description', TRUE);

Related

Passing data from ajax in elgg

This is using Elgg 1.8, social networking engine.
I'm new to ajax and trying to make the form submit without refresh, it submits the form but adds item to the list without title.
The form is displayed in fancybox popup.
Here is my document ready script:
$(document).ready(function() {
$('.elgg-form-announcements-save').live('submit', function(e) {
//e.preventDefault();
var $form = $(this);
elgg.action($form.attr('action'), {
data: $form.serialize(),
success: function(data) {
// check the status and do stuff
//e.preventDefault();
var river = $('ul.announcements_list');
if (river.length < 1) {
river.append(data.output); } else {
$(data.output).find('li:first').hide().prependTo(river).slideDown(500);
};
$.fancybox.close();
}
});
return false; // prevent the form from submitting
e.preventDefault();
}); });
Here is code in the action php file:
if ($announcement->save()) {
elgg_clear_sticky_form('announcement');
system_message(elgg_echo('announcement:save:success'));
if (elgg_is_xhr()) {
$options = array( 'type' => 'object', 'subtype' => 'announcements', 'full_view' => false,
'pagination' => false, 'limit' => 1 );
$newannouncement = elgg_list_entities($options);
echo json_encode($newannouncement); }
} else {
I'm not sure what I need to do here, maybe use "success: function(json) {" instead of "success: function(data) {" but not sure how to implement that.
I tried using ajaxForm with no success.
Thank you.

How to use post() jquery method with codeigniter form_validation

I want to validate a form without refreshing the page using the .post() jQuery method.
I use codeigniter for validation. Could you please tell me how to make it right? I find it pretty confusing ...
Here is the jQuery code:
$(document).ready(function(){
$(".form_errors").hide();
$("#send").on("click",function(){ //the submit button has the id="send"
$(".form_errors").hide(); //these are <p> for each input to show the error
var user=$("input.box");
var data={};
var names=$("input.box").attr("name");
for(i=0;i<user.length;i++){
name=names[i];
value=user[i].val();
data.name=value;
}
$.post('ksite/account',
data,
function(result){
$("div.answer").html(result);
for(i=0;i<user.length;i++){
error_message=<?php echo form_error("?>names[i]<?php ");?>;
$("p#error_"+names[i]+".form_errors").html(error_message).show();
}
}
return false;});
});
form_error is a CodeIgniter function. (I suppose someone who used ci is familiar with).
The form:
<p id="error_user" class="form_errors"></p>
<input type="text" class="box" name="user">
<p id="error_password" class="form_errors"></p>
<input type="password" class="box" name="password">
<input type="submit" id="send">
Is the form tag neccessary ? And if yes,do i have to mention action and method ?
Do I have to specify the type of the response?
And in ksite/account I do:
/* ...... */
if (!this->form_validation->run(''account")) {
echo "The account couldn't be made";
} else {
echo "The account was successfully created ";
}
P.S.Although you may not be familiar with codeigniter, I would appreciate if someone could tell me if the code is correct and what improvements could be made.
Here is what I did.
You have to Ajax for getting data without refreshing the page.
HTML Page
$form = $(form);
var url = $form.attr('action');
dataString = $form.serialize();
$.ajax({
type: "POST",
url: url,
data: dataString,
dataType: "json",
success: function(data) {
$(data).each(function(j,details){
var status = details.status;
var message = details.message;
$('#message_ajax_register').show();
$('#message_ajax_register').html('<div class="alert alert-success">'+message+'</div>');
});
}
});//end of $.ajax**
I am first setting up the rules in my controller method and then validating it.
Controller
public function update_fest()
{
if($this->input->post())
{
$this->form_validation->set_rules('txtgpluswebsite', 'Google Plus Page URL', 'trim|xss_clean|prep_url');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run() == false){
$message = validation_errors();
$data = array('message' => $message,'status'=>0);
}
else{
$message = $this->add_fest_database();
$data = $message;
}
}
else{
$message = "Fest details are required";
$data = array('message' => $message,'status'=>0);
}
$this->output->set_content_type('application/json');
$json = $this->output->set_output(json_encode($data));
return $json;
}
If validation run is not false, then go to add_fest_database(other function). In that function,
function add_fest_database()
{
$youtubeWebsite = $this->input->post('txtyoutubewebsite');
$gplusWebsite = $this->input->post('txtgpluswebsite');
$this->load->model('model_fest');
$data = array("fest_youtube"=>$youtubeWebsite,"fest_gplus"=>$gplusWebsite);
return data;
}

Ajax response to only allow data in field if true

I need to have a field that will not allow data to be inputted if it returns a message of "false". The field "ponumber" checks against a DB and if that record already exists I dont want it to allow the user to use that particular PO Number. Basically it blanks it out if they leave the field and displays the message. The script is working perfect except now allowing the input based on the false return from checkponumberajax php file.
$(document).ready(function () {
var validateponumber = $('#validateponumber');
$('#ponumber').keyup(function () {
var t = this;
if (this.value != this.lastValue) {
if (this.timer) clearTimeout(this.timer);
validateponumber.removeClass('error').html('<img src="/images/ajax-loader.gif" height="16" width="16" /> checking availability...');
this.timer = setTimeout(function () {
$.ajax({
url: 'includes/checkponumberajax.php',
data: 'action=check_ponumber&ponumber=' + t.value,
dataType: 'json',
type: 'post',
success: function (j) {
validateponumber.html(j.msg);
}
});
}, 200);
this.lastValue = this.value;
}
});
});
<input type="text" name="ponumber" value="<?=#$_REQUEST['ponumber']?>" id="ponumber" />
and my checkponumber.php file returns like this
if ($records != 0) {
$response = array(
'ok' => false,
'msg' => "The selected PO# is not available");
} else {
$response = array(
'ok' => true,
'msg' => "This PO# is free");
}
Edit: Solved!
I ended up thanks to #bourch using this which simply blanks out the field once a value is reached that matches false
if(j.ok != true){$("#ponumber").attr("value", "");}
Try this:
if(j.ok != true){$("#ponumber").attr("disabled", "disabled");
}

AJAX form in Codeigniter responds with error when valid [half working]

From further developments, I have a AJAX and CI email form that works perfectly with javascript disabled (using Codeigniter code) and when on using AJAX there are a few bugs.
When there are missing boxes and press submit it pops up with the error mmessage-great, but when filled in correctly and on submit it still posts the error even though its right and the email i still receive in my inbox.This is really bugging me as its something in my AJAX code and i think i am missing something in my controller.
Another question:On the AJAX form it is posting personal error/success messages- is there a way of echoing CI $success and echo validation_errors(); in this so they are the same?
The code is what i have so far:
TO see my controller follow this link: Codeigniter AJAX email validation
VIEW
<h1>Contact</h1>
<div id="contact">
<?php
//This is loaded in the view as it wont be used in the other pages
$this->load->helper('form');
echo form_open('contact/send_email');
//success message if passed validation checks
echo '<div id="success">';
echo $success;
echo '</div>';
// empty div for error messages (ajax)
echo '<div id="errors">';
// empty div for error messages (php)
if(validation_errors() != ""){
echo '<h3>Please correct the following errors:</h3>';
echo validation_errors();
}
echo '</div>';
echo form_label('Name: ', 'name');
$data = array (
'name' => 'name',
'id' => 'name',
'value' => set_value('name')
);
echo form_input($data);
echo form_label('Email: ', 'email');
$data = array (
'name' => 'email',
'id' => 'email',
'value' =>set_value('email')
);
echo form_input($data);
echo form_label('Message: ', 'message');
$data = array (
'name' => 'message',
'id' => 'message',
'value' =>set_value('message')
);
echo form_textarea($data);
?>
<br />
<?php
echo form_submit('submit', 'Send');
echo form_close();
?>
AJAX
<script type="text/javascript">
$(function() {
$('form').submit(function() {
// get the form values
var form_data = {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val()
};
// send the form data to the controller
$.ajax({
url: "<?php echo site_url('contact/send_email'); ?>",
type: "POST",
data: $(form_data).serialize(),
success: function(msg) {
if(msg.validate)
{
$('form').prepend('<div id ="success">Success</div>');
$('#success').fadeOut(5000);
}else
$('form').prepend('<div id ="errors">Error</div>');
$('#errors').fadeOut(5000);
}
});
// prevents from refreshing the page
return false;
});
});
</script>
Looks to me like this line (in your javascript)
if(msg.validate)
is looking for an object, where your script isn't actually parsing any JSON or returning any JSON.
What I can only assume would be a good idea here (after looking at your controller too) would be to check if the <div id="errors"> has anything inside it in the response
Then in you javascript you would need to make a few changes
success: function(msg) {
if(msg.validate)
{
$('form').prepend('<div id ="success">Success</div>');
$('#success').fadeOut(5000);
}
else
{
$('form').prepend('<div id ="errors">Error</div>');
$('#errors').fadeOut(5000);
}
});
will become
success: function(msg) {
$msg = $(msg);
// check to see if the div with id="errors" contains a h3 tag
if($msg.filter('#errors').find('h3').length < 1)
{
$('form').prepend('<div id ="success">Success</div>');
$('#success').fadeOut(5000);
}
else
{
$('form').prepend('<div id ="errors">Error</div>');
$('#errors').fadeOut(5000);
}
});
Note: I've written this off the cuff so I haven't actually tested it but it should point you in the right direction.
Edited to use id selectors and not class selectors (habit of mine)

autocomplete value in textbox codeigniter

I have done to make control autocomplete, but I have a problem to post data with jquery.
<input type="text" id="matakuliah" class="med" name="matakuliah">
<script type="text/javascript">
$(this).ready( function() {
$("#matakuliah").autocomplete({
minLength: 1,
source:
function(req, add){
$.ajax({
url: "<?php echo site_url('bahanAjar/lookup'); ?>",
dataType: 'json',
type: 'POST',
data:req,
success:
function(data){
if(data.response =="true"){
add(data.message);
}
},
});
},
});
});
</script>
on my controller
function lookup(){
// process posted form data (the requested items like province)
$keyword = $this->input->post('term');
$data['response'] = 'false'; //Set default response
$query = $this->matakuliah_model->lookup($keyword); //Search DB
if( ! empty($query) )
{
$data['response'] = 'true'; //Set response
$data['message'] = array(); //Create array
foreach( $query as $row )
{
$data['message'][] = array(
'id_matakuliah'=>$row->id,
'value' => $row->matakuliah,
''
); //Add a row to array
}
}
if('IS_AJAX')
{
echo json_encode($data); //echo json string if ajax request
}
else
{
$this->load->view('admin/bahan_ajar/form_manage_file_view', $data); //Load html view of search results
}
}
The code work it well, but I want to add parameter to call database.
$query = $this->matakuliah_model->lookup($keyword, $id_matakuliah);
like this. how I can get
$this->input-<post('id_matakuliah')
from jquery before.;
and I have another textbox for fill value of autocomplete from textbox matakuliah.
`<input type="hidden" id="matakuliah_post" class="med" name="matakuliah_post">`
When I'm use autocomplete textbox automatic fill another textbox, please help me.
In this case req will contain {term:"your search term"}. Your can extend this javascript object to pass extra data. If you want to post id_matakuliah, you can assign its value like following before $.ajax call:
req.id_matakuliah = "Whatever you want to send";

Categories