PHP break list items "<li>" into variables to add in MYSQL table

Asked

Viewed 53 times

0

I have a form with <textarea> with the following value:

<textarea id="lista-ingredientes" name="lista-ingredientes">
 <ul>
  <li>sal</li>
  <li>pimenta</li>
 </ul>
<textarea>

I need that when sending the form, PHP "break" the items of <li> in separate variables to add a row of each item, what I have is this one, but it inserts everything in one line:

$sql = "INSERT INTO receitas_ingredientes (id, ingrediente) VALUES ('NULL', ".$_POST['lista-ingredientes'].")";

I believe it is something using explodes and foreach, but I do not have much knowledge and I could not assemble

  • Question: Why do you own one ul/li within a textarea? The only permitted type of content for a textarea is text, so this ul would be interpreted as plain text, not HTML, not making much sense.

  • yes, it is that after this value will be pulled from the table to show inside a div, there is a better, or more correct, way to do this?

  • what happens is that I have an ingredient input +add button that with a jquery will add all the ingredients in the text area, then insert them all at once in the table

  • @Leandromarzullo the ideal is to save in the bank without the read and when you pull from the bank and insert in the view, add the text inside the li in a foreach, for example

  • got it, just for you to see, this is what I was doing: https://answall.com/questions/245268/bot%C3%A3o-add-insert-value-do-input-inside-do-pr%C3%B3prio-formul%C3%A1rio/245274#245274

  • the ideal is I save as <textarea>1;2;3;4</textarea> then when I show this value I break them and play the <li>, this?

  • No. textarea It doesn’t even make sense to exist. You can store the ingredient list within a JS structure or even structure the list with HTML correctly.

  • From what I understand, you are putting in the textarea to go displaying to the user, but you can put tbm in a <input type="Hidden" name="lista_ingredientes[]"> saving as an array and pick this field when saving and with it would be better for you to work with the data

  • Actually, they ended up helping me a lot in the other problem.... rs, Rigadão!!!!

  • I replied in the original question a much more viable solution to the problem. The one that had been presented there, and used here, is not good.

Show 5 more comments

1 answer

-2

You could do the listing by separating only with commas, it is much easier when filling the user’s textarea and easier for you to make an explosion with Trim. You can also ask the user for only one line break between the items, and the same way the comma explodes works:

ex1: " sal, pimenta, tompero, ..."
    $lista = explode(',',$_POST['texto']);

  ex2:
  sal
  pimenta
  tompero


$lista = explode('\n',$_POST['texto']);
  • I left my -1 because, in my opinion, that would be to make a gambiarra good. The solution of storing the list of ingredients in a textarea is bad enough.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.