I am creating a page where a user can create a table. They can add rows, data, and styling. Whenever the user adds rows or data it's appended to an originally blank HTML table element that is shown on the page
<table class='preview_table' id='preview' name='preview'>
<!-- this is the space where entered info forms a table -->
<thead></thead>
<tbody></tbody>
</table>
This acts as a preview. The user can then style the preview, and that styling is also added onto the table and its children elements.
I want to be able to take the resulting HTML code and place it into the database, preferably using PHP, when the user hits the save button. This is my PHP code:
if(isset($_POST['submitted'])){
$prob = false;
if(!empty($_POST['title']) && !empty($_POST['type']) && !empty($_POST['name'])){
$new_title = mysql_real_escape_string(trim($_POST['title']));
$new_type = mysql_real_escape_string(trim($_POST['type']));
$new_name = mysql_real_escape_string(trim($_POST['name']));
$new_table = '' ;
$new_id = $id;
}else{
echo '<p style="background-color:red;font-size:20px;">Enter values for all fields</p>';
$prob = true;
}
if(!$prob){
$q = "INSERT INTO tables (id , active , type , name , title , html_code) VALUES ('$new_id' , 'Y' , '$new_type' , '$new_name' , '$new_title' , '$new_table')";
if(@mysql_query($q)){
echo '<p style="background-color:green;font-size:20px;">Success!</p>';
}else{
echo '<p>'.mysql_error().'</p>';
}
}
}
I want to set the HTML code for the table to the variable $new_table. So far I've tried
$new_table = $_POST['preview'];
but I understand now why that doesn't work. I've also tried to use ob_start and ob_get_contents but that doesn't work either. I am fairly new to PHP, so there may be something fairly simple that I'm missing. So, my question is: how would I go about storing the HTML of a dynamically created element into a database using PHP?
Any and all help is greatly appreciated! Thank you.
mysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.