Mahesh Mahesh - 1 month ago 16
PHP Question

What is wrong in my xmlhttprequest, not getting any response



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<form action="" method="GET" id="st_epic">
<label> EPIC Number</label>
<input type="text" id="epic_num" name="epic_num" placeholder="Enter the epic number">
</form>

<script>
jQuery(document).ready(function($) {
$('#st_epic').submit(function() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

var url = '/curl.php';

xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState===4 && xhr.status===200) {
console.log("complte =" + xhr.responseText);
var div = document.getElementById('epic_result');
div.innerHTML = xhr.responseText;
}
}
xhr.send();
return false;
})
})
</script>

<div id="epic_result">
</div>
<?php
$en = isset($_GET['epic_num']);
$es = isset($_GET['epic_state']);
$ec = isset($_GET['captcha']);

$url = 'example.com/';

$ch = curl_init();

$fields = 'Search?epic_no=' . $en . '&page_no=1&results_per_page=10&reureureired=a32412c7-5598-401f-903c-f68a8690d4b1;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded","Accept: */*"));
$result = curl_exec($ch);
curl_close($ch);
?>





I am trying to send a request from mydomain.com to a domain example.com/ with a parameters in $fields. When i run, i not getting any response neither it is showing some error. I am confused what's wrong am i doing? If anybody can help, it will be very appreciated. Thank you

//my curl.php


Answer

Try like this:

    <script>
           jQuery(document).ready(function($){

             $('#st_epic').submit(function(){
              var epicNum = $("#epic_num").val();
                  $.ajax({
                      url: '/curl.php',
                      method: 'GET',
                      data: {epicNum:epicNum},
                      dataType: 'json'
                  }).done(function (response){
                     if (response.message == 'succes') {
                        console.log("complte =" + response.data);
                        var div = document.getElementById('epic_result');
                       div.html(response.data);
                     }
                  });
            return false;
    })
})
</script>

UPDATE You have bad php code (((

try this:

<?php
  if ($en = isset($_GET['epic_num'])) {
       $es = isset($_GET['epic_state']); //you must send it from ajax
       $ec = isset($_GET['captcha']); // this too
       $url = 'example.com/';
       $ch = curl_init();
       $fields = 'Search?epic_no=' . $en . '&page_no=1&results_per_page=10&reureureired=a32412c7-5598-401f-903c-f68a8690d4b1';
       $url = $url.$fields
       curl_setopt($ch,CURLOPT_URL,$url);
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
       $output=curl_exec($ch);
       curl_close($ch);
       return array(
          'message' => 'succes',
          'data' => $output);

  }
?>