1

index.html

<script>
function check() {
        var id = $('input[name=id]').val();
        $("#result").load("/ajax.php", {id:id});        
}
</script>

<input type="text" size="3" name="id"><br />
<button onclick="check();">Pay</button>
<div id="result"></div>

ajax.php

<?php
$id = (int)$_REQUEST['id'];
//some validations and SQL executions
echo "<script language=JavaScript src='https://*****/index.php?id=$id&invoice=$invoice&sum=$sum.......etc'></script>";

I try to form javascript in php file and embed it into div with id="result". I've used get, load, ajax, createElement methods but the script doesn't execute.

2
  • shouldn't your script tag contain valid js file..., you seem to use php file as source of script tag ..? Commented Jan 16, 2013 at 7:09
  • To be honest what you do is a bit strange. But if you want to load script by URL to loaded document, you do not have to send whole <script> tag by ajax. Just send URL and generate <script> tag on client side. Commented Jan 16, 2013 at 11:58

6 Answers 6

1

try this.. $('#result').load('ajax.php');

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

2 Comments

I need to send POST data to PHP script
@user1312750 api.jquery.com/load/#request-method - jQuery.load supports sending data as POST.
1

In your php file ajax.php include the header.

header('Content-Type: text/javascript');

And in index.html add type=”text/javascript” to the script tag.

<script type=”text/javascript”>
        function check() {
           var id = $('input[name=id]').val();
           $("#result").load("/ajax.php", {id:id});        
         }
   </script>

1 Comment

Since ajax.php returns HTML (<script ...) sending text/javascript content type makes no sense.
1

You must load script as real script element. DOM conversion of "<script>/*...*/</script>" may fail in this case. This is done via standart DOM document.createElement function.

var script = document.createElement("script");
script.src = path;

Other way is to download the script via AJAX and then eval it.

Comments

0

Try using getScript:

$.ajax({
  url: url,
  dataType: "script",
  success: success
});

Comments

0

try this:-

"<script language=JavaScript src='https://*****/index.php?id="'.$id.'"&invoice="'.$invoice.'"&sum="'.$sum.'".......etc'></script>";

Comments

0

Why wouldn't you load directly the script URL using AJAX? ajax.php only should return path to the script:

 $id = (int)$_REQUEST['id'];
 //some validations and SQL executions
 echo "https://*****/index.php?id=$id&invoice=$invoice&sum=$sum.......etc";

Besides that all HTML element attributes should be enclosed with quotes or apostrohpes.

Then you should load this url in javascript and generate script element here:

   function loadScript(url,appendTo) {
     var script = document.createElement("script")
     script.type = "text/javascript";
     script.src = url;
     if(typeof appendTo.appendChild == "function")     //Chceking if we can append script to given element
       appendTo.appendChild(script); 
     else
       document.body.appendChild(script)       //This will not work in old IE, where document.body is not defined. 
   }

You call this function with script url and optional target element (where will be put in) as parameters. Like this:

  $.get("ajax.php",{param1:"value"},function(scriptUrl) {loadScript(scriptUrl, document.getElementById("result");})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.