1

why it is my code doesn't work? maybe I miss something.

here is my code

View admin_page.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

 <script type="text/javascript">

 // Ajax post
  $(document).ready(function() {

  $(".submit").click(function(event) {

  event.preventDefault();
  var message = $("input#l_message").val();

  jQuery.ajax({
  type: "POST",
  url: "<?php echo base_url(); ?>" + "admin/user_data_submit",
  dataType: 'json',
  data: {l_message: message},
   success: function(res) {
  if (res)
  {
   // Show Entered Value
   jQuery("div#msg").show();
    jQuery("div#msg").html(res..message);
  }
  }
  });

 });
 });
 </script>
<body>
<?php echo form_open();
    echo form_label('Librarians Message');?>
    <textarea class="form-control" name="the_librarian" id="l_message"></textarea>
    <?php echo form_submit('submit', 'Update',"class='submit'");?>
 <?php echo form_close();?>
</body>

in my controller admin.php

public function user_data_submit()
{
    $data = array('message' => $this->input->post('l_message'),
 );
  $this->home_admin_database->librarian_msg_insert($data);
  //Either you can print value or you can send value to database
  echo json_encode($data);
 }

then in my model Home_admin_database.php

public function librarian_msg_insert($data) {
$message = array(
           'message' => $data,
        );
 // Query to insert data in database
  $this->db->where('lm_id', '1');
  $this->db->update('librarians_message', $message);
  if ($this->db->affected_rows() > 0) {
     return true;
    }
   else {
   return false;
   }
  } 

when I click the update button nothing happen and the page refresh without changing the database. help me please...

I want to update the database without refreshing the page then display a message after the success..

3
  • did you use tinymce for textarea ? Commented Feb 3, 2016 at 9:48
  • In ur ajax change "type" to method: "POST" Commented Feb 3, 2016 at 10:12
  • Also check your network tab in the browser. Check if there are any errors being displayed. Commented Feb 3, 2016 at 10:13

4 Answers 4

1

Replace

 var message = $("input#l_message").val();

with

var message = $("textarea#l_message").val();

Then it should work

Sign up to request clarification or add additional context in comments.

8 Comments

not working sir kumar, I tried to put alert('test') before the jQuery.ajax then make the jQuery.ajax as comment. when I click the button the alert popup shown but if I remove the the jQuery.ajax as comment nothing happen and my page refresh then nothing change to my database...
@MGBC jQuery("div#msg").html(res..message); to jQuery("div#msg").html(res.message);
doesn't work sir even I change it. I think there was a problem on the ajax syntax sir, I tried to put alert(); before the jQuery.ajax and remove the jQuery.ajax method., when I click the button the alert popup shown... maybe the ajax is the problem but dont know how to fix it...
I admit, Im not expert in English language, sorry for that.
@MGBC YOur ajax is working now just open your console and look for network tab and go to last and look for "user_data_submit" and add a screenshot link of that.
|
0

in your view file try using

echo form_button('submit', 'Update',"class='submit'");

instead of

echo form_submit('submit', 'Update',"class='submit'");

Try your luck with the submit event instead of click if above not working...

Also Replace

var message = $("input#l_message").val();

with

var message = $("#l_message").val();

=> Another step is to use

<form Method="Post" class="submit_form">

instead of

echo form_open();

or remove form action attribute by jquery on document load

and $(".submit_form").submit(function(event) {...}); for ajax call

4 Comments

I have done some editing to my answer.. please try... good luck
i tried but nothing happen even I change it into submit. is there any way to trace the problem like putting an alert?
try Another step is to use <form Method="Post" class="submit_form"> instead of echo form_open(); or remove form action attribute by jquery
Sir Raj my javascript does not have any class name "submit_form" is that ok?
0

Hello i guess you using tinymce, tinymce cannot get value like that you should add to get value

tinyMCE.triggerSave();

then please change your javascript

 <script type="text/javascript">

 // Ajax post
  $(document).ready(function() {

  $(".submit").click(function(event) {

  event.preventDefault();
  tinyMCE.triggerSave();
  var message = $("#l_message").val();

alert(message) //please see is it right value or not !

  jQuery.ajax({
  type: "POST",
  url: "<?php echo base_url(); ?>" + "admin/user_data_submit",
  dataType: 'json',
  data: {l_message: message},
   success: function(res) {
  if (res)
  {
   // Show Entered Value
   jQuery("div#msg").show();
    jQuery("div#msg").html(res..message);
  }
  }
  });

 });
 });
 </script>

hope this help someone using tinymce,

1 Comment

not working sir,, the error is GET localhost/nmsc/js/jquery.min.js is there any problem with jquery?
0

Change your View code with this :

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
 <script type="text/javascript">

  $(document).ready(function() {
        $("#submit").click(function(){
          // get all post from form with id form-msg
            dataString = $("#form-msg").serialize(); 
            $.ajax({
            type:"POST",
            url:"<?php echo base_url(); ?>index.php/admin/user_data_submit", 
            //parsing data to your controller
            data:dataString, 

            success:function (data) {
              // Show Entered Value
            $("div#msg").show();
            $("div#msg").html(data);
                alert('Success');
            }

            });

        return false;
        }); 
    }); 
 </script>

<body>

    <?php  
    // add form id
    echo form_open('',"id='form-msg'"); 
    echo form_label('Librarians Message');?>
    <textarea class="form-control" name="the_librarian" id="l_message"></textarea>    
    <?php 
    // add button
    echo form_button('submit', 'Update',"id='submit'"); 
    ?>
    <?php echo form_close();?>

</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.