jquery ajax query is executed twice - php

i have form with user data to input and javascript ajax to send information on server and handle it with php to send this info on email, but when i submit, ajax send this informatio twice
javascript:
$.ajax({
type: "POST",
url: "<?php echo JURI::root(). " / components / com_searchtour / handler.php " ?>",
data: {
'birthday': $("#birthday").val(),
'name': $("#name").val(),
'name2': $("#name2").val(),
'name3': $("#name3").val(),
},
success: function(html) {
$('div#text_after_submit').append("<h2>Отправлено</h2>");
}
});
return false;
found the problem i have some code below this
$(function(){
$.datepicker.setDefaults(
$.extend($.datepicker.regional["ru"])
);
when i remove it, now all works fine :) thanks everyone for help

$.ajax({
type: "POST",
url: "<?php echo JURI::root(). " / components / com_searchtour / handler.php " ?>",
data: {
'birthday': $("#birthday").val(),
'name': $("#name").val(),
'name2': $("#name2").val(),
'name3': $("#name3").val(),
},
success: function(html) {
$('div#text_after_submit').append("<h2>Отправлено</h2>");
}
return false;
});

function(event){ e.preventDefault(); $
event and e are two different var names, so the submit event won't be catched.
try to substitute event with e.

Related

Function is not working when called with AJAX

I have:
call.php:
function ajaxnotify(){
notify('hello');
}
function notify($a){
echo "
<script src=\"/js/jquery.min.js\"></script>
<script src=\"//cdn.socket.io/socket.io-1.4.5.js\"></script>
<script>
$(function(){
socket = io.connect(\"MY IP AND PORT\",{secure: true});
socket.emit('say', {'msg': '$a'})
});
</script>
";
}
switch($_GET["do"]){
case "1":
ajaxnotify();
break;
case "2":
notify();
break;
}
and this code absolutely works when I directly go to mysite/call.php. But when I use:
$.ajax({
type: "POST",
url: "call.php?do=1",
data: "",
success: function(html){
}
});
It just doesn't work. it works when I'm directly going to page, but it doesn't work when I call ajax function from html.
Where is my mistake, guys?
You're sending the request using POST, yet using $_GET to retrieve the variable in PHP.
Change the type: "POST" in jQuery, to type: 'GET'. Here's a complete example:
$.ajax({
type: 'GET',
url: 'call.php',
data: { do = 1 },
success: function(html) {
console.log(html); // to at least verify the response
}
});

Simple Ajax Post to PHP echo

I have the following code on product.php .. can't seem to echo post variable from ajax post. Alert displays fine. Please help
JQUERY
document.getElementById("LBTest").onchange = function(){
var lbtest = $('#LBTest :selected').val();
$.ajax({
type: "POST",
url: "product.php",
data: {test: lbtest},
success: function()
{
alert("Successful");
}
});
}
PHP
if(isset($_POST['test'])){
$data = $_POST['test'];
echo $data;
}
You need to do something with the data you receive from the ajax call. For example, to put the result into a <div> called resultDiv:
success: function(data)
{
$('#resultDiv').html(data);
alert("Successful");
}
$.ajax({
type: "POST",
url: "product.php",
data: {test: lbtest},
success: function(data)
{
alert("Successful");
}
});
You need to add the data to the success function that is called. You can do this locally or reference another function meant to handle responses coming back from the server.
success: function(data)
{
console.log(data);
alert(data + " was returned from the server");
}
It is a good idea on the server side to json_encode the objects that are being returned and using error codes that can be more appropriately handled on the client.
handleResponse(data) {
var data = $.parseJSON(data);
if(data.code >= 200 || data.code < 300) {
// modify the dom, add data to a model, take over the world with your web app.
}
}

How to Send Jquery Ajax data on Same php Page?

hi i want to send javascript data via ajax to the same page in wordpress. Cannot fetch the data in testajax.php page. its shows Undefined index: name. And how to send the data on same php page rather than testajax.php ?
<script>
jQuery.ajax({
type: "POST",
url: 'http://localhost/projects/xxx/wordpress/wp-content/themes/xxx/testajax.php',
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});
var foo = 'somename';
jQuery.ajax({
url:'yourUrl',
type: "POST",
data: {'name':foo},
success: function(data)
{
alert('success');
}
});
php
<?php
if(isset($_POST['name'])){
//Do whatever you want
}else{
//Do whatever you want
}
?>
jQuery.ajax({
url: "./_FILE_" // ./index.php for example
type: "POST",
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});

Passing AJAX variables to a Jquery ui dialog on the same page

Right now I've got a ui dialog on a page, and I need some way to pass a javascript variable to the php within the dialog via AJAX. Here's my code:
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(){
$("#dialog").dialog('open');
}
});
});
and the beginning of the PHP:
<?php
if(isset($_POST['user'])){
echo '<center><b>User: '.ucfirst($_POST['user']).'</b></center><br />';
}
The problem is, it's just not getting passed. I'm very new with Ajax so I'm sure I'm messing something up.
This is not working because you don't specify the return variable, see your code with correction:
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(data){ // Here you specify the callback variable from the AJAX call
alert(data); // Here will show '<center><b>User: UserExample </b></center><br />';
$("#dialog").dialog('open');
}
});
});
You can make your dialog content my loading some page like Userpage.php and then pass User as a url parameter
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(){
$("#dialog").load('/userpage.php?User=' + user).dialog('open');
}
});
});

Ajax passing data to php script

I am trying to send data to my PHP script to handle some stuff and generate some items.
$.ajax({
type: "POST",
url: "test.php",
data: "album="+ this.title,
success: function(response) {
content.html(response);
}
});
In my PHP file I try to retrieve the album name. Though when I validate it, I created an alert to show what the albumname is I get nothing, I try to get the album name by $albumname = $_GET['album'];
Though it will say undefined :/
You are sending a POST AJAX request so use $albumname = $_POST['album']; on your server to fetch the value. Also I would recommend you writing the request like this in order to ensure proper encoding:
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.post('test.php', { album: this.title }, function() {
content.html(response);
});
and if you wanted to use a GET request:
$.ajax({
type: 'GET',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
or in its shorter form:
$.get('test.php', { album: this.title }, function() {
content.html(response);
});
and now on your server you wil be able to use $albumname = $_GET['album'];. Be careful though with AJAX GET requests as they might be cached by some browsers. To avoid caching them you could set the cache: false setting.
Try sending the data like this:
var data = {};
data.album = this.title;
Then you can access it like
$_POST['album']
Notice not a 'GET'
You can also use bellow code for pass data using ajax.
var dataString = "album" + title;
$.ajax({
type: 'POST',
url: 'test.php',
data: dataString,
success: function(response) {
content.html(response);
}
});
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});

Categories