0

My task is to post selected autocomplete value from index.php to test.php.I need to return data to index.php as a variable not an output.I think I need to use php CURL.But I don't get any positive result yet.

index.php:

select: function(event, ui)
      {
selected=ui.item.value;

    jQuery.ajax({

url:"test.php",
data:{selected},
type:"POST",

success:function(data){

}
</script>

<?php

if(isset($response)){

echo $response;
}

test.php:

if(isset($_POST["selected"])){

    $selection1=$_POST["selected"];

    $url=mysite.com/index.php";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $selection1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

}

?>
Darius92
  • 301
  • 5
  • 21
  • why do an http request to your own site? php's already running as par tof that site. and your test.php doesn't actually output anything. you run curl, then simply exit. you should at least have `echo $response` or somethingo, so the curl results get sent back to the JS code. – Marc B Apr 22 '16 at 19:27
  • ok, but with `echo` i wll back only output?I need back actual variable with an value – Darius92 Apr 22 '16 at 19:35
  • 1
    I assume that test.php is POSTing to another domain or, as @MarcB points out, this would not be needed. What di you get in $response? have you checked that $_POST values are as expected? Have you tried encoding $selection1? See http://stackoverflow.com/questions/5224790/curl-post-format-for-curlopt-postfields for more info on encoding the curl POST – bitfiddler Apr 22 '16 at 19:35
  • So I gues this line should be like this: `$url="index.php?";` and `curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($selection1));` – Darius92 Apr 22 '16 at 19:54

0 Answers0