SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast nickweavers's Avatar
    Join Date
    Feb 2004
    Location
    Hampshire, UK
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why, when my json gets stored in the DOm does it get changed?

    I have some html that uses some javascript to take a JSON object and store it in a hidden form field.
    Code:
    <a href='javascript:submitForm(<?php echo $json_sort_fields; ?>)'
        title="Click to sort by this column">
        <?php echo $fieldAttribute->Field; ?>
    </a>
    The javascript looks lik this:
    Code:
    <script type="text/javascript">
    <!--
      function submitForm(json_sort_fields) { 
      	var thisForm = document.adminForm;
      	thisForm.json_sort_fields.value = "'" + json_sort_fields + "'";
      	thisForm.submit(); 
      } 
    // -->
    </script>
    When I debug using Firebug I see the that the contents of the variable json_sort_fields are now an object and when assigned to the form field thisForm.json_sort_fields and subsequently retrieved from $_POST by PHP all I see is "[object Object]". How can I preserve the JSON string so that it gets through to my PHP ?

    TIA,
    Nick.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The quotes are missing so the function thinks the parameter is a reference instead. Use this instead

    Code html4strict:
    <a href="javascript:submitForm('<?php echo $json_sort_fields; ?>')"
    ...
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast nickweavers's Avatar
    Join Date
    Feb 2004
    Location
    Hampshire, UK
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul,

    I had tried that but found that although everything looked good to me in the html....
    Code:
    <a href="javascript:submitForm('{"owner":"DESC"}')"
      title="Click to sort by this column">
      owner
    </a>
    Firebug reported this error during parsing:
    Code:
    unterminated string literal
    javascript:submitForm('{
    Line 1
    It looks like the second pair of double quotes is terminating the string even though it is enclosed in single quotes. I suppose I could try addslashes.

    Nick.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You're trying to next too many quoted parts. Use a separate function to do the job for you, and for pete's sake, don't use the javascript: protocol. That was originally to let you do the following

    Code html4strict:
    <a href="javascript:<p>Thank you for that.</p>">Click here</a>

    But it got taken over, badly for scripting. Instead of that, you might be better suited to use the onclick attribute instead.


    Code html4strict:
    <a href="#" onclick="submitDescForm()"
      title="Click to sort by this column">
      owner
    </a>

    Code javascript:
    submitDescForm() {
        submitForm('{"owner":"DESC"}');
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •