codeigniter sound notification system - ajax

here is notification method
public function activetask() {
$notification['ReceiverID']=$this->input->post('user_reciver_id');
$notification['SenderID']=$this->input->post('createdby');
$notification['Status']=$this->input->post('task');
$notification['AppID']=$this->input->post('App_id');
$notification['TaskID']=$this->input->post('PhaseID');
$notification['CreatedDate']=date('D M Y h:i');
$this->Mapps->notification($notification);
if($this->input->post('user_reciver_id')==$this->session->userdata('id')){
echo '<audio controls autoplay>
<source src="<?php echo base_url(); ?>notify.ogg" type="audio/ogg">
<source src="<?php echo base_url(); ?>notify.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>';
}
here ajax function call on click event
function active(task,user_reciver_id,createdby,Task_id,App_id,PhaseID){
$.ajax({
type: "post",
url: '<?php echo base_url();?>users/activetask',
cache: false,
data: {task: task,Task_id:Task_id,App_id:App_id,PhaseID:PhaseID,user_reciver_id:user_reciver_id,createdby:createdby},
success: function(response){
location.reload();
}
});
}
it does not work for me where is problem
how to solve it
i send some detail one user to other
if receiver user is login then listen notification sound

You need to append the echo reponse to some element.
First, Create an element in the html.
<div id="audios" style="display:none;">We are audios!</div>
Then, get the response in the ajax and append it to #audios before reload the window.
$.ajax({
type: "post",
url: '<?php echo base_url();?>users/activetask',
cache: false,
data: {task: task,Task_id:Task_id,App_id:App_id,PhaseID:PhaseID,user_reciver_id:user_reciver_id,createdby:createdby},
success: function(response){
var audio_html = response;
$('#audios').append(audio_html);
location.reload();
}
});

Related

Data not passing in ajax call in Codeigniter

I am sending a file through ajax. Instead of passing the csrf token its not available in the controller
Bellow is code
<script type="text/javascript">
var csrfName = '<?php echo $this->security->get_csrf_token_name(); ?>',
csrfHash = '<?php echo $this->security->get_csrf_hash(); ?>';
$(document).on("change","#channel_setup_file",function(e){
var formData = new FormData($("#process-form")[0]);
$.ajax({
url: "<?php echo site_url('parse-setup-file')?>",
type: "POST",
data: {cform : formData,csrfName:csrfHash},
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
error: function(msg){
},
success: function(msg){
}
});
});
</script>
I am getting only the url in the controller.
Expecting that you are getting both token and name so in context to that I am writing the code
var formData = new FormData($("#process-form")[0]);
$.ajax({
url: "<?php echo site_url('parse-setup-file')?>",
type: "POST",
data: {cform : formData,cName:csrfName,cToken:csrfHash},
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
error: function(msg){
},
success: function(msg){
}
});

Form validation in codeigniter when ajax used to form submit

Form submit is not happened in this scenario..
$.ajax({
type: "POST",
async: false,
url: base_url+"register/registration_val",
data: "register_first_name="+first_name,
success: function(data){
$('#inferiz').html(data);
},
error: function(){
alert('error');
}
In your view you can add this:
<script type="text/javascript">
var base_url = "<?php print base_url(); ?>";
</script>
Plus try to alert and see the value of final url in ajax i.e alert(url);
Try adding a id to the firstname input
<script type="text/javascript">
$(document).on('submit','#form-reg',function(){ // #form-reg is id on form open tag
$.ajax({
url: "<?php echo base_url('register/registration_val');?>",
type: 'POST',
data: {
firstname: $('#firstname').val(),
},
dataType: 'html', // I perfer to use json
success: function(data){
$('#inferiz').html(data);
},
error: function(){
alert('error');
}
}
});
});
</script>
I would use dataType: json much easier that way to get data from controller
You used data: "register_first_name="+first_name, it's not correct. Correction is data: {register_first_name:first_name},
base_url like this var base_url = <?php echo base_url(); ?>
So, Bellow final code :
<script type="text/javascript">
jQuery(document).ready(function ($) {
var base_url = <?php echo base_url(); ?>
$.ajax({
url: base_url+"register/registration_val", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: {register_first_name:first_name}, // Data sent to server, a set of key/value pairs representing form fields and values
contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
}).done(function (data) {
$('#inferiz').html(data);
}).fail(function (data) {
console.log('failed');
});
}(jQuery));
</script>
Please verify your view part that whether you provided id same as in ajax function.
view part:
<form id="form-reg">
<input name="firstname" id="firstname" type="text" required placeholder="Enter firstname " >
<span id="name_validation" class="text-danger"></span>
<button name="submit" id="submit_button" onClick="myFunction();" >submit</button>
</form>
Then correct the base url path which has to be given inside php tag.
function myFunction() {
$.ajax({
url: "<?php echo base_url();?>register/registration_val",
type: "POST",
data:'firstname='+$("#firstname").val(),
success: function(msg)
{
alert('done..!');
}
});
}

Populate data from mysql to ListView (JQuery Mobile) using ajax

<div data-role="main" class="ui-content">
<ul data-role="listview" id="responsecontainer" >
</ul>
This is my html file.
var output = $('#responsecontainer');
$.ajax({
url: 'http://192.168.1.28/mobile/db_select.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
$.each(data, function(i,item){
var landmark = '<li><a href="#"><h2>'+item.tr_issue+'</h2>'
+ '<p>'+item.tr_assign+'</p></a></li>';
output.html(landmark);
console.log(output);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
This is my JavaScript.
I want to populate my listview from my database(mysql) but the problem is when it generates the theme of jquery mobile di not work.
its like a simple unordered list. thanks in advance
Add $('ul').listview('refresh'); after appending the HTML
Show change your success callback as below
success: function(data, status){
$.each(data, function(i,item){
var landmark = '<li><a href="#"><h2>'+item.tr_issue+'</h2>'
+ '<p>'+item.tr_assign+'</p></a></li>';
output.html(landmark);
console.log(output);
});
$('ul').listview('refresh');
},

how to codeigniter ajax get data?

HTML
<div id="tabs">
<ul id="category">
<li><a href='#' class='cate' id='3'>3</a></li></br>
</ul>
</div>
Jquery
$(function() {
$(".cate").click(function()
{
var user_id = $(this).attr("id");
$.ajax({
type: "GET",
url: "<?php echo base_url('workplace/'.$username.'/menu') ?>",
data: user_id,
success: function(result){
$("#category-details").html(result);
}
});
});});
but in url is : localhost/project/workplace/test/menu?3
how to make : localhost/project/workplace/test/menu/3
Try replace
url: "<?php echo base_url('workplace/'.$username.'/menu') ?>",
this
url: "<?php echo base_url('workplace/'.$username.'/menu/') ?>" + user_id,
but it isn't best solution.
If you want to remove the ?3 from the URL, use a POST instead of a GET:
jQuery.ajax({
type: "POST",
url: "<?php echo base_url('workplace/'.$username.'/menu') ?>",
data: user_id,
error : function(result){
// Do something here to see what the problem is
// maybe console.log(result);
},
success : function(result){
$("#category-details").html(result);
}
});
This will make the URL localhost/project/workplace/test/menu/

AJAX POST from a href class

I have a problem with my script. Why this AJAX don't do anything..
Thanks For help..
In body tag
<div class="share_playlist">
<a href="#" data-toggle="tooltip" title="add to playlist" class="plyshr" id="<?php echo $tracks['track_id']; ?>">
<img src="assets/img/ico/share_icon.png" width="28">
</a>
</div>
and AJAX
$(document).ready(function(){
$(".plyshr").click(function() {
var id = $(this).attr('id');
var dataString = 'id='+ id ;
var parent = $(this);
//alert (data);
$.ajax({
type: "POST",
url: "playlist.php",
success: function(html)
data: dataString,
cache: false,
success: function(html)
}).done(function( msg ) {
parent.html(html);
});
});
});
some more details
And in playlist.php
include 'connect.php';
session_start();
$ip=$_SESSION['id'];
if ($_POST['id'])
{
$id=$_POST['id'];
$ip_sql="insert into playlist (id_user, track_id) values ('$ip','$id')";
$list = mysql_query($ip_sql);
if(isset ($list)){
echo ("succes");
}
else
{
echo("failed");
}
}
you are having a syntax error in your javascript
success: function(html){
data: dataString,
there should be a brace

Resources