-3

HOW TO USE eval() FUNCTION HERE
ajax.js

 var x="hello world";
 var y="anyother string";
    $.ajax({
        url:"ajax.php",
        success:function(data){
            console.log($.globalEval(data));
        }
    });

ajax.php

<?php exit("'first string is '+x+' second one is'+y");?>

I want to return x,y as a variables to ajax response, so console.log(data) can print value of

first string is hello world second one is another string  

but it says "x is undefine"

NOTE : I need the solution without passing x and y as data from ajax

4

3 回答 3

1

Javascript can make use of the eval() function to parse the string you get from PHP. For a simpel example I have dropped the ajax call to get the request in the next example, but you get the idea. This jsfiddle has a working example.

<script>
var x="hello world";
var y="anyother string";

//normally you would get this from ajax
var str = "'first string is '+x+' second one is'+y";

//complete will contain the full string after eval.
var complete = '';
eval('complete=' + str);


alert(complete); //first string is hello world second one isanyother string
</script>

Now in your case, when you do use AJAX, it would become

<script>
var x="hello world";
var y="anyother string";

$.ajax({
  url:"ajax.php",
  success:function(data){
    //complete will contain the full string after eval.
    var complete = '';
    eval('complete=' + data);
    console.log(complete);
  }
});
</script>

Now the above is the actual answer to your question, I would still advise against it. See why is using the javascript eval function a bad idea

To have something like this without eval, you could use something like

<script>
var x = "hello world";
var y = "anyother string";

//normally you would get this from ajax
var str = "first string is {x} second one is {y}";

//complete will contain the full string after eval.
var complete = str.replace('{x}', x).replace('{y}', y);

alert(complete); //first string is hello world second one isanyother string
</script>
于 2013-06-14T07:10:20.213 回答
-1

ajax.js:

  $.ajax({
        method: "post",
        url:"ajax.php",
        data: {x: "helloworld", y:"another string"},
        success:function(data){
            console.log(data);
        }
    });

ajax.php:

echo "First string: ".$_POST['x'].", second string: ".$_POST["y"];
于 2013-06-07T10:22:46.090 回答
-1

Are you trying to create an echo script, whereby you send a variable via AJAX to your PHP script, and your PHP script returns that value back?

If so, you need to provide x in the data setting for your AJAX request, as so;

var x="helloworld";
$.ajax({
    url:"ajax.php",
    data: {
        x: x
    }
    success:function(data){
        console.log(data);
    }
});

And then in your PHP script, you need to retrieve the value from the POST array, and output it as the response:

<?php
exit($_POST['x']);

EDIT

To include a second variable (y)

var x="helloworld",
    y="another string";
$.ajax({
    url:"ajax.php",
    data: {
        x: x,
        y: y
    }
    success:function(data){
        console.log(data);
    }
});

and in PHP;

<?php
$x = $_POST['x'];
$y = $_POST['y'];
exit("First string is $x, second string is $y");

I would strongly recommend finding some tutorials on JavaScript and PHP basics to get an understanding of some of these concepts

于 2013-06-07T10:23:58.513 回答