Reputation: 452
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..
Upvotes: 1
Views: 2371
Reputation: 21
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>
Upvotes: 0
Reputation: 292
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,
Upvotes: 0
Reputation: 488
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
Upvotes: 0
Reputation: 2408
Replace
var message = $("input#l_message").val();
with
var message = $("textarea#l_message").val();
Then it should work
Upvotes: 1