I'm trying to store HTML in a database, so when I retrieve the form from the database, I need to show it as a form rather than text. Is there a way to do that?
This is the form
$form = "<form id='' method='' action='' class=''>
<input type='hidden' name='my-item-id' value= $uid />
<input type='hidden' name='my-item-name' value=$title />
<input type='hidden' name='my-item-price' value='1' />
<input type='hidden' name='my-item-qty' value='1' />
<input type='submit' name='my-add-button' class='button' value='Add to cart'/>
</form>";
at the moment when I retrieve the above from the database it shows as text,
also the form field in the database is varchar(1000)
include('adodb.inc.php');
include('adodb-pager.inc.php');
$sql = 'select title as "TITLE",description as "DESCRIPTION",form as "ADD TO CART" from mathspapers order by sysdate desc';
$pager = new ADODB_Pager($db,$sql);
$pager->Render();
Thanks
Certainly it is possible to store html (or whatever markup or language) inside a database. Also handling that with PHP is possible. All you have to make sure is that you escape the content so that a) your code is not open to sql injection and b) the statements are valid regardless of the contents value.
you can save the html codes just like text. You can use varchar(max) type column to save the html code in table. Display the code is depending the browser. But if you use nvarchar type that will cause problems in display.
When you are storing text in Database you probably use addslashes()
.
Then when you return text from Database you need something like stripslashes()
before you show your HTML text. PHP Manual on stripslashes
Instead of storing whole form pattern (and values) in the db field i can advise you to store only form field names and data (values) as key-value pairs.
You can easily use them if you store them in a standard way like ; (use at form processing page)
$form_data = "my-item-id=".$_POST["my-item-id"]."&my-item-name=".$_POST["my-item-name"]."&my-item-price=".$_POST["my-item-price"]."&my-item-qty=".$_POST["my-item-qty"];
then store $form_data only at db
to use data from db (surely after pulling $form_data from db) you can use parse_str
parse_str($form_data,$form_data_from_db);
echo $form_data_from_db["my-item-id"] will print your stored form input for instance
But these are not the my main advice. How you will build your pattern for each stored data field? Just build a function to create form like many cms does.
try this ;
function myform_pattern_1($id,$method,$action,$class,$form_data){
parse_str($form_data,$fd);
$form_html = "<form id='' method='' action='' class=''>";
$form_html .= "<input type='hidden' name='my-item-id' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-name' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-price' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-qty' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='submit' name='my-add-button' class='button' value='Add to cart'/>";
$form_html .= "</form>";
return $form_html;
}
Call this function where you like as ;
echo myform_pattern_1("form_id","post","","form_class",$form_data);
This usage has a great advantage to your method. You can change form syntax whenever you wish this way. You will -maybe- want to integrate a Jquery validation plugin later or just want to use another styling or even change syntax by adding tags you will have to change all stored db fields if you store whole form at db. Function usage is much more flexible.Also you will not use db for storing unnecessary -repeating- tags etc.
i hope you like , cys
For more information on parse_str ; http://php.net/manual/en/function.parse-str.phpenter code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With