0

i have made some random input names, because its a drag and drop page builder, so i can't guess, how much elements user will use, so i have created a random input names, for that am using php foreach loop for $_POST requests. i have tried to make it encoded into json and then later save it into database. but it looks like something is wrong in my json. Here are my html demo codes :

<input style="display:none;" name="DATA-BLOCK-A(some random string)">
<input style="display:none;" name="DATA-BLOCK-B(some random string)">

PS: A is for selecting element A, and B is for element B.

here is my PHP code :

if (isset($_POST)) {
    //$arr = array();
foreach($_POST as $key => $value)
    {

     $arr = array($key => $value);
     $encode = json_encode($arr);
    echo $encode;
    }


}

and Here is the result :

{"sortlist":"block[]=D5e3385b75a75d&block[]=K5e3385b85a75e&block[]=C5e3385b95a75f&block[]=F5e3385ba5a760"}{"save_cont_flag":"0"}{"DATA-block-D5e3385b75a75d":"0#TRANSP

<\/p>"}{"DATA-block-K5e3385b85a75e":"0#TRANSP20"}{"DATA-block-C5e3385b95a75f":"01#TRANSP0images\/250place.jpg\u00b8"}{"text-1573532276681":""}{"textarea-1573532278320":""}{"DATA-block-F5e3385ba5a760":"121212unundefined"}{"page_name":"123"}{"aff_link":""}{"pause_link":""}{"seo_title":""}{"fbook":""}{"seo_desc":""}{"seo_keywords":""}{"back_color":"#EEEEEE"}{"body_color":"#FFFFFF"}{"back_image":""}{"ty_font_color":"#000000"}{"ty_override":""}{"ty_name":"12314"}{"ty_stm":""}{"modal_para_width":"0"}{"catcha_url":""}{"catcha_un":"Yes"}{"catcha_message":""}{"code_head":""}{"code_body":""}{"modal_share_width":"0"}{"modal_cta_width":"0"}{"modal_video_width":"0"}{"modal_mp_width":"0"}{"modal_stm_width":"0"}{"modal_image_width":"0"}{"modal_bonus_width":"1"}{"ty_headline":""}{"modal_spacer_width":"0"}{"att_bar_status":"0"}{"att_delay_in":"0"}{"att_bar_color":"#bbbbbb"}{"att_gradient":"0"}{"att_text_color":"#000000"}{"att_text_font":"Open Sans:400"}{"att_text_size":"14"}{"att_bar_message":"Add Your Attention Bar Text Here"}{"att_link_color":"#000000"}{"att_link_label":"Add Link Text Here"}{"att_link_url":"http:\/\/commissiongorilla.com"}{"count_font":"Open Sans:800"}{"count_size":"55"}{"count_status":"0"}{"count_type":"0"}{"count_end":"01\/31\/2020 6:41 AM"}{"count_zone":"0.0"}{"count_eg_days":"0"}{"count_eg_hours":"0"}{"count_eg_mins":"0"}{"count_digit_color":"#bbbbbb"}{"count_label_color":"#bbbbbb"}{"count_background":"0"}{"count_language":"1"}{"count_exp":"0"}{"count_url":"http:\/\/commissiongorilla.com"}{"count_add_days":"0"}{"count_add_hours":"0"}{"count_add_mins":"0"}{"modal_countdown_width":"0"}{"modal_review_width":"0"}

and also how seperate all A BLOCKS and B BLOCKS? Thanks.!

2
  • Where does that <\/p> come form? Sounds like a missing quote in the form field attribute Commented Jan 31, 2020 at 8:23
  • that's the value of DATA-block-D5e3385b75a75d Commented Jan 31, 2020 at 8:29

2 Answers 2

1

You don't need to use a loop.

Just used json_encode :

$json = json_encode($_POST);

If you need to get key contain DATA-block-, you can write :

foreach ($_POST as $key => $value) {
    if (strpos($key, 'DATA-block-') !== false) {
        // Here `DATA-block-{}`
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you change the input names to DATA-BLOCK-A[] and DATA-BLOCK-B[], $_POST['DATA-BLOCK-A'] will contain an array of all a blocks and $_POST['DATA-BLOCK-B'] will contain an array of all b blocks.

This also eliminates the need for generating random strings.

1 Comment

That will not work, as you can see in the result, i am sorting the elements. like i have two blocks "block[]=D5e3385b75a75d&block[]=K5e3385b85a75e" and block D5e3385b75a75d will be at number 1 and K5e3385b85a75e will be at number 2 when i will call them from database to show contents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.