How to check php is loaded when using AJAX - php

I use AJAX, and this work without problems, like this:
$('#main .left').load('top.php');
or this:
$.ajax({
type: "POST",
url: '/ajax_main.php',
data: "i=ajax_call",
success: function(msg){
console.log('ok');
}
});
// php side reprorts $_GET['i'] = "ajax_call";
When I set:
function periodicMethod(){
$.ajax({
type: "POST",
url: 'ajax_main.php',
data: "i=ajax_call",
success: function(msg) {
console.log('ok');
}
});
}
setInterval(periodicMethod, 1000);
The method is called and console.log reports "ok" any second. This looks like 'ajax_main.php' is loaded. But in the PHP file:
edit
echo '<script>console.log("php");</script>';
Does not give any results. The path in URL is ok. When I change it or remove 'ajax_main.php' from server, error is reported. What may be wrong?
edit
another version of php:
<script>
function ppp()
{ console.log('sss');
}
</script>
<?
echo '<script>ppp();</script>';
?>
is not working to

try this code
<script>
function periodicMethod(){
$.ajax({type: "POST",url: 'ajax_main.php',data: "i=ajax_call",
success: function(msg){ console.log('ok');
jQuery("#demo_data").html(msg);
}});}
setInterval(periodicMethod, 1000);
</script>
<html>
<body>
<form action="" method="post">
<input type="hidden" name="demo_data" id="demo_data"/>
</form>
</body>
</html>

Related

I want to call php function without refreshing page and return html

this code is about showing more data from database within the same page,without refreshing page,but this deos not work.need some help.
function showmore()
{
$.ajax({
type: "POST",
url: 'showmore.php',
data:{action:'true'},
success:function(html) {
alert(html);
}
});
}
<button type="button" onclick="showmore()">showmore</button>
showmore.php
<?php echo "<div><p>something</p></div>"?>
Change this to update your page dynamically:
In the php function:
<?php echo 'something'; ?>
Add this line to your html document (it is good practice not to return html from the server):
<div><p id="the-p-id"></p></div>
And in your jquery function, on success:
success: function(result) {
$('#the-p-id').html(result);
}
This is work for me. If I use library of jquery like this
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function showmore(){
$.ajax({
type: "POST",
url: 'showmore.php',
data:{action:'true'},
success:function(html) {
console.log(html);
}
});
}
</script>
<button type="button" onclick="showmore()">showmore</button>
**showmore.php**
<?php echo "<div><p>something</p></div>"?>
Output
<div><p>something</p></div>

how to get php response by using ajax

In a html web page, i want PHP response show in <span>..</span>.
Can anyone tell me how to do it or what's wrong with other codes?
thanks~~ ^^
web code:
<html>
<head>
<script type="text/javascript">
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
success: function(data) {
//dosomething.....
},
});
});
</script>
</head>
<body>
<form method="post">
<input id="submit" type="button" value="next">
</form>
<br><br><span id ="status"></span>
</body>
</html>
php code:
<?php
echo ("success");
?>
After you get the response do the following
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
success: function(data) {
$("span#status").html(data);
},
});
});
This won't work as the datatype provided won't be JSON... the dataType attribute in jQuery must match the type of data you send using PHP.
To get it working with a simple "success" message, you may do :
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'html',
success: function(data) {
$("span#status").html(data);
},
});
});
Because you use dataType: "json", the AJAX function thinks it's getting a JSON array. But since you only respond with "succes" in your php, it will not parse as JSON.
You should JSONify your output in PHP with json_encode(), after that you can call the succes callback in ajax, or use the .done function;
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
success: function(data) {
$("span#status").html(data);
}
});
});
or with .done functionality:
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
}).done(function(data) {
$("span#status").html(data);
});
});
The .done function is better for readability and maintainability in my opinion.

jquery, get data via ajax then submit form

I'm trying to get data via ajax then send it through a form. However it doesn't seem to be working. Any ideas what im doing wrong?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<form method="get" action="test.php">
<input id="myvar" type="hidden" name="albumid" />
<button type="submit" id="btnsubmit">Submit</button>
</form>
<script type="text/javascript">
$('form').submit(function() {
$.ajax({
url: "newAlbum.php",
success: function(data){
var album = data;
$('#myvar').val(album);
}
});
});
</script>
newAlbum.php
<?PHP echo '11'; ?>
test.php
<?php echo $_GET["albumid"]; ?>
Okay try adding as follows:
$('form').submit(function() {
$.ajax({
url: "newAlbum.php",
async:false,
success: function(data){
var album = data;
$('#myvar').val(album);
}
});
});
As someone else pointed out - you need to add the data parameter. But I would also use $(this).serialize(), since that will fetch all form input elements.
<script type="text/javascript">
$('form').submit(function() {
$.ajax({
url: "newAlbum.php",
data: $(this).serialize(),
success: function(data){
var album = data;
$('#myvar').val(album);
},
error : function(data) {
console.log(data);
}
});
});
</script>
Try receiving the data as JSON
$.getJSON()

AJAX call success but data not retrieved at server

I have an ajax call that passes data to another php file, createTest2.php, as below.
But the createTest2.php file throws error
"Notice: Undefined index: aaa in C:\xampp\htdocs\TestProj\Test\createTest2.php on line 2
I have no clue how to fix it.
caller.php
$(document).ready(function(){
$("#button_submit").click(function()
{
$.ajax({
type:"POST",
url:"createTest2.php",
data:{aaa : "UNIT_TEST"},
success:function()
{
alert("success");
}
});
});
});
createTest2.php
$test_name = $_POST['aaa'];
if you are using
$test_name = $_POST['aaa'];
you have to call ajax like this
$(document).ready(function(){
$("#button_submit").click(function()
{
$.ajax({
type:"POST",
url:"createTest2.php",
data:"aaa=UNIT_TEST",
success:function()
{
alert("success");
}
});
});
});
the main thing is that " data:"aaa=UNIT_TEST", "
Try this:
//sample.php
<?php
if(isset($_POST) && isset($_POST['aaa'])){
echo json_encode("hello world! Your data was: " . $_POST['aaa']);
}
?>
//your client side page
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function send(){
$.ajax({
type:"POST",
url: 'sample.php',
data:{aaa:"UNIT_TEST"},
dataType: 'json'
}).done(function(data){
alert(data);
});
}
</script>
</head>
<body>
Send
</body>
</html>
Try this in your, for example, index.html:
<script>
$(document).ready(function(){
$("#button_submit").click(function()
{
$.ajax({
global: false,
type:"post",
dataType: "html",
cache: false,
url: "createTest2.php",
data: {aaa:'test'},
success:function(html) { alert(html); },
error: function(e) {alert(e);}
});
});
});
</script>
And in your createTest2.php just put this to see if ajax calls are received:
<?php
echo $_POST['aaa'];
?>

I am trying to fetch json data's from .php file.php file i.e stored in server.I have writtened the following code.But I am unable to fetch data

I am new to this jquery and phonegap.I am finding little difficulty in parsing the data from .php file from a local server.Please help me in doing so.
This is my index.html page:
<!DOCTYPE HTML>
<html>
<h2>JSON Parser</h2>
<script type="text/javascript" src="jquery.js"/></script>
<script type="text/javascript">
function parseJSON()
{
var json;
$.ajax({
type: 'POST',
url: 'http://192.168.1.12/training/services/login.php',
cache: false,
// data: $('#abc').serialize(),
dataType: 'json',
success: function(data){
alert(data);
$('#data').append(data);
}
});
}
</script>
</head>
<body onload="parseJSON()">
<p>Employee's Information</p>
<form id="abc" method ="post">
<div id="data"></div>
</form>
</body>
</html>
The login.php file contains a sample json data's as follows:
{"username":"test#test.com","password":"password"}
If you are trying to get the data using php and ajax use jsonp,
in your PHP file add callback for json output:
echo $_GET['callback'] . '('.json_encode($data).')';exit;
apppend callback in your ajax call
Javascript:
$.ajax({
url: 'http://192.168.1.12/training/services/login.php?callback=?',
cache: false,
type: "GET",
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
And last but not least, dont forget to add domain white list for your localhost
in corodova.xml:
<access origin="http://192.168.1.12"/> OR
<access origin="*"/> to allow all domains
If I have understand correctly you may do something like that.
If your php page send data to your html one, you may use GET instead of POST
<!DOCTYPE HTML>
<html>
<h2>JSON Parser</h2>
<script type="text/javascript" src="jquery.js"/></script>
<script type="text/javascript">
function parse()
{
var json;
$.ajax({
type: 'GET',
url: 'http://192.168.1.12/training/services/login.php',
cache: false,
dataType: 'json',
success: function(data)
{
var obj = jQuery.parseJSON(data);
$('#data').html(obj["username"]);
}
});
}
</script>
</head>
<body onload="parse()">
<p>Employee's Information</p>
<form id="abc" method ="post">
<div id="data"></div>
</form>
</body>
</html>

Categories