dmsinc Posted February 10, 2014 Share Posted February 10, 2014 When I hard code the URL variable of this AJAX call, all is well, it executes and returns expected values. But when I replace part of the URL variable with a PHP variable, the AJAX fails to execute. There are no errors or flags being thrown by javaconsole or PHP. When i look at source view of page in either circumstance, they are identical - the PHP variable has been replaced properly. Why might the use of the PHP echo break the AJAX execution even though when viewing the source all appears to be normal? Hard coded version: <?php $domain="http://www.somewebsite.com/"; ?> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { if( $("#dig6").val() ) { $.ajax({ type : 'POST', url : 'http://www.somewebsite.com/includes/ffl2.php', data: { var1 : $('#dig1').val(), var2 : $('#dig2').val(), var3 : $('#dig6').val(), fflck : "chk" }, success:function (data) { $("#datatarget").append(data); } }); } }); </script> Version using PHP variable: <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { if( $("#dig6").val() ) { $.ajax({ type : 'POST', url : '<?php echo $domain; ?>includes/ffl2.php', data: { var1 : $('#dig1').val(), var2 : $('#dig2').val(), var3 : $('#dig6').val(), fflck : "chk" }, success:function (data) { $("#datatarget").append(data); } }); } }); </script> Snippet of 'view source' after page ready with PHP variable in AJAX call: <-- MORE CODE HERE --> $.ajax({ type : 'POST', url : 'http://www.somewebsite.com/includes/ffl2.php', data: { var1 : $('#dig1').val(), var2 : $('#dig2').val(), var3 : $('#dig6').val(), fflck : "chk" <-- MORE CODE HERE --> Quote Link to comment Share on other sites More sharing options...
.josh Posted February 10, 2014 Share Posted February 10, 2014 Your script works fine to me.. are you sure if( $("#dig6").val() ) { Is really triggering during your efforts to QA the $domain echo? try commenting out that condition and see if it makes a request. Quote Link to comment Share on other sites More sharing options...
dmsinc Posted February 10, 2014 Author Share Posted February 10, 2014 (edited) Your script works fine to me.. are you sure if( $("#dig6").val() ) { Is really triggering during your efforts to QA the $domain echo? try commenting out that condition and see if it makes a request. I commented the IF check out and ran it again, the AJAX fails - I checked it in Firebug console and got: POST http://www.somewebsite.com/includes/ffl2.php 200 OK 840ms Also in Firebug, Console: The POST data sent shows as: Parametersapplication/x-www-form-urlencoded fflck chk var1 5 var2 76 var3 02993 I just cannot understand why the PHP echoed variable is breaking the AJAX, it shouldn't -- but it is. The variables and data is correct so i just dont get it.. Edited February 10, 2014 by dmsinc Quote Link to comment Share on other sites More sharing options...
.josh Posted February 10, 2014 Share Posted February 10, 2014 so just to be clear.. your php var domain vs. the hardcoded one is the SAME domain, and it is the SAME domain as the page's domain? Because I can't think of any reason it could possibly fail, except that maybe in the php version, you are using a domain that's not the same as the page's domain, which would constitute as cross-site scripting. Quote Link to comment Share on other sites More sharing options...
dmsinc Posted February 10, 2014 Author Share Posted February 10, 2014 (edited) Josh - They are identical, letter for letter. I posted the PHP variable (copy/paste) in the first part of my question - 1'st line As a matter of fact - This AJAX call is accessed/called both in the automated way (like above) as well as a manual method (sumbitting the values through $_POST from a form) - and it works without a hitch. It's only when adding the PHP echo that it fails. Edited February 10, 2014 by dmsinc Quote Link to comment Share on other sites More sharing options...
.josh Posted February 10, 2014 Share Posted February 10, 2014 well, I'm stumped. At face value from what's presented here, I can't think of any reason it would do that :/ Quote Link to comment Share on other sites More sharing options...
davidannis Posted February 10, 2014 Share Posted February 10, 2014 I'm stumped too, but if I was going to try to debug it I'd do something like this: <?php $url="http://www.somewebsite.com/includes/ffl2.php";?> $.ajax({ type : 'POST', url : '<?php echo $url; ?>', to make sure it's not a missing / or something. Then, if it still did not work I'd view source in my browser and cut and paste the echoed value of $url into the browser's address bar and see if I got the expected response. Quote Link to comment Share on other sites More sharing options...
davidannis Posted February 11, 2014 Share Posted February 11, 2014 One more thought, it is possible that you have an error in your php code (could even be in a part of the code that we're not seeing) and that error causes the script to fail but doesn't produce an error that we see in the browser. So, add the following to the top of the script: ini_set("display_errors", "1"); error_reporting(-1); See if you get an error message. Quote Link to comment Share on other sites More sharing options...
.josh Posted February 11, 2014 Share Posted February 11, 2014 i thought about that.. but he said it works perfectly fine when the exact same url is hardcoded. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 11, 2014 Share Posted February 11, 2014 these unexplained problems are usually caused by something other than the code being focused on. to let someone duplicate the problem your PAGE is having, post all the code, less any database credentials/actual url... that would be needed to reproduce the problem. i'm guessing things like a conditionally defined variable, multiple page requests, some initial condition assumption, is the cause. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.