Home > Software design >  How can I send data from a div with contenteditable attribute to php table?
How can I send data from a div with contenteditable attribute to php table?

Time:12-06

Good evening. I show you the code that sends data but empty

 <div class="container-contentEditable">
                   <form action="../logica/insertPost.php">
                    <div class="editable" contenteditable="true"><span name='publicar' id="span-hidden">¿Qué estás pensando?</span>
                    </div>

                    <input type="hidden" id='input' name="publicar">

                    <button id="button" class="boton-publicar">Publicar</button>
                  </form>
                </div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

enter code here

CodePudding user response:

Sorry, idk what is effect you want. um... you said that you want to send the datas which have the attribute "contenteditable", but in your case, your element of "contenteditable" have no value, and it's not a input-box, it's div and span. So what datas are actually you want to send?

Your mean is, the div has attribute "contenteditable", and the name of element in the div is "publicar", so you want to send the value of element "<input type="hidden" id='input' name="publicar">" OR you want to send the text "¿Qué estás pensando?"

CodePudding user response:

You should not use div in form tag. Form has its own elements that you should use. In form element you can use these elements:

  • input
  • button
  • textarea
  • label

In your situation the best solution is to use textarea element instead of div.

 <div class="container-contentEditable">
     <form action="../logica/insertPost.php" method="POST">
         <textarea name="publicar" placeholder="¿Qué estás pensando?"></textarea>
         <button id="button" class="boton-publicar">Publicar</button>
     </form>
 </div>

I also set method to form element. It should always be used when sending request to PHP file.

As what comes to your original question I don't quite understand it. Do you want to insert data to database? Because that is how i understood your question. Here is basic method how to insert data to database.

$query = 'INSERT INTO table_name (comment) VALUES (:comment)';
$stmt = $conn->prepare($query);
$stmt->execute(
    [':comment' => htmlspecialchars($_POST['publicar'])]
);

I can explain what this code does and why. First line we set SQL command that we want to execute in database. Second line we send this SQL command to database. In last lines we send data to database. Data and SQL command is sent separately because we want to avoid SQL injection from happening. You can read more about SQL injection in here

You need to establish connection to database. In this example we assume that you already have done that. You can check more information about PDO from PHP manual.

Here are few links that you might need:

  •  Tags:  
  • php
  • Related