Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"

share|improve this question
1  
why don't you directly access "x" variable in ajax response? like console.log(x); – Hasina Jun 7 at 10:20
here in my case, data is a long HTML string with a lot of variable which values are in javascript. – Wasim Jun 7 at 10:22
Now your Question is understoodable. – Hasina Jun 7 at 10:27
thanks, i just modified my question – Wasim Jun 7 at 10:28

2 Answers

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

share|improve this answer
i have modified my question, please review – Wasim Jun 7 at 10:27
I think, from what I've shown you, you can at least try to figure out how to add a second variable. I'm not your code monkey. – Pudge601 Jun 7 at 10:28
i know but i can't pass variable like that. It some thing use of eval function – Wasim Jun 7 at 10:31
You don't need to use eval to achieve this. Can you update your question to include your attempt at solving this for 2 variables? – Pudge601 Jun 7 at 10:44
i am using $.globalEval(data) but it through error "x is undefine" – Wasim Jun 7 at 10:45
show 2 more comments

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"];
share|improve this answer
i have modified my question, please review – Wasim Jun 7 at 10:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.