0

I have two files 1) index.php(picks data from the code editor and submits for processing via Jquery Ajax to exec.php) 2) exec.php (currently just transfer the data it recieved via index.php using jsonp)

Code of index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function test() {
    var code = document.getElementById('code').value;
    var code_data = "code=" + code;
    alert(code_data);
    $.ajax({
        type: "POST",
        crossDomain: true,
        url: "http://code1.guru99.com/exec.php",
        data: code_data,
        dataType: "jsonp",
        success: function (data) {
            alert(data);
        }

    });
    alert("End of Test");
}
</script> 


<form name="myform" id="myform" method="POST" class="code-box">

<textarea   name="code" id="code"><?
$code='<?php
"Hello";
?>';
echo $code;
?>

</textarea>   <!-- for add html tag in text area nad print the code-->
<div class="hint">This code is editable. Click Run to execute.</div>
<input type="submit" value="Run" id="submit" onClick="test();"><!--<img id="ajax-loader"     name="ajax-loader" src="/img/ajax-loader.gif" class="hidden" style="vertical-align:middle" />-->

</form>
<div name="label" id="label"> </div>
<div name="out" id="out"> </div>

Code of exec.php

<?php
$code=$_POST['code'];
$fp=fopen("file.txt","w"); // Storing the data into a file just to know that data is passed
fwrite($fp,$code);
fclose($fp);
header('Content-Type: application/jsonp');
echo $_GET['callback']."(".json_encode($code).");"
?> 

The problem is data just does not pass into exec.php. I am not sure why... The code is live at http://code.guru99.com/php/ Please help...

  • please indent your code so we can read it. – Mike 'Pomax' Kamermans Jun 20 '13 at 14:02
  • Are you dealing with three different domains, or two (including the one that the javascript is running from) – Kevin B Jun 20 '13 at 14:49
  • 2
    JSONP cannot use `POST`. JSONP works by appending a new ` – apsillers Jun 20 '13 at 19:32

2 Answers2

0

You cannot use AJAX to do this. Instead consider posting from a hidden Iframe using a regular FORM and setting the action to the URL you desire. You can still submit the form using JavaScript.

You can also listen to the onload event on the iframe to detect when your post has completed.

Alternately, you can use a server-side proxy.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

The code syntax is correct.

May the problem could be with your server