In the View File
<script type="text/javascript">
function fetch_comp(val){
$.ajax({
type:"POST",
url:"<?php echo base_url()?>admin/admin_main/fetchData",
data:{'c_name':val},
datatype: "JSON",
success: function(data){
var obj = JSON.parse(data);
$("#job_description").val(obj.job_description);
$("#ctc").val(obj.ctc);
$("#location").val(obj.location);
}
})
}
</script>
In the Controller File
public function fetchData(){
$val = $this->input->post('c_name');
echo "<script>alert('$val');</script>";
}
The value is not being passed from the view page to controller page.
Related
I am new in Codeigniter
I want to access base URL ID into AJAX, within I also pass another id to the controller in Codeigniter. At the controller side, I access two different id one site_url id and another s_id.
So how can I achieve this task?
Site URL
https://www.demoexample.com=?uid=10
Ajax script
<script>
$(document).ready(function(){
$('#member_type').on('change',function(){
var memId = $(this).val();
if(memId!=''){
$.ajax({
url: "<?php echo site_url('index.php/template/vehicle/get_member_type')?>",
method:'POST',
data:'member_id='+memId, // here I want to access site_url id and s_id from drop-down id
success:function(data)
{
$('#mid').html(data);
}
});
}
else{
$('#mid').html('<option value="">Select sttxt_Standard first</option>');
}
});
});
</script>
Controller .php file
function show_vehicle_reg(){
$id=$this->input->post('id'); // access multiple id over here from ajax
}
}
Thanks in advance
ajax:
GET Method
$.ajax({
beforeSend: function () {
},
complete: function () {
},
type: "GET",
url: "<?php echo site_url('controller/cmethod/'.$s_id); ?>",
success: function (data) {
}
});
POST Method
$.ajax({
beforeSend: function () {
},
complete: function () {
},
type: "POST",
url: "<?php echo site_url('controller/cmethod'); ?>",
data: ({member_id: memId}),
success: function (data) {
}
});
I have a script which allows user to like(+1) a record. Everything is working, but I don't know how to show a new value from DB after button is pressed (without refresh). I'm using codeigniter and I'm not really know a lot about jquery.
My script:
<script type="text/javascript">
var base_url = '<?php echo base_url();?>';
jQuery(document).ready(function(){
$('.like').click(function() {
var id = $(this).attr("uid");
$.ajax({
url: base_url+'quotes/quotes_rating',
type: 'GET',
data: {'id': id}
});
});
});
</script>
My controller:
public function quotes_rating() {
$id = $this->input->get('id');
$this->quotes_model->write_likes($id);
}
Model function write_likes() saves records into DB.
This is my model function which shows how many likes has a record:
public function all_likes($id)
{
$sql = "select COALESCE(sum(like),0) as likes from rating WHERE record_id = '{$id}'";
$likes = $this->db->query($sql)->row_array();
return $allikes['likes'];
}
What I have to change in this code?
When user will click .like button, he shoud see a new value from database. Thank you!!!
jQuery(document).ready(function(){
$('.like').click(function() {
var $this = $(this);
$.ajax({
url: base_url+'quotes/quotes_rating',
type: 'GET',
data: {'id': id},
dataType: 'html',
success: function(d){
$this.html(d); //will set html of clicked link
}, error: function(){ //code for error }
});
});
<script type="text/javascript">
var base_url = '<?php echo base_url();?>';
jQuery(document).ready(function(){
$('.like').click(function() {
var id = $(this).attr("uid");
$.ajax({
url: base_url+'quotes/quotes_rating',
type: 'GET',
data: {'id': id}
success: function(data) {
$("someelementyouwanttoseelikesin").html(data);
//data will contain anything you echo in your php script!
}
});
});
});
</script>
i have a div that shows the total sum of some products:
<div class="total-price"><?php echo (!empty($cart)) ? $cart['total'] : '0'; ?> $</div>
with ajax, i'm adding products to cart ... se the page is not reloading.
How to refresh the div after I add the product to cart?
The ajax that i'm using:
<script>
$('#order-to-cart').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '/tdt/order',
data: $(this).serialize(),
success: function () {
$(".success-message").slideDown().delay(5000).slideUp();
$(".total-price").something...;
}
});
})
</script>
Thank you!
You can do something like this:
<script>
$('#order-to-cart').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '/tdt/order',
data: $(this).serialize(),
success: function () {
$(".success-message").slideDown().delay(5000).slideUp();
var oldPrice = $('.total-price').text() * 1;
var itemPrice = "15"; //the price that should be added
$('.total-price').text(oldPrice + itemPrice);
}
});
})
</script>
You should be returning a total basket value from your /tdt/order path.
In the PHP script you should echo some JSON data with all the required information, for example
echo json_encode(array("totalPrice" => "£10.01"));
Then you need to parse this information into your Javascript and update the pages elements;
<script>
$('#order-to-cart').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '/tdt/order',
dataType: 'json',
data: $(this).serialize(),
success: function (data) {
$(".success-message").slideDown().delay(5000).slideUp();
$('.total-price').val(data.totalPrice);
}
});
})
</script>
The above ajax request will expect the data returned to be JSON, you will then use this to update the total-price element.
You can use something like angularjs or knockoutjs - for angular you would update your model - for knockout you would use the self.object.push(value) i.e.,
function OrderViewModel() {
var self = this;
self.myOrder = ko.observableArray([]);
self.addOrderItem = function () {
$.ajax({
type: "post",
url: "yourURL",
data: $("#YOURFORMFIELDID").serialize(),
dataType: "json",
success: function (value) {
self.myOrder.push(value);
},
headers: {
'RequestVerificationToken': '#TokenHeaderValue()'
}
});
}
}
ko.applyBindings(new orderViewModel());
</script>
</pre>
Let's say I have this script for example:
<script type="text/javascript">
function getdata() {
var referenceNumber = $("#reference_number").val();
$.ajax({
url: 'getlistofsomething.php',
type: 'POST',
dataType: 'json',
data: 'referenceNumber=' + referenceNumber,
success: function (output_string) {
$("#name").val(output_string[0]);
$("#address").val(output_string[1]);
}
});
}
</script>
Is there a way I can pass the value in $("#address").val(output_string[1]); into the PHP file this script above is in?
Thanks.
The easiest way is just to send it to the server using GET. Only do this if the data isn't very sensitive...
<script type="text/javascript">
function getdata()
{
var referenceNumber = $("#reference_number").val();
$.ajax({
url: 'getlistofsomething.php?address=' + $("#address").val(output_string[1]),
type: 'POST',
dataType: 'json',
data: 'referenceNumber='+referenceNumber,
success: function(output_string)
{
$("#name").val(output_string[0]);
$("#address").val(output_string[1]);
}
});
}
</script>
This can then be accessed in your php script with:
$address = $_GET['address'];
Let's say .. I have the following script in my controller:
$result= array("Name"=>Charlie Sheen, "Age"=>30);
echo josn_encode($result);
And in my view file I have the following java script. Now could you please tell me what exactly to write in the success function of the script below in order to get the "Name" and "Age" displayed in two different divs inside body tag? I have found some tutorials on this but all those are very confusing to me. Could you please kindly help?
Thanks in Advance :)
<script type="text/javascript">
$(function(){ // added
$('a.read').click(function(){
var a_href = $(this).attr('href');
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>batch/get_info",
data: "id="+a_href,
success: function(server_response){
//What exactly to write here
}
}); //$.ajax ends here
return false
});//.click function ends here
}); // function ends here
</script>
Make sure you spell you json_encode function correctly.
Your html should look like this:
<head>
<script type="text/javascript">
$(function(){ // added
$('a.read').click(function(){
var a_href = $(this).attr('href');
$.ajax({
dataType : 'json', //You need to declare data type
type: "POST",
url: "<?php echo base_url(); ?>batch/get_info",
data: "id="+a_href,
success: function(server_response){
$( '#name' ).text( server_response.Name );
$( '#age' ).text( server_response.Age);
}
}); //$.ajax ends here
return false
});//.click function ends here
}); // function ends here
</script>
</head>
<body>
<div id='name'></div>
<div id='age'></div>
</body>
First of all, you should set the dataType. It should have the value 'json'. As for accessing it, you just access it as you would any other JavaScript object:
<script type="text/javascript">
$(function() {
$('a.read').click(function() {
var a_href = $(this).attr('href');
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>batch/get_info",
data: "id=" + a_href,
dataType: 'json',
success: function(result) {
alert(result.Name); // Charlie Sheen
}
});
return false;
});
});
</script>