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
}
});
Related
Main.php
<script>
$(document).ready(function(){
searchUser();
});
function searchUser() {
alert("aaaaaaaa");
var data = $("#user-search-form").serialize();
$.ajax({
type: "POST",
url: "test.php",
data: data,
success: function(response) {
alert("bbbbbbbb");
}
});
alert("cccccccc");
return false;
}
test.php
<?php echo "testing 1234" ?>
Directory:
phpturtorial/admin/main.php
phptutorial/admin/test.php
I am calling php function using ajax but not working. My code able to alert "aaaaaaaa" and "cccccccc" but cannot alert "bbbbbbbb". Any idea ? is it related to my incorrect path ?
phpturtorial/admin/main.php and phptutorial/admin/test.php are in two different directories.
Hence why it is not able to locate test.php.
Change url: "test.php" to url: "/phptutorial/admin/test.php"
With the Given Information:
Case 1:
You need to modify the ajax request url slightly.
$(document).ready(function(){
searchUser();
});
function searchUser() {
alert("aaaaaaaa");
var data = $("#user-search-form").serialize();
$.ajax({
type: "POST",
url: "phptutorial/admin/test.php", //or the path to test.php
data: data,
success: function(response) {
alert("bbbbbbbb");
}
});
alert("cccccccc");
return false;
}
Case 2:
Provided path could be wrong as they are identical.
here is my simple code in trying to get data from php to ajax.
i just wanted to get a simple data pass back to ajax success. i already searched for this but i cant get it properly. my code is really simple.
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<SCRIPT TYPE="text/javascript">
$(document).ready(function(){
$("#1").click(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { "txt": "try"},
cache: false,
success: function(html)
{
alert(html.mes);
}
});
});
});
</SCRIPT>
<pre><button id="1">try</button></pre>
Then i load a ajax.php that has a simple code too
$var['mes'] = 'message';
echo json_encode($var);
and its alerting me "undefined". i know this is simple but i cant find it out where do am i wrong
You need to tell jQuery that the script is running JSON:
$(document).ready(function(){
$("#1").click(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { "txt": "try"},
dataType: 'json',
cache: false,
success: function(html)
{
alert(html.mes);
}
});
});
});
I'm trying to make a json call with jquery but noting happened. My code:
javascript:
<script type="text/javascript" charset="utf-8">
$(document).ready(function()
{
$("#TwImport").click(function()
{
$.ajax({
type: "POST",
url: "https://<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",
dataType: 'json',
success: function (data)
{
alert(data.percentage);
}
});
});
});
</script>
PHP
$output = array(
'percentage' => "50"
);
echo json_encode($output);
Any suggestions?
The code looks fine to me,
EDITED
Also try removing the protocol and use url: "//<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",
$("#TwImport").click(function()
{
$.ajax({
type: "POST",
url: "https://<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",
dataType: 'json',
success: function (data)
{
alert(data.percentage);
},
error: function (jqXHR,textStatus,errorThrown)
{
//Check for any error here
}
});
});
if you add and error callback to the ajax call you should get some error printouts to let you know what is going on
$.ajax({
type: "POST",
url: "https://<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",
dataType: 'json',
success: function (data)
{
alert(data.percentage);
},
error : function (e1, e2, e3) {
console.log(e1);
console.log(e2);
console.log(e3);
}
});
EDIT:
i just had a thought, if i remember correctly jquery ajax doesnt like using full url's if possible try using a relative path
I've just started learning jQuery and PHP, and I encountered a problem when I try to use Ajax. Either the $.ajax() function won't fire, or PHP won't return anything, I cannot tell. I must have forgotten something really stupid, I guess...
Here's the code. There's no reply, no alert, nothing.
js:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
done: function(response) {
alert("response");
}
});
});
</script>
PHP:
<?php echo "Something"; ?>
Thanks in advance.
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
done: function(response) {
alert("response");
}
});
supposed to be
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
}).done(function(response) {
alert("response");
});
success , error methods are generally declared in the place where you have written done which are now deprecated
You have your done in the wrong place.
Try this instead:
$.ajax({
url: "get_profile.php",
type: "GET",
data: {}
})
.done(function(response) {
alert("response");
});
You can have alternative option for check is there any error in your ajax call. and you can also do some stuff before getting the response of your ajax call like loading image shows to end users until the response result. for this you can use following code:
$.ajax({
url: "get_profile.php",
type: "GET",
data: {},
beforeSend:function(){
//do something like loading image
},
success:function(response){
alert(response);
},
error:function(e){
alert("something wrong"+e);
}
})
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.