how to codeigniter ajax get data? - codeigniter

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/

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..!');
}
});
}

codeigniter sound notification system

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();
}
});

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

Ajax retrieve data from success function

I am submitting a form via Ajax, and I want to prepend the project name to a list item when the form is submitted. Everything works fine except for pulling the information needed inside the success function. Data[Project][project_name] is being posted. How do I get the project name inside the success function? Right now "data" is being displayed in the list. I only have the one textbox in my form.
<script type="text/javascript">
$(document).ready(function () {
var frm = $('#ProjectAddForm');
frm.submit(function() {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function(data) {
$('#ProjectProjectName').val('');
$('#ProjectList').prepend("<li style='color: green;'>data</li>");
}
})
});
return false;
});
</script>
HTML:
<ul id="ProjectList">
<?php foreach ($projects as $project): ?>
<li><?php echo $project['Project']['project_name']; ?></li>
<?php endforeach; ?>
<?php unset($project); ?>
</ul>
<form accept-charset="utf-8" method="post" onsubmit="event.returnValue = false; return false;" id="ProjectAddForm" action="/callLog/projects/add">
I needed to add a dataType: 'json' to the request.
<script type="text/javascript">
$(document).ready(function () {
var frm = $('#ProjectAddForm');
frm.submit(function() {
$.ajax({
type: frm.attr('method'),
url: "<?php echo $this->Html->Url(array('controller' => 'projects', 'action' => 'add.json')); ?>",
data: frm.serialize(),
dataType: 'json',
success: function(data) {
$('#ProjectProjectName').val('');
$('#ProjectList').prepend("<li class='icon-remove', style='color: green;'>" + data.projectName + "</li>");
$('#modalProject').modal('hide');
}
})
});
return false;
});
</script>

Resources