Jump to content

Recommended Posts

I want to create a form which has a field called attribute (there are other fields in the form but its this specific one I need help with). I want it that I can input text in the attribute and then there is a button that says ‘Add a line’. I click it and it creates a new input line for attribute. I can input and add as many lines as I want. Then, when I submit the form, these attributes should all be stored in one field called attributes but each inputted text of each line should be surrounded by ‘<li> inputted text </li><br>’ so that it is stored in the db with the bullets and break line so that when outputted, it would be a bullet list.

I am having trouble creating the new lines and how I would be able to store the multiple fields into one single field in the db and adding the <li></li><br> tags.

Link to comment
https://forums.phpfreaks.com/topic/129698-add-new-line-array-phpmysql/
Share on other sites

this isn't that hard to do with PHP-alone and once you're more comfortable adding some nice js effects to it will make it nice and sexy  ;D

 

*disclaimer: using tables and otherwise frowned-upon design techniques and also NO VALIDATION (or any error checking for that matter) to illustrate the general concept...

 

sorry im writing this directly in the forum textarea so no echos.  if you copy and paste into an IDE it'll hopefully make sense and probably have a syntax error or two.... apologies in advance if you find this confusing, ill be happy to clarify if necessary!!

 

<?php
session_start();
// initialize variable
$_SESSION['values'] = array();

// handle posts
if($_POST['addline']){
    $_SESSION['values'][] = $_POST['input'.(string)count($_SESSION['values'])];
}elseif($_POST['save']){
    // prepare input for db
    // start list
    $str = '<ul>';
    // loop through inputs and add items to list
    foreach($_SESSION['values'] as $value){
        $str.= '<li>'.$value.'</li>'; // no need for <br> to achieve list format but add if u want
    }
    // done adding items, end list
    $str.= '</ul>;

    // INSERT $str INTO THE DATABASE...  MAYBE SOMEONE ELSE CAN TAKE OVER FROM HERE
}else{ ?>
<form method='post'>
<table>
<!-- loop through and show all current values and if none are available just one blank field -->
<?php foreach($_SESSION['values'] as $key => $val) { ?>
<tr>
   <td colspan="2"><input type="text" name="input<?= $key; ?>" value="<?= $val; ?>"/></td>
</tr>
<?php } ?>
<!-- ok looping done, show buttons -->
<tr>
    <td><input type="submit" name="addline" value="add line!" /></td>
    <td><input type="submit" name="save" value="save to db" /></td>
</tr>
</table>
</form>
<?php } ?>

 

this has a 80% chance of working :)  if you copy and paste into a proper IDE the HTML/PHP seperation will become clearer and you might find a few syntax errors.

 

i hope you understood the general concept! :)

 

now for improvements:

 

- validation, always

- in my experience count() sometimes returns unexpected results with empty arrays so check the documentation and maybe use the "type and value" operator

===

to check for an empty array and adjust accordingly

- this is important because the names of the text inputs increment with the array and if they don't match up for whatever reason the results will be inaccurate/incomplete.

- you're gonna have to at the very least apply htmlentities() to $str before inserting it into the database

- consequently, when you retrieve the results from the database you're gonna have to apply html_entity_decode() to the results to get the html entities back to their original form

 

+ many other possibilities

 

hope i didnt fudge up too much as far as the syntax goes doing this notepad style but i think its pretty accurate, also my bad about the weird switching back and forth between php and html (if its new to you then cool, a bonus tidbit of info, but really you should avoid it because in certain situations it might output extra whitespace which could mess things up

 

and yeah this post was long enough.... so no DB freebie for you!  :P;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.