Aan Aan - 1 month ago 5
PHP Question

Display HTML code from $_POST variable

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

Answer

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();
Comments