How do I alert the variable using url segment with ajax? - javascript

I am using HVC codeigniter.
I wanted to alert the value of variable.
Here's the code on my view.
$("#user_table tr").click(function(){
$userid = $(this).closest('tr').children('td:first').text();
$.ajax ({
type: "POST",
url: "manageUser_controller/log_history/$userid",
success: function(data){
alert(data);
}
});
});
Here's the code on my controller.
function log_history($userid) {
echo $userid;
}

You need to edit your code as follows
var userid = $(this).closest('tr').children('td:first').text();
url: "manageUser_controller/log_history/'+userid,

try to send as data like this
$("#user_table tr").click(function(){
var userid = $(this).closest('tr').children('td:first').text();
$.ajax ({
type: "POST",
url: "manageUser_controller/log_history",
data:{userid : userid},
success: function(data){
alert(data);
}
});
});
and get it on controller using input post like this
function log_history()
{
$user_id = $this->input->post('userid',true);
echo $userid;
}
it's also be filter by true.

Related

Handle with ajax request after click link

i have link in my page
+1111
i want to save every click in database
i made this ajax code :
<script type="text/javascript">
$('#tel').on('click', function() {
var location = $(this).attr('href');
var action = 'script.php';
$.ajax({
method: 'POST',
url: action,
data: '',
dataType: 'json',
success: function (data) {
window.location = location;
},
error: function(data){
return false;
}
});
});
</script>
now what must i do, what is the code i have to put in script.php
First you need send some data to script.php, for example
url: action,
data: {location: location},
dataType: 'json',
next in script.php you can read this data
$_POST['location']
create sql query and save data in DB.
send href as json
<script type="text/javascript">
$('#tel').on('click', function() {
var location = $(this).attr('href');
var action = 'script.php';
$.ajax({
method: 'POST',
url: action,
data: {"href":location}, // this line update
dataType: 'json',
success: function (data) {
console.log(data['state]); // this line add for debug server side
window.location = location;
},
error: function(data){
return false;
}
});
});
</script>
and update script.php like below:
<?php
if($_POST['href']){
//connect to database
//update table
echo json_encode(array('state'=>'ok'));
}else{
echo json_encode(array('state'=>'error'));
} ?>

Getting multimple results from Ajax post

Maybe this is a duplicate but I can not understand why mo code does not work. I am trying to get multiple results via Ajax/php.
This is from my php file:
$result11 = 'test1'
$result22 = 'test2';
echo json_encode(array("data1" => $result11, "data2" => $result22));
Ajax call:
$(document.body).on('submit','#sendmessage',function() {
$.ajax({
type: "POST",
url: "/send.php",
data: {par:par,kid:kid,ha:ha,sform:sform,editors:editors},
cache: false,
dataType:'json',
success: function(datax) {
alert(datax.data1);
}
});
return false;
});
Problem:
When I submit a form, the page refreshes instead of sending ajax request.
At the same time this works but I can't get multiple results from Php file:
$(document.body).on('submit','#sendmessagex',function() {
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "/send.php",
data:str,
success: function(data) {
alert(data);
}
});
return false;
});
Add a preventDefault() call to your script
$(document.body).on('submit','#sendmessagex',function(event) {
//----------------------------------------------------^^^^^
event.preventDefault();
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "/send.php",
data:str,
success: function(data) {
alert(data);
}
});
return false;
});
You have to use preventDefault() ofcourse to prevent page refresh. Then you can use the second code without datatype json. But in that case at first you have to parse the json like this way:
success: function(data){
var datax = JSON.parse(data);
//now you have object and can access like this: datax.data1, datax.data2
}
Incase you want to use first code, in php you have to set php header to define it as proper json output.
header('Content-Type: application/json');

How to call the php function using jquery ajax

I am using the following script
<script>
$(document).ready(function(){
$("#view").click(function(){
var requestId = $("#hdnRequestId").val();
$.ajax({
type: "POST",
url: "enquiryProcess.php",
data: requestId,
cache: false,
success: function(data){
console.log(data);
}
});
return false;
});
});
My controller function is
<?php
include('enquiry_function.php');
$functionObj=new Enquiry();
if(isset($_POST['requestId']))
{
$qt_request_id=$_POST['requestId'];
$responce=$functionObj->view_enquiry_request($qt_request_id);
echo json_encode($responce);
}
?>
And my model function is
class Enquiry
{
public function view_enquiry_request($qt_request_id)
{
$query=mysql_query("SELECT * FROM quote_request WHERE qt_request_id='$qt_request_id'");
$result=mysql_fetch_assoc($query);
return $result;
}
}
I did not get any error.But result in console message is empty.How to get the result from php in jquery ajax.please help me.
Please change
var requestId = $("#hdnRequestId").val();
$.ajax({
type: "POST"
, url: "enquiryProcess.php"
, data: {"requestId":requestId}
, cache: false
, success: function (data) {
console.log(data);
}
});
Pass data as PlainObject or String or Array. See jQuery documentation here http://api.jquery.com/jquery.ajax/

Unable to get the return from php in ajax request

I just want to get back some return value from the ajax data post. I am not sure why I am not getting something back in success. Please review the code and tell me where I am wrong
My jquery code
$("#btnlogin").click(function(){
var email = $("#emaillog").val();
var password = $("#passlog").val();
console.log('test');
/* $.ajax({
url: 'home2/login_user',
//data: {'title': title}, change this to send js object
type: "post",
data: 'email=' + email+'&password='+password,
success: function(output) {
url:"home2/login_user",
data: 'email=' + email+'&password='+password,
alert(output);
}
});*/
$.ajax ({
url:"home2/login_user",
type: "post",
dataType: 'json',
data: 'email=' + email+'&password='+password,
success: function (data) {
$.each(data, function(key, value) {
console.log(key,'--',value);
});
//iterate here the object
}
});
});
My php code
public function Login_user()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$data['result'] = $this->Home_model->login_to_user($email,$password);
echo json_encode ($data['result']);
}
In php code I echo the result but in in jquery. I am not getting any result in success
Thanks
use parseJSON
var obj = jQuery.parseJSON(data);
console.log(obj.key);
The reason is because your backend code does not seem to be able to find the username and password parameters. You're passing them the wrong way at this line of code:
data: 'email=' + email+'&password='+password,
Replace the string with a JavaScript object:
data: { email: email, password: password }
well, first, you are passing the parameters like it was a get request, and its not.
Change the way you passing to something like this.
$.ajax ({
url:"home2/login_user",
type: "post",
dataType: 'json',
data: { field1: "hello", field2 : "hello2"},
success: function (data) {
$.each(data, function(key, value) {
console.log(key,'--',value);
});
//iterate here the object
}
});

How to call a function and load part of view using ajax?

I am trying to hide a div and load another div by calling a function in ajax, but it's not redirecting me to that function.I just want to load a div without refreshing the page.
<script type="text/javascript">
function checkVoucher(){
var voucher_number = $("#voucher_number").val();
$.ajax({
type: "POST",
url: "<?php echo base_url('cart/gift_validate'); ?>",
data: {gift_voucher_number:voucher_number} ,
success: function(data){
$('#gift_msg').html('<span id="gift_msg" style="display:block;">'+data+'</span>');
$('.order_details').html('').hide();
$('.cart_table').html('');
$('.cart_table').load('cart/ajax_cart_view');
return false;
},
});
}
</script>
Controller:
public function ajax_cart_view()
{
$gift_data = $this->cart_model->getgiftvoucher();
if($gift_data){
$this->data['giftDiscount'] = $giftData['amount'];
$this->data['giftID'] = $giftData['id'];
}
$this->load->view('cart/ajax_cart',$this->data);
}
View :(Contains div which needs to be loaded on the same page)
<div class="order_details_ajax">
.
.
</div>
It looks like the success method is deprecated.
http://api.jquery.com/jquery.ajax/
Pass it a .done() after you close your parentheses:
$.ajax({
type: "POST",
url: "<?php echo base_url('cart/gift_validate'); ?>",
data: {gift_voucher_number:voucher_number} ,
}).done(
function(data){
$('#gift_msg').html('<span id="gift_msg" style="display:block;">'+data+'</span>');
$('.order_details').html('').hide();
$('.cart_table').html('');
$('.cart_table').load('cart/ajax_cart_view');
return false;
})
Change your ajax call with following, because there is mistake in your success function :-
$.ajax({
type: "POST",
url: "<?php echo base_url('cart/gift_validate'); ?>",
data: {gift_voucher_number:voucher_number} ,
success: function(data){
$('.order_details_ajax').html('<span id="gift_msg" style="display:block;">'+data+'</span>');
$('.order_details').html('').hide();
$('.cart_table').html('');
$('.cart_table').load('cart/ajax_cart_view');
return false;
},
});
}

Categories

Resources