0

I am trying to give a jQuery variable to a PHP variable when document is ready. What I have is a list with all sort of values with different links, which I get by an external website via cURL. With jQuery I am getting the value's by the href link. So what I want is when document is ready with loading, then it should give the jQuery variable to a php variable and echo it.

What I have so far:

//Javascript.js
$(document).ready(function() {
var value = $("a[href='index.php?p=trading&m=42&b=BTC']:eq(3)").text();
 $.ajax({
    url: "index.php",
    type: "get",
    data: value,
    success: function(){
        alert("success");
        alert(value);
    },
    error:function(){
        alert("failure");
    }
});

alert(value);   
});

The output of value = 55.0

<a href="index.php?p=trading&m=42&b=BTC">55.0</a> //from cURL page
<!-- index.php -->
<?php
$a = $_GET['value'];
if($_GET['value'] == "")
{
    echo("empty");
}
else {
    echo($a);
}
?>

The GET/POST is success, but it doesn't show up at the php page. Hope anyone got a solution for this.

3
  • and where should the output of echo be? within the same page you are calling the ajax function? Commented Mar 12, 2014 at 11:09
  • Yes, if that is possible when the page is ready with loading Commented Mar 12, 2014 at 11:10
  • data: value, is what's being passed to the PHP file, you need a response object like the answer below which will hold what's returned from the PHP file Commented Mar 12, 2014 at 11:12

3 Answers 3

4

Change this:

data: value,
success: function(){
    alert("success");
    alert(value);
},

to this:

data: "value=" + value,
success: function(response){
    alert("success");
    alert(response);
},

You need to capture the response(echo) from the index.php file and alert that.

ADDITIONAL INFO: After seeing the whole index.php file

Put this block at the top of your PHP file, not the bottom:

<?php
    if($_POST['value'] == "")
    {
        echo "empty";
    }
    else {
        echo $_POST['value'] ;
    }
    exit;
?>

And add the exit; at the end like above.

When you call this php file via ajax, you do not want to execute the rest of the php file, only the part in the if $_GET block. So this needs to be at the top of the file with exit after it so the rest doesn't get executed when it is a $_GET request.

Sign up to request clarification or add additional context in comments.

11 Comments

also note he hasn't set a variable in data:value meaning , the url will look like this , if value is say 55.0 index.php?55.0. so it should be something like this data: "value="+value
That response is giving me the whole source of the page
@Jordy144 The whole source of what page?
I don't see any point in the response. What I want is that PHP gets the jQuery value, and echo it.
I also get the message Undefined index: value in index.php, it seems that it can't get the value
|
1

Try this

$(document).ready(function() {
var value = $("a[href='index.php?p=trading&m=42&b=BTC']:eq(3)").text();
 $.ajax({
    url: "index.php",
    type: "post",
    data: "value="+value,
    success: function(result){
        alert("success");
        alert(result);
    },
    error:function(){
        alert("failure");
    }
});

alert(result); 
});


index.php

$a = $_POST['value'];
if($_POST['value'] == "")
{
    echo "empty";
}
else {
    echo $a ;
}

1 Comment

I did not vote you down, but it's not working. Please take a look at my index.php pastebin.com/m0vMvQ7G
0

You can get the desired result by changing data:value to data:{"value":value}

Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.