I am trying to post via link. But I think there is a problem. I am not good at Javascript.
I want to send attribute and show with div.
Here is my Code :
<script type="text/javascript">
$(document).ready(function() {
$("#slidingProduct").click(function() {
var aa = $(this).attr('urun_id');
$.ajax({
url: "data.php",
dataType: "POST",
data: {
"number1": aa
},
success: function(json) {
$("#result").html(json.number1);
}
});
});
});
</script>
A
B
O
<div id="result"></div>
You can do something like this:
$(".slpd").on('click',function(){
var aa = $(this).data('urun_id');
var json={"number1":aa};
$.ajax({
url: "data.php",
type: "POST",
dataType:'JSON',
data: JSON.stringify(json),
success: function(result){
$("#result").html(result.number1);
}
});
});
As ids in DOM should be unique, you can specify similar class name to capture click event in a single go and
some modifications in html - It's good to use data-* property of html5:
A
B
O
$(document).ready(function() {
$("a").on('click', function(e) {
e.preventDefault(); // Stop redirection
var aa = $(this).attr('urun_id');
$.ajax({
url: "data.php",
type: "POST"
dataType: "JSON",
data: {
"number1": aa
},
success: function(response) {
$("#result").html(response.number1); // This depends on how you've sent response from server
}
});
});
});
Related
I am going to print the response data from test.php in JSON format to print it on particular field
$.ajax({
type: 'POST',
url: 'test.php',
data: data,
success: function(response) {
var result = $.parseJSON(response);
$(document).ready(function(){
$("#test").click(function(){
$("#bemail").val(result.email);//when i prints only result than it displays [object object]
});
});
}
});
Try it like this . You have to put your ajax inside $(document).ready
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'test.php',
data: data,
success: function(response) {
var result = JSON.parse(response);
$("#bemail").val(result.email);
}
});
});
you are calling document.ready() inside the AJAX success handler which doesn't get invoked since AJAX call doesn't invoke the document loading again, DOM has already loaded and it loads only once in the life cycle of a page session.
This much should do
$.ajax({
type: 'POST',
url: 'test.php',
data: data,
success: function(response) {
var result = JSON.parse(response);
$("#bemail").val(result[0].email); //after you explained the JSON response
}
});
Your code is totally wrong, it should be
function displayEmail() {
$.ajax({
type: 'POST',
url: 'test.php',
data: data,
success: function(response) {
var result = $.parseJSON(response);
//Just Print the Result in Console using console.log(result)
$("#bemail").val(result.email);
}
});
}
$(document).ready(function() {
$("#test").click(function() {
displayEmail();
});
});
i want to post an array from java script to php by ajax. But i don't know how do that, especially send it to php function like controller class. Correct me if i'm wrong, this is my java script source, as a function to send an array :
<script>
function send(){
var obj = JSON.stringify(array);
window.location.href = "post.php?q=" + obj;
}
</script>
i was try, but still fail. really need help..
As described in the JQuery API documentation, you can use
var rootPath="http://example.com/"
var jsonData = $.toJSON({ q: array });
var urlWS = rootPath + "post.php";
$.ajax({
url: urlWS,
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: jsonData,
success: function(result) {
// do something here with returned result
}
});
var array= [];
array[0] = 'hi';
array[1] = 'hello';
$.ajax({
url: 'http://something.com/post.php',
data: {array: array},
type: 'POST'
});
try like this,
var data_to_send = $.serialize(array);
$.ajax({
type: "POST",
url: 'post.php',
data: data_to_send,
success: function(msg){
}
});
or
you can pass as json like below,
$.ajax({
type: "POST",
url: 'post.php',
dataType: "json",
data: {result:JSON.stringify(array)},
success: function(msg){
}
});
var arr = <?php echo json_encode($postdata); ?>;
ajax: {
url:"post.php"
type: "POST",
data: {dataarr: arr},
complete: function (jqXHR, textStatus) {
}
You can try this .this will work
example
ajax code:
$.ajax({
url: 'save.php',
data: {data: yourdata},
type: 'POST',
dataType: 'json', // you will get return json data
success:function(result){
// to do result from php file
}
});
PHP Code:
$data['something'] = "value";
echo json_encode($data);
I'm using Jquery V1.11.1 and I want to replace a cell in a HTML table after a Jquery AJAX request. I want the result in the cell.
$(".permission").click(function(e) {
var acoPath = $(this).siblings('th').text();
var rowsLeft = $(this).prevUntil("th").length;
var aroGroup = $('.aro').eq(rowsLeft).text();
$.ajax({
url: "/permissions/update",
type: "POST",
data: { aco : acoPath, aro : aroGroup },
cache: false,
dataType: "HTML",
success: function (html) {
$(this).html(html);
}
});
});
});
When I click on the text in the desired cell, nothing is replaced. How can i fix this?
You need to set the context in ajax call to current object. You can use context option from ajax to do that:
context: this,
Modified ajax call:
$.ajax({
url: "/permissions/update",
type: "POST",
context: this,
data: { aco : acoPath, aro : aroGroup },
cache: false,
dataType: "HTML",
success: function (html) {
$(this).html(html);
}
});
Your context has changed. in the 'click' function this referce to the button. But in the callback function the context has changed. You should save the context to a variable first (var self).
$(".permission").click(function(e) {
var acoPath = $(this).siblings('th').text();
var rowsLeft = $(this).prevUntil("th").length;
var aroGroup = $('.aro').eq(rowsLeft).text();
var self = this;
$.ajax({
url: "/permissions/update",
type: "POST",
data: { aco : acoPath, aro : aroGroup },
cache: false,
dataType: "HTML",
success: function (html) {
self.html(html);
}
});
});
});
$("#delete").click(function() {
deleterecord();
});
function deleterecord(){
var id = $("#iduser").val();
alert("aw"+id);
var id = $('#iduser').attr();
e.preventDefault();
pressed = "delete"
$.ajax({
type: "post",
url: "IngSave.php",
data: "&idshit="+ id,
data: "&gagawin="+ pressed,
success: function(){
alert("ATTENTION");
$("#usertbl").html(data);
}
});
return false;
}
I'm not getting the variables $_POST['idshit']; and $_POST['gagawin']; in IngSave.php file.
Try this:
$.ajax({
type: "post",
url: "IngSave.php",
data: {idshit:id,gagawin:pressed},
success: function(){
alert("bitch");
$("#usertbl").html(data);
}
});
You have two data params incorrectly in your ajax call, change it to
$.ajax({
type: "post",
url: "IngSave.php",
data: "idshit="+id+"&gagawin="+pressed, // or - { idshit:id,gagawin:pressed }
success: function(){
alert("bitch");
$("#usertbl").html(data);
}
});
I have an ajax call and i need to show one particular div based on the response from the ajax call. here is my ajax call
var cmnumber = document.forms['myform']['cm'].value;
alert(cmnumber)
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber,
success: function(data) {
},
error: function(data) {
}
})
});
I need to show on div if the response is success. Default the div is Hidden.
<div class="downtime" id="downtime" style="display: none" >
--------------
</div>
Any help wil be appreciated..
try this
var cmnumber = document.forms['myform']['cm'].value;
alert(cmnumber)
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber,
success: function(data) {
if(data == "success")
$('#downtime').show();
else
$('#downtime').hide();
},
error: function(data) {
}
})
I think you can use
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber
}).done(function(data){
$('#downtime').show();
})
Add the data into the div and show it.
success: function(data) {
$('#downtime').html(data).show();
},
error: function(data) {
$('#downtime').html().hide();
}
document.getElementById("downtime").style.display = "block";
See https://developer.mozilla.org/en-US/docs/CSS/display
get the value of text box and save it in cmnumber. Make sure you include jquery in your html file
function makeAjaxCall(){
var cmnumber=$("#cm").val();
var url="/validatecm/"+cmnumber;
$.ajax({url:url,success:function(result){
handleResult(result);
}});
}
function handleResult(result){
// if result is what you expect it is then enable the div
if(result=="ok"){
$("#downtime").css('display','block');
}
}
I'm new to AJAX but you have to change one thing to hide/show a the div:
var cmnumber = document.forms['myform']['cm'].value;
alert(cmnumber)
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber,
success: function(data) {
if(data.**MESSAGE** == "success")
$('#downtime').show();
else
$('#downtime').hide();
}, error: function(data) { } })