Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:23:34+00:00 2026-06-18T01:23:34+00:00

I have a form with fields and a text-area that allows any characters to

  • 0

I have a form with fields and a text-area that allows any characters to be entered. I can’t just submit the form, because the form is being recycled many times over, so the form values are being stored in associative arrays:

<form name='Theform'>

    <input type="text" id="VISITOR_DETAILS_NAME" value="Joe">
    <input type="text" id="VISITOR_DETAILS_SIZE" value="Large">
    <textarea id='VISITOR_DETAILS_INFO'>
       User can enter anything here including double " and single ' quotes
    </textarea>
<input type="hidden" name="package" id="package" value="" />

</form>

The text-area value are stored in a JavaScript array along with the other form values:

myArray[0]['VISITOR_DETAILS_NAME'] = document.getElementById('VISITOR_DETAILS_NAME').value;
myArray[0]['VISITOR_DETAILS_SIZE'] = document.getElementById('VISITOR_DETAILS_SIZE').value;
myArray[0]['VISITOR_DETAILS_INFO'] = document.getElementById('VISITOR_DETAILS_INFO').value;

I end up with an array something like this:

{
VISITOR_DETAILS_NAME : "Joe",
VISITOR_DETAILS_SIZE : "Large",
VISITOR_DETAILS_INFO : "User can enter anything here including double " and single ' quotes"
};

I then pass this JavaScript array to the hidden form field using JSON.stringify and then POST this to PHP:

document.getElementById('package').value = JSON.stringify(myArray[0]);
Theform.submit();

(For now I’m just posting to an iframe to test that the JSON is passing the JavaScript arrays properly through POST).

When I get it on the PHP side – it seems good to go. It looks like the JSON.stringify has added the backslash to the double quote (\” ) – and now I want to store the values in MySQL. But I want to first test that I can send/reconstruct the JSON back to the javascript as an array – so I try this:

parent.myArray[0] = JSON.parse('<?php echo $_POST['package']; ?>');

I get an ERROR: SyntaxError: Expected token ‘)’ OR SyntaxError: missing ) after argument list


This is strange to me – because when I try it without POSTING – It seems to work fine like this:

document.getElementById('package').value = JSON.stringify(myArray[0]);

now if I try to just pass back the stringified value back to the array

myArray[0] =  JSON.parse(document.getElementById('package').value);

– it seems to work fine – no errors


QUESTIONS:

  • Why am I getting this error when trying to reconstruct the ARRAY from the
    POSTED JSON.stringify() value?
  • Do I save this JSON.stringify() value in MySQL as is?
  • Or do I PHP json_decode() it first?

I want to grab the form data – handle it properly – store it in MySQL and then read it back into the form when I need it.

Thanks All 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:23:35+00:00Added an answer on June 18, 2026 at 1:23 am
    parent.myArray[0] = JSON.parse('<?php echo $_POST['package']; ?>');
    

    Here you are are trying to convert a JSON text into an HTML representation of a JavaScript string representation of a JSON text, but you aren’t doing anything to escape it for either.

    If you have any ' characters in the JSON data, then they will terminate the JavaScript string.

    If you have any " characters in the JSON data, then they will be represented as \", but \" is a JavaScript string representation of ". Since you don’t do anything to escape the text you put in the JS string, the slash character will be consumed by the JavaScript parser and will be gone before it reached the JSON parser.

    If you want to convert data for placing in a JavaScript string then you need to escape it.

    However, JSON is a subset (almost) of JavaScript. So the process of converting a JSON text to a JavaScript string so it can be parsed into a JavaScript object is over-complicated. You can skip that can just go straight to:

    <script>
    var foo = <?php echo $json; ?>
    </script>
    

    However, since you are taking in the JSON from the client, echoing out directly will expose you to XSS attacks. In order to deal with this you should filter the data on the server.

    This will:

    • Fail to parse any invalid JSON and so not output bad JSON (but it might output nothing, giving you a JSON syntax error, you should apply tests to see if the parse was successful and output a sensible default case if it fails).
    • Convert any </script> in the data to <\/script> making it safe to place in a script element (because that is how PHP’s json_encode works

    Such:

    <!-- I don't do PHP, this is untested -->
    <script>
    var foo = <?php
        $unsafe_json = $_POST['package'];
        $data_structure = json_parse($unsafe_json);
        $safe_json = json_encode($data_structure);
        echo $safe_json;
    ?>;
    </script>
    

    Do I save this JSON.stringify() value in MySQL as is? Or do I PHP json_decode() it first?

    That depends on what you intend to do with the data. In general when putting things into a database it is a good idea to extra the data from the data format and normalize it. That way you can run queries over it.

    If you are only going to store the data and then retrieve it, you might be able to get away with not doing that and storing strings of JSON in the database. That loses you a lot of flexibility though and might bite you in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a multipart form that has fields for text, integers and file
I have an order form with about 30 text fields that contain numerical values.
I have form in html where user can put the text in text area.
i have a simple form in my view that has a text area :
so I have a form that I have multiple input text fields as well
I have a form with different html input fields... 1) <input type=text> 2) <textarea></textarea>
I have problem with TinyMCE editor. I have form with few text fields and
I have a form with two input text fields: <input id=ModelName_test_0 name=ModelName[test][0] type=text> <input
I have one form with several text fields and one button. When i enter
I have a form with 3 text fields and 3 checkboxes. I had implemented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.