I have this code in my index.php:
$('#whois').click(function () {
//console.log("button clicked");
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
I want to pass a post value to test.php inside this script, like so:
$('#whois').click(function () {
//console.log("button clicked");
$.ajax({
type: "POST",
url: "test.php",
data: info,
success: function(){
//dont know what to write here
}
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
And then call this post value in test.php $_POST['info']
Maybe someone will understand what im saying.
this would be better
$.post("test.php", { info: "Something", moreinfo: "Something Else" })
.done(function(data) {
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
This should work , try it out:
$('#whois').click(function () {
$.ajax({
type: "POST",
url: "test.php",
data: {info: "some info"},
success: function () {
}
$('#whois_content').html("");
$('#fade').fadeOut('slow', function () {
urlLinkas();
});
});
Related
this is my Code inside a html file:
<script>
$(document).ready(function (event)
{
$(document).on("click", ".interestbutton", function(e)
{
var docID = e.target.parentNode.parentNode.id;
$.ajax({
type: "POST",
url: "interest.php",
data: {docID },
success: function(data) {
console.log("done");
}
});
});
});
</script>
The "console.log" works but nothing inside my php file does. Any ideas?
Best regards
Change data: {docID } for data: {docID: docID} as it is an object.
index.php
<button class="cat" data-table="cat" >Cat</button>
<div class="animals"></div>
script.js:
$(document).on("click", ".cat", function (event) {
alert("cat");
var table = $(this).data('table');
$.ajax({
url: "update.php",
data: {
table: table,
},
type: "POST",
success: function (data) {
$(".animals").html(data);
}
})
});
$(document).on("click", ".dog", function (event) {
alert("dog");
var table = $(this).data('table');
$.ajax({
url: "update.php",
data: {
table: table,
},
type: "POST",
success: function (data) {
$(".animals").html(data);
}
})
});
$(document).on("click", ".bird", function (event) {
alert("bird");
var table = $(this).data('table');
$.ajax({
url: "update.php",
data: {
table: table,
},
type: "POST",
success: function (data) {
$(".animals").html(data);
}
})
});
update.php
if ($table == "cat") {
echo "<button class='dog' data-table='dog'>Dog</button>";
}
if ($table == "dog") {
echo "<button class='dog' data-table='dog'>Dog</button><button class='bird' data-table='bird'>bird</button>";
}
if ($table == "bird") {
echo "nothing";
}
By click on an animal button (for example "bird") I expect the alert box fire only once. But everytime I click on an animal the alert box is firing more and more times.
Try this .. 'Unbind'
$('#updateUser').unbind('click').click(function () {
//do here ..
})
Change your click event. use "one" instead of "on".Refer Jquery Docs It will execute your click event only once. refer below example
$(document).one("click","#btn1",function(){
alert("button btn1 clicked");
});
$(document).on("click","#btn2",function(){
alert("button btn2 clicked");
});
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.0.min.js">
</script>
<button id="btn1">Click me(Executes once)</button>
<button id="btn2">Click me(Executes multiple times)</button>
Try this way.
$('#do-something').click(function(e) {
var me = $(this);
e.preventDefault();
if ( me.data('requestRunning') ) {
return;
}
me.data('requestRunning', true);
$.ajax({
type: "POST",
url: "<url>",
data: $("#form").serialize(),
success: function(msg) {
//stuffs
},
complete: function() {
me.data('requestRunning', false);
}
});
});
Try with .off() before .on(), like this:
$(document).off().on("click", ".dog", function (event) {
alert("dog");
var table = $(this).data('table');
$.ajax({
url: "update.php",
data: {
table: table,
},
type: "POST",
success: function (data) {
$(".animals").html(data);
}
})
});
instead of .on you can use .one method of jquery.
Here is my ajax
$(document).ready(function()
{
$( ".iCheck-helper" ).on( "click", function(){
var value_to_send = $('.i-check:checked').map(function() {
//alert(this.value);
return this.value;
}).get().join(', ');
});
});
Here,its my URL '/hotel/hotelresults/'.folder_name/function_name and the my Controller Name is HotelController
How should I get the ""return this.value"" using ajax to the controller.
Can Someone help me.
Try this:
$.ajax({
type: "POST",
url: "hotel/hotelresults",
data: {
key : value
},
success: function (data) {
alert(data)
}
});
Route:
Route::post('hotel/hotelresults', 'YourController#YourMethod');
In YourController:
public function YourMethod(Request $request)
{
//
return $request->key; //or return Input::get('key');
}
Please read more
docs
Thank you so much #rome 웃
I Tried like this..
$(document).ready(function()
{
$( ".iCheck-helper" ).on( "click", function(){
console.log($('.i-check:checked').map(function() {
// alert(this.value);
var value = this.value;
$.ajax({
// alert();
type: "POST",
url: "hotelresults",
data: {
key : value
},
success: function (data) {
// alert(data);
}
});
}).get().join(', '));
});
});
And in Route:
Route::get('hotel/hotelresults', 'HotelController#postHotelresults');
And in my Controller:
public function postHotelresults(Request $request)
{
//
return $request->key; //or return Input::get('key');
}
Because of giving the URL as "url: "hotel/hotelresults"," It seems to be an error in my console
I am trying to update the user input ratings through ajax call. This one alert(performance_rating) returned the user input ratings properly. But, I have a problem with my url. it won't call user_ratings.php. I don't know why? I have tried alert function in user_ratings.php page. But, it won't alert.
I have the user_ratings.php file in siteurl.com/include/pages/user_ratings.php
How do I call my php file properly?
ajax request
$(function () {
$('#form').on('submit', function (e) {
performance_rating = $('input:radio[name=rating]:checked').val();
e.preventDefault();
$.ajax({
type: 'POST',
url: 'user_ratings.php',
data: {
rating: performance_rating
},
success: function() {
alert(performance_rating);
}
});
});
});
If you are sending your ajax request from localhost to your domain, then you have to use full site url in your ajax call as follows
$(function () {
$('#form').on('submit', function (e) {
performance_rating = $('input:radio[name=rating]:checked').val();
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://domain.com/include/pages/user_ratings.php',
data: {
rating: performance_rating
},
success: function() {
alert(performance_rating);
}
});
});
});
first its better you do this
define("URL", "http://siteurl.com/");
Then in the ajax url section write
url: '<?php echo URL ;?>includes/pages/user_ratings.php',
Put your FQDN (e.g. www.yoururl.com) before the user_ratings.php in your jQuery Ajax call. So change to this:
$(function () {
$('#form').on('submit', function (e) {
performance_rating = $('input:radio[name=rating]:checked').val();
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://www.yoururl.com/user_ratings.php',
data: {
rating: performance_rating
},
success: function() {
alert(performance_rating);
}
});
});
});
Can anyone please point out exactly what I am doing wrong, I am trying to make a php call when a selected value is changed.
The code is not echoing the information from the php file.
JQUERY CODE
// Skill sort on change
$('#order_by').on('change', function() {
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: {skill:this.value}
}).done(function(result){
console.log(result)
})
});
PHP CODE
<?php
session_start();
$skill_sort = $_POST['skill'];
echo $skill_sort;
echo 'I got in here';
?>
Thank you for the help and time!
EDIT: It works correctly now, Thanks for all the help!
Try this:
$('#order_by').on('change', function() {
var sk = $(this).val();
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: 'skill=' + sk,
success: function(result) {
alert(result);
}
});
});
Try this
$('#order_by').on('change', function() {
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: {skill:$(this).val()},
success: function(result){
alert("done");
},
error: function(){
alert("error");
}
});
});
You should use then instead of done http://promises-aplus.github.io/promises-spec/
$('#order_by').on('change', function () {
$.post("sort_skill_be.php", {
skill: $(this).val()
}).then(function (result) {
console.log(result);
}).fail(function () {
console.err('failed to fetch data');
});
});
You could test things out like this...
// Skill sort on change
$('#order_by').on('change', function() {
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: {skill:this.value}
}).done(function(result){
console.log('my results' + results);
})
});