Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP Form implode / explode

I am using the a db field merchant_sku_item in a form. the original value is separated by / in the db like this:

2*CC689/1*CC368-8/1*SW6228-AB

I want to display in a text area on each line so I tried like this:

   <textarea name="merchant_sku_item" rows="5" class="form-control" id="merchant_sku_item"><?
               $items=explode('/',$merchant_sku_item); 
                foreach($items as $item){
                    echo $item."\r\n";
                    } 
    ?></textarea>
    

All works fine:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2*CC689
1*CC368-8
1*SW6228-AB

but when I post the form I get a value like this:

2*CC689 1*CC368-8 1*SW6228-AB

but I wan’t it back in the original format to update the DB in the correct format:

2*CC689/1*CC368-8/1*SW6228-AB

I tried to implode it with the / but I think it’s just one string now so it’s not working. I could replace the spaces I guess but this will not work if the field contains spaces.

Could somebody please tell me the best way to handle this?

>Solution :

Try replacing the created spaces and removing newline characters like so:

<?php
   $merchant_sku_item = str_replace("\r\n","/",trim($_POST["merchant_sku_item"]));
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading