PHP Tools for Visual Studio. How to debug AJAX requests? - php

I have small project on PHP and I want to debug it with PHP Tools for Visual Studio. It works fine, debugger works great. But I have part of my project that works as a service, it listen AJAX requests from html pages and send responses in JSON format. In this situation debugger doesn't work at all. I set breakpoints in my php service file and it never fires, but I get correct response in client-side. So, my question is how to debug AJAX requests with PHP Tools?
I make an example of my client and server code to illustrate my problem.
server.php
<?php
if(isset($_REQUEST['response'])) {
echo json_encode('ok');
}
else {
echo json_encode('cancel');
}
?>
client.html
<!DOCTYPE HTML>
<html>
<head>
<title>AJAX test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$( "#request" ).bind( "click", function() {
var data = { response: "response" };
$.ajax({
url: "server.php",
type: "POST",
async: false,
timeout: 5000,
dataType: "json",
data: data,
success: function(result) {
$("#response").text(result);
},
error: function(error) {
$("#response").html(error.responseText);
}
});
});
});
</script>
</head>
<body>
<p>
<b>Request: </b> <span id="request">Send request</span>
</p>
<p>
<b>Response: </b> <span id="response">Here will be your response...</span>
</p>
</body>
</html>
So when I set the breakpoint in the server.php on the line with if-statement it doesn't fire, but client side successfully receive server response.

I've found the solution on the DEVSENSE forum
In this case xdebug just doesn't know when it should start debugging. Easist way is to add to php.ini next line:
xdebug.remote_autostart = on
And all works!

Related

JQuery.Ajax Success but just returns server PHP code as text

I am running a very simple Jquery.Ajax call as a test. The code should work as it is an example from a programming class. However, on my Windows XAMPP, all I get back in the alert is the ajax.php code in html format... I do get a success 200 code though from the Ajax query. I'm at a loss for what is wrong. Does anyone have any suggestions?
Ajax.html
<!DOCTYPE html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
// Handler for .ready() called.
useJQuery();
});
function useJQuery() {
$.ajax({
type: 'POST',
url: "ajax.php",
data: { i_want_text:'yes' },
success: function (response) { alert(response); },
});
}
</script>
</head>
<body>
</body>
</html>
Ajax.php
<?
if ($_POST["i_want_text"]) {
print "text received with value of " . $_POST["i_want_text"];
}
?>
from php documentation http://php.net/manual/en/language.basic-syntax.phptags.php
PHP allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).`
so In ajax.php change <? to <?php

New to ajax and can't set the session variable with a post request

I've been struggling with this for two hours now and I've narrowed down the issue to a simple test case. I've done plenty of research and finally settled on trying to reproduce the first answer from this question: Set session var with ajax.
When I push this button (lazy html, but that's not where the issue lies):
<html>
<head>
...
<script src="jquery.js"></script>
...
<head>
<body>
....
<?php
var_dump($_SESSION);
?>
<input type="button" class="update">
....
</body>
</html>
This ajax request in jquery.js gets called:
$.(".update").click(function() {
$.ajax({
type: "POST",
url:"setSession.php",
data: {bar: "foobar"},
success: function() {
console.log("Successful request sent");
}
});
And finally the setSession.php file:
<?php
session_start();
$_SESSION["foo"] = $_POST["bar"];
?>
The success message is printed to the console so I know I'm hitting the right file. Why isn't the session variable getting set? I have php 5.5.2 if that matters at all. Thanks for you help!
Like I suggested on the comments, do this to fix your code:
Reload the page on your success:
success: function() {
console.log("Successful request sent");
location.reload();
}
Make sure you have session_start() in all the pages that uses sessions:
session_start(); //this must be at the top, before you print anything
Might this can help you.
In setSession.php write echo $_SESSION["foo"] = $_POST["bar"];
In your ajax script do
$.(".update").click(function() {
$.ajax({
type: "POST",
url:"setSession.php",
data: {bar: "foobar"},
success: function(e) {
console.log(e);
}
});
now open browser console (F12). click on your button.update. Check if is there written bar in console.

jQuery calling php script onload

I am trying to use jQuery's ajax to call a php script onload. This script will return xml from a url web service, parse through it, and then display bits and pieces of it into a div tag when the page loads (onload). I'm okay with php when it comes to using forms, but getting php scripts to run onload is not working for me. Could someone please look through my code and give me your advice? Thanks in advance.
HTML:
<!doctype html>
<html>
<head>
<title>Word of the Day</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="code.js"></script>
</head>
<body>
<h3>Word of the Day: </h3>
<div id="word_day"></div>
</body>
</html>
JavaScript:
$(document).ready(function() {
$(window).load(function() {
$.ajax({
post: "GET",
url: "word_day.php"
}).done(function() {
alert(this.responseText);
}).fail(function() {
alert(this.responseText);
});
});
});
I did not add my PHP code since I was pretty sure that it wasn't the cause of my frustration.
You don't need those two handlers, just one:
$(document).ready(function()
{
$.ajax(
{
post: "GET",
url: "word_day.php"
}).done(function()
{
alert(this.responseText);
}).fail(function()
{
alert(this.responseText);
});
});
As you had it you were trying to create one handler when the handler fired which was never going to work.
Edit:
Your done/fail part should look like:
}).done(function(data)
{
alert(data);
}).fail(function(jqXHR, textStatus, errorThrown)
{
alert(textStatus);
});

Passing variable from jquery to php

i want to pass a variable from jquery to php but my code doesnt work .I tried very hard but no luck . When i click the button nothing happens.plz help thank you
one more thing i tried this without passing variable and i use only echo command then it works but when i pass variable nothing happens
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$("button").click(function(){
var var_data = 5;
$.ajax({
url: "myscript.php",
data: { var_php_data: var_data },
success: function(data) {
// do something;
alert(data);
}
});
});
</script>
</head>
<body>
<button>Change Content</button>
</body>
</html>
myscript.php contains the following code
<?php
echo $_GET['var_php_data'];
?>
Try wrapping your script in a document ready handler. Also you have a trailing comma (,) after the success callback that you should remove. Also you should use a , instead of ; after the data parameter. Specifying the HTTP verb for your AJAX request might also be useful:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('button').click(function() {
var var_data = 5;
$.ajax({
url: 'myscript.php',
type: 'GET',
data: { var_PHP_data: var_data },
success: function(data) {
// do something;
alert(data);
}
});
});
});
</script>
</head>
<body>
<button>Change Content</button>
</body>
</html>
The reason you need to wrap your click registration in a document ready handler is because you put your script inside the <head> section of your markup and the button hasn't yet been loaded by the browser when you attempt to subscribe to its click handler.
You can install Firebug (Firexox plugin), or user something similar for your browser to see real HTTP request and response. If you will post them here it will be more simple to find the problem.
Track request in FireBug. You can find by FB did request success (200) as well as date send by request.
Also, try http://{your_host}>/myscript.php?var_PHP_data=echoText
Addition:
Darin Dimitrov described the problem right way

bit.ly API use issues (shorten, lookup, and clicks)

as part of my time isn't dedicated to PHP dev, I'm having an issue which is probably easy to solve, but having absolutely no logs (PHP logs, browser firebug logs...) I'm pretty stuck.
Here's my code; as I'm testing stuff, it's pretty raw.
The index.php file :
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
// You may specify partial version numbers, such as "1" or "1.3",
// with the same result. Doing so will automatically load the
// latest version matching that partial revision pattern
// (e.g. 1.3 would load 1.3.2 today and 1 would load 1.4.2).
google.load("jquery", "1.4.2");
google.setOnLoadCallback(function() {
// Place init code here instead of $(document).ready()
$("#shrelock").submit(function(){
var url = $(this).attr('action');
$.ajax({
url: url,
success: function(data) {
alert(data);
}
});
return false;
});
});
</script>
<form id="shrelock" action='stats.php' method='get'>
<input type="text" name="url"/>
</form>
Now the stats.php file :
include("bitly.php");
if ( isset($_POST["url"]) ){
$urlToCheck = $_POST["url"];
$bitly = new bitly('myLogin', 'myKey');
print $bitly->shorten($urlToCheck);
}
I'm not sure what your question is, but I do see a few problems with your code.
The ajax request you perform uses GET while the serverside code seems to expect a POST
and also you forgot to send the 'url' parameter in the ajax call.
$.ajax({
url: url,
type: 'POST',
data: 'url=' + $('#shrelock input[name="url"]').val(),
success: function(data) {
alert(data);
}
});

Categories