Reputation: 127
I'm attempting to return an HTML form from a GET request triggered by jQuery but I'm not sure how to go about doing this. A customer's server will call to mine which will then return a form. I understand the same domain policy comes into play and there are ways around it such as JSONP, but I can't figure out how to return an entire HTML form.
right now I'm testing on localhost, so I can just use GET to another page on my server without having to worry about same domain policy, if that helps to simplify things
Here's my thought process.
//form_deliver.php
//this code would go on the customer's server which would then call to mine
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<?php
echo "look at all of this stuff!";
?>
<script>
$.getJSON("/form_deliverer.php?form_request=true",function(data) {
//alert(data.check); //this was just for testing,
});
</script>
Here's the page that it sends the GET request to
//form_deliverer.php
//this code is on my server which will return a form to the customer
<?
if(isset($_GET['form_request'])){
// echo json_encode(array("check" => "true")); //just for testing
//return form here
?>
For simplicity, let's say this is the form I want to return is
<form method="post">
<input type="textbox" name="text"/>
<input type="submit" name="submit_text"/>
</form>
Edit this is my update according to jackJoe's suggestions
Code on form_deliver.php
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<?php
echo "look at all of this stuff";
?>
<script>
var form_request = "true";
$.get("/form_deliverer.php", {var: form_request},
function(data){
$("#yourdiv").html(data);
}, "html");
</script>
<div id = "yourdiv">
</div>
This is on form_deliverer.php
<?
if(isset($_GET['form_request'])){
return "
<form method='post'>
<input type='textbox' name='text'/>
<input type='submit' name='submit_text'/>
</form>
";
}
?>
Upvotes: 1
Views: 3374
Reputation: 71939
If you'll eventually use JSONP, you'll have to make the form markup a string, which should be the value of a property of the json-encoded object the server is responding with.
So, your server will have to echo something like this:
<?php
$o = new stdClass();
$o->formHTML = '<form>...</form>';
echo json_encode($o);
?>
Upvotes: 1
Reputation: 11148
As I said in my comment, you could use AJAX
or in your case GET
(http://api.jquery.com/jQuery.get/) and it allows you to return html.
Example:
$.get("yourPHP.php", {var: somethingdynamicpassedviajavascript},
function(data){
//get the result
$("#yourdiv").html(data);
}, "html");
The end of this function has the return type (in this case "html"). This example would place the HTML into the div #yourdiv
Upvotes: 3