Jump to content

PHP variable fails in AJAX call


dmsinc

Recommended Posts

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 -->
Link to comment
Share on other sites

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:

 

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 by dmsinc
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by dmsinc
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.