-2

I am trying to send html code using post by the following JavaScript code:

var htmlTable= $('#granting_result').html();
alert(htmlTable);
$('<form action="tbl_create.php" method="POST"><input type="hidden" name="data" value='+htmlTable+' ></form>').submit();

The receiving page tbl_create.php has the following php code:

<?php

echo $_POST['data'];

?>

The problem is that this page is shown as a blank page and nothing displayed, although the output of alert(htmlTable); is as following: enter image description here

Aan
  • 12,247
  • 36
  • 89
  • 150

3 Answers3

2

You're building a string. You're inserting HTML into that string. That means the html you build looks like

<form><input ... value=<html><body>....

There's no quotes on your value, so the FIRST space in that blob of html you're inserting TERMINATES that attribute, leaving the rest of the html as illegal/unknown attributes.

If you want to embed HTML inside an attribute, you have to html-encode it

foo = '<form><input ....value="' + encodeHTML(var_with_html) + '">....';

producing

<form><input ... value="&lt;html&gt;&lt;body&gt; ... &quot; etc..." ...

for the encoding function: HTML-encoding lost when attribute read from input field

Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500
2

Create the form elements themselves and pass the html string as value property of the input instead of concatenating as attribute.

Then insert into dom and submit

var $input = $('<input>',{type:'hidden', name:'data'}).val(htmlTable);
var $form = $('<form>',{method:'post', action:'tbl_create.php'});
$form.append($input).appendTo('body').submit();
charlietfl
  • 170,828
  • 13
  • 121
  • 150
-1

Try to use load javascript method instead of $('#granting_result').html();

j08691
  • 204,283
  • 31
  • 260
  • 272
ahmed
  • 1