javascript:post data to same page - javascript

$(document).ready(function(){
$('#list li').click(function(){
var ID= $(this).attr('id');
$.post("index.php", ID,
function(){
$("#category").html(ID);
}
);
});
});
This code shows my selected list ID in html <div id="category">.
Question
Is there any way to post that data directly to PHP code and echo it? I need data in php, because I want to use it in my query.

$.ajax({
type: "POST",
url: 'index.php',
data: { id: ID }, // name of the post variable ($_POST['id'])
success: function(data) {
console.log('successfully posted data! response body: ' + data);
}
});

Maybe you can use ajax:
$('#list li').click(function(){
var ID = $(this).attr('id');
$.ajax(
url:"index.php", //Page with data
data:{id: ID},
type: "POST", //You can use any method POST, GET, DELETE, PUT...
success:function(result){
$("#category").html(result);
});
});

You aren't sending a key/value pair ... only a value.
You also have not added the argument for the response in the completion callback
Change
$.post("index.php", ID, function(){
$("#category").html(ID);
});
To
$.post("index.php", {id: ID}, function(response){ //object used for key/value pairs
$("#category").html(response.data);
},'json')
.fail(function(error){
alert("Ooops ..something went wrong");
});
Then for simple php test
echo json_encode(array('data'=>'The ID sent was '. $_POST['id']));

Related

Ajax with JSON GET request undefined

I am trying to get the front end to display the contents of my dynamo db table although it displays the information as undefined.
$(document).ready(function(){
$.ajax({
type: 'GET',
url:someURL,
success: function(data){
$('#incidentid').html('');
data.forEach(function(IncidentNotesItem){
$('#incidentid').append ('<p>'+ IncidentNotesItem.incidentid + '</p>');
})
}
});
});
I solved it! The issue was that I wasn't calling the column name properly. I did console.log(IncidentNotesItem) to see what is stored inside the IncidentNotesItem Function and saw on the console that it is called id instead of incidentid.
$(document).ready(function(){
$.ajax({
type: 'GET',
url:someURL,
success: function(data){
$('#incidentid').html('');
data.forEach(function(IncidentNotesItem){
$('#incidentid').append ('<p>'+ IncidentNotesItem.id + '</p>');
})
}
});
});

pass data($post) to php file using javascript without callback

I need to pass data from HTML page to PHP page But without data callback ....
i'm used two method but One of them did not succeed
1)
$.ajax({
type: "POST",
url: 'phpexample.php',
data: {voteid: x },
success: function(data)
{
alert("success! X:" + data);
}
});
2)
$.post("getClassStudent.php",
{
},
function(data){
$("#div_id.php").html(data);
}
);
as i can understand, you just want to send info to a php script and don't need the response, is that right?
try this
$.post("phpexample.php", {voteid:x});
or simply remove the "succes" function from the equation if you feel more confortable using $.ajax instead of $.post
$.ajax({
type: "POST",
url: 'phpexample.php',
data: {voteid: x }
});
your fisrt example is correct, the second is not well formed.
more info:
http://api.jquery.com/jquery.post/
EDIT: to help you some more :)
<button type="button" id="element-id">click</button>
<button type="button" class="class-name">Click</button>
$(document).ready(function(){
//if you are marking ans element by class use '.class-name'
$(".class-name").click(function(){
$.post("getClassStudent.php");
});
//if marking by id element use '#id-name'
$("#element-id").click(function(){
$.post("getClassStudent.php");
});
});
be carefful with the markings, for debuggin try to use "console.log()" or "alert()" so you can see where is the problem and where the code crushes.
var formData = {
'voteid' : 'x',
};
$.ajax({
type : 'POST',
url : 'phpexample.php',
data : formData, // our data object
dataType : 'json',
encode : true
}).done(function(data) {
console.log(data);
});

Send id with ajax and jquery

Today I have a problem sending id with ajax and jquery ,I have a foreach in the view of laravel like this ,
<div class="ident" id="auctionIdentifier-{{$i}}" auction="{{$auction->id}}"></div>
this in the elements recieve correctly the id of auctions but when I try to doing a click I only send the first id.
I don't know when I doing a click in the next bid , I don't send the id corresponding.
id}}">
$(".inscription").click(function(){
console.log($(".ident").attr('auction'));
$.ajax({
url:'../bid/inscription',
dataType:'json',
type:'get',
cache:true,
success: function (response) {
console.log("ok");
},
});
});
Maybe this helps:
$(".ident").click(function(){
var id = $(this).attr('auction');
$.ajax({
url:'../bid/inscription',
dataType:'json',
type:'get',
cache:true,
data: {
id: id
},
success: function (response) {
console.log("ok");
},
});
});
Basically you need to get the currently clicked element auction attribute. Also you might want to add data- in front of it like so: data-auction="VALUE". This way you can get it with $(el).data('auction').

pass data to lable in same page using jquery

i have this working code with this code i can pass data to div within same page
but insted of passing data to div i want to pass this to label or textbox so that i can post it to the server. i am new in ajax,jquery . please suggest me best answer
<script type="text/javascript">
//send clicker data to div
$(function() {
$(".clicker").mouseover(function(){
var data = $(this).attr('id');
$.ajax({
type: "POST",
data: "db_data=" + data,
success: function(){
//alert(data);
$('.responseDiv').text(data);
}
});
});
});
</script>
<script type="text/javascript">
i think i need to change this line only
$('.responseDiv').text(data);
but dont know how to do that. didnt find any solution on net also
take any name like propertyId
Let's say the text field has id="propertyID"
Use the val() method to assign this new value to your text field.
$('#proertyID').val(data);
Like:
$(function() {
$(".clicker").mouseover(function(){
var data = $(this).attr('id');
$.ajax({
type: "POST",
data: "db_data=" + data,
success: function(){
//alert(data);
// $('.responseDiv').text(data);
$('#proertyID').val(data);
}
});
});
});
In the below line change selector .responseDiv with id/name or class of input/label $('.responseDiv').val(data);
<script type="text/javascript"> //send clicker data to div
$(function() {
$(".clicker").mouseover(function(){
var data = $(this).attr('id');
$.ajax({
type: "POST",
data: "db_data=" + data,
success: function(data2){
//alert(data);
$('.responseDiv').text(data2);
}
});
});
});
success return in data2 and use this..

jquery json post not working

Well, im trying to post a variable in jquery to my controller. But it seems that the posting is not successful. I am not getting any value when i try to retrieve it in my controller. It says undefined index. Here's what I have:
my jquery:
$(document).ready(function(){
$('.buttons').click(function(){
var data = $(this).attr("value");
// var test = 'test';
jQuery.ajax({
url:'<?php echo $this->Html->url(array('controller'=>'maps','action'=>'instantiateButtonValue'));?>',
type: 'POST',
async: false,
data: data,
dataType: 'json'
// success:function(data){
// alert(data);
// },
// error:function(data){
// alert(data);
// }
});
});
});
my controller:
function instantiateButtonValue(){
echo $_POST['data'];
// $this->set('data','some');
// $this->render('json');
}
I think you should enclose with " quotes instead of ' quotes in URL.
From PHP you should encode as JSON instead of direct echo, to retrieve the value by JQuery.
like below
echo json_encode($_POST['data']);
i got an idea from this link here
$(document).ready(function(){
$('.buttons').click(function(){
var data = $(this).attr("value");
// var test = 'test';
jQuery.ajax({
url:"<?php echo $this->Html->url(array('controller'=>'maps','action'=>'instantiateButtonValue'));?>",
type: 'POST',
async: false,
data: {data:data},
dataType: 'json'
});
});
});

Categories

Resources