I need the content of the Variable data from this AJAX Request but in a PHP Variable.
Is that possible?
$.ajax({
type: 'POST',
async: false,
url: '/php/checkAccountData.php',
data: 'un=' + PasswordResetUsernameText + '&e=' + PasswordResetEmailText,
success: function(data){
var responseanswer = data;
alert(responseanswer);
setTimeout(function(){
$("img[name=UserNameEmailText]").fadeTo(500,0);
$("div[id=PasswordResetLoadingDiv]").fadeOut(500)}, 1500);
setTimeout(function(){
$("img[name=UserNameEmailComplete]").fadeTo(500,1)}, 2500);
setTimeout(function(){
$('MainFrame').fadeTo(500,1);
$('#LoginField').fadeOut(500, "swing")}, 7000000);
}
Related
I need to send a response from the ajax to the php code. Alert msg as to be displayed when success.. I have to get entered date should be displayed in the url and response as to be sent..
<script>
$(document).ready(function() {
$('#txtdate').change(function(){
date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date ,
success: function() {
alert(data);
}
});
});
});
</script>
Two error:
1:
date = $(this).val();
to
var date = $(this).val();
2:
url: "http://localhost/data/check_date.php?date=" +date ",
to
url: "http://localhost/data/check_date.php?date=" +date ,
3:
success: function() {
to
success: function(data) {
All script:
<script>
$(document).ready(function() {
$('#txtdate').change(function(){
var date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date ,
success: function(data) {
alert(data);
}
});
});
});
</script>
Remove " mentioned at the end of the url because date is variable to be passed through url.
To overcome all mistakes change your whole code to
<script>
$(document).ready(function() {
$('#txtdate').change(function(){
var date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date,
success: function(date) {
alert(date);
}
});
});
});
</script>
var date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date ,
dataType: 'text',
success: function(data) { //add data in the function which is returned from the file
alert(data);
}
});
also try opening your network tab by clicking F12 in your browser before doing ajax request, if there's some error in your php file you can view it here in network tab, see if it helps
In my ajax script below when I do console.log for duration it returns <select name="duration" id="duration"> instead of the value. I think for this reason my function is not working properly. I am not getting any response back from the PHP page and the loading image keeps on loading.
$(document).ready(function() {
$("#submit").click(function() {
var dataString = {
quantity: $("#quantity").val(),
duration: $("select[name='duration']").val()
};
console.log(duration);
$.ajax({
type: "POST",
dataType : "json",
url: "rent-process.php",
data: dataString,
cache: true,
beforeSend: function(){
$("#submit").hide();
$("#loading-rent").show();
},
success: function(json){
$("#loading-rent").hide();
$("#submit").show();
$(".message").html(json.status).fadeIn();
$('#wallet-av').html('$' + json.wallet);
$('#ref-av').html(json.referrals);
}
});
return false;
});
});
why isn't this working?
jQuery AJAX Code:
$("header input").bind("keyup", function()
{
var searchString= $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false
});
});
PHP Code(Just a test Code):
if($_POST["search"]) {
echo "TEST MESSAGE!";
}
It doesn't show The echo :/
thanks for ur help ;)
You need to display the data you receive from the ajax call.
Example, to put the result into a <div> called YourresultDiv:
Try with this
$("header input").on("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
success: function (data) {
$('#YourresultDiv').html(data);
alert("Successful");
}
});
});
Hopes this will help you....
$("header input").bind("keyup", function()
{
var searchString= $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
async: false
},success: function (data) {
$('div#posteddata').append(data);
}
);
});
<html>
<head>
</head>
<body>
<div id="posteddata"></div>
</body>
</html>
You need to specify an element you want to update.. for example
<div id="result"> </div>
and then append a success event handler to your ajax call
$("header input").bind("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false
}).success(function (data) {
$("#result").html(data);
}).fail(function () {
alert("Ajax failed!");
});
});
Try with this
In js add
success: function (data) {
// success handler
}
as your response handler
if($_POST["data"]) {
// search = search string available here in $_POST['data']
echo "TEST MESSAGE!";
}
where is your call back function in $.ajax() function,with callback function only,you can display anything through an ajax request..
So try this.
$("header input").on("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
success: function (data) {
$('#Yourdiv').html(data); // or $('#Yourdiv')text(data);
}
});
});
Without success function,you can see the echoed statement in your network segment in console.
for that, press F12,then you will get a link like
XHR finished loading: POST "http://localhost/yourproject/func_name.Click on that link and you will goto network segment and from there,click on the function name and then in response or preview tab,you canb see the echoed statement..
try it.
i have problem with my rate, my code like below
$(function(){
var href = jQuery(location).attr('href');
$('.rate-it').rating({
required: true,
callback: function(value, link){
$.ajax({
type: "POST",
url: "<? echo base_url('post/rate/'); ?>",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
success: function(msg){
$('.rate-it').attr('disabled', true);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
displayError();
}
});
},
how to set people to can not vote repeatedly?
i'm using codeigniter
$(function(){
var href = jQuery(location).attr('href');
$('.rate-it').rating({
required: true,
callback: function(value, link){
$.ajax({
type: "POST",
url: "<? echo base_url('post/rate/'); ?>",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
success: function(msg){
$('.rate-it').attr('disabled', true);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
displayError();
}
});
},
focus: function(value, link){
var tip = $('#rate-result');
tip[0].data = tip[0].data || tip.html();
tip.html(link.title || 'value: '+value);
},
blur: function(value, link){
var tip = $('#rate-result');
$('#rate-result').html(tip[0].data || '');
}
});
});
If you want someone not to be able to vote repeatedly, you have to disable the button AND check on the server side that this client didn't vote yet.
Otherwise one could intercept the ajax request and repeat it.
You can try it :
In firefox or chrome, browse to your site, type F12, go to the console menu and copy the following :
$.ajax({
type: "POST",
url: "[your url here]",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
});
Launching it with ctrl + enter, you send a vote request.
Now put that in a for loop, and you have tons of votes.
Globally, keep in mind that you never clean user input on the client side.
I have a script
$('#postinput').on('keyup',function(){
var txt=$(this).val();
$.ajax({
type: "POST",
url: "action.php",
data: 'txt='+txt,
cache: false,
context:this,
success: function(html)
{
alert(html);
}
});
});
Suppose someone types a ,ajax runs . Immediately he types b c and so on. Ajax runs everytime. Is there a way to stop previous request when new is made ?
You can use the abort() method of the xhr. Try this:
var currentXhr;
$('#postinput').on('keyup',function(){
currentXhr && currentXhr.readyState != 4 && currentXhr.abort(); // clear previous request
var txt = $(this).val();
var currentXhr = $.ajax({
type: "POST",
url: "action.php",
data: 'txt=' + txt,
cache: false,
context:this,
success: function(html) {
alert(html);
}
});
});
If you don't want to use a global variable, you could store the xhr in a data-* attribute of the #postinput element.
Another method is to only fire the AJAX request when the user has stopped typing:
var timer;
$('#postinput').on('keyup',function(){
clearTimeout(timer);
var txt = $(this).val();
var timer = setTimeout(function() {
$.ajax({
type: "POST",
url: "action.php",
data: 'txt=' + txt,
cache: false,
context:this,
success: function(html) {
alert(html);
}
});
}, 100); // sends request 100ms after typing stops.
});
You can do like this with Jquery-
jaxRequests = new Array();
queueRequest = function(whatever, you, want) {
if(ajaxRequests[ajaxRequests.length - 1]) {
ajaxRequests[ajaxRequests.length - 1].abort();
}
ajaxRequests[ajaxRequests.length] = //Insert New jQuery AJAX call here.
}