0

I am trying to make a form with editable labels using Json, PHP, and jQuery.

I have got my PHP form reading in the questions from the Json file.

I have taken the output of the form, serialized it as Json, and passed it to another PHP file.

Once there, I want to take my json string, and replace bits of it so that it matches my original json layout, and then write it back to the file.

If the below explanation is too long, jump to Step 5 for the actual problem.

Step 1. Here is the json that gets read into the form (obviously there are more questions than this in the real file):

{
    "servers": {
        "questions": [
            {
                "server": "server1",
                "question":"Is the server running?",
                "helptext":"It should be",
                "answers": "Yes, No"
            },
             {
                "server": "server2",
                "question":"Is the server running?",
                "helptext":"It should be",
                "answers": "Yes, No"
            }
        ]
    }
}

Step 2. Here's where I import the file:

// copy file content into a string var
$json_file = file_get_contents('data/questions2.json');

// convert the string to a json object
$jfo = json_decode($json_file);

// read the title value
//$servers = $jfo->servers->title;

// copy the posts array to a php var
$questions = $jfo->servers->questions;

Step 3. This is my PHP for outputting the form fields (done like this so as to get an array for each question).

<?php 
            foreach ($questions as $question) 
            {
                $id++;
                echo '<tr>';
                echo '  <td>';
                echo '      <label for="server'.$id.'">Server</label><br>';
                echo '      <input name="question'.$id.'[]" type="hidden" id="serverlabel'.$id.'" value="server"/>';
                echo '      <input name="question'.$id.'[]" id="server'.$id.'" value="'.$question->server.'"/>';
                echo '  </td>';
                echo '  <td>';
                echo '      <label for="question'.$id.'">Question '.$id.'</label><br>';
                echo '      <input name="question'.$id.'[]" type="hidden" id="questionlabel'.$id.'" value="question"/>';
                echo '      <input name="question'.$id.'[]" id="question'.$id.'" size="70" value="'.$question->question.'"/>';
                echo '  </td>';
                echo '  <td>';
                echo '      <label for="helptext'.$id.'">Helptext '.$id.'</label><br>';
                echo '      <input name="question'.$id.'[]" type="hidden" id="helptextlabel'.$id.'" value="helptext"/>';
                echo '      <input name="question'.$id.'[]" id="helptext'.$id.'" size="70" value="'. $question->helptext . '"/>';
                echo '  </td>';
                echo '  <td>';
                echo '      <label for="answers'.$id.'">Answers to Question '.$id.'</label><br>';
                echo '      <input name="question'.$id.'[]" type="hidden" id="answerslabel'.$id.'" value="answers"/>';
                echo '      <input class="help" name="question'.$id.'[]" id="answers'.$id.'" value="'.$question->answers.'">';
                echo '  <td>';
                echo '          <div class="helptext leftpoint">Comma separated list</div>';
                echo '  </td>';
                echo '</tr>';
            }

                ?>  

Step 4. I then use jQuery & Ajax to post the form data to the other PHP file.

I successfully converted the form data to a Json string as follows:

{
  "question1": [
    "server",
    "server1",
    "question",
    "Is the server running?",
    "helptext",
    "It should be",
    "answers",
    "Yes, No"
  ],
  "question2": [
    "server",
    "server2",
    "question",
    "Is the server running?",
    "helptext",
    "It should be",
    "answers",
    "Yes, No"
  ]
}

Step 5. And now I want to make the above json back into my original json format so I can write it back to the file, so I did this preg_replace(), but $newJson is empty.

//replace numbered questions with single "question" label
// make hidden form data back into json labels
$patterns = array();
$patterns[0] = '/(question\d)/g';
$patterns[1] = '/"server",/';
$patterns[2] = '/"question",/';
$patterns[3] = '/"helptext",/';
$patterns[4] = '/"answers",/';
$replacements = array();
$replacements[0] = 'question';
$replacements[1] = '"server":';
$replacements[2] = '"question":';
$replacements[3] = '"helptext":';
$replacements[4] = '"answers":';

$newJson = preg_replace($patterns, $replacements, $json);

I used this documentation http://php.net/manual/en/function.preg-replace.php and the wonderful regex validation tool, https://regex101.com/

What am I doing wrong in Step 5?

(sorry if my question is too long, but hopefully it will give you an idea of what I am trying to do and why, and also, as my solution was pieced together from various other StackOverflow pages, it might help someone)

3
  • 1
    There is no g modifier in PHP so that could be an issue. Commented Jan 20, 2016 at 17:12
  • 3
    This is such an X/Y problem. Why not read the JSON on the client and generate there? Saves 2 or 3 steps. the original JSON is even from the same server so no origin issues Commented Jan 20, 2016 at 17:18
  • Because I tried using Angular and couldn't get further JavaScript events to load after the Json. (See numerous other StackOverflow questions about getting events to fire on the Angular equivalent of document.onload, and the numerous conflicting answers). Commented Jan 21, 2016 at 9:05

1 Answer 1

1

changing this line:

$patterns[0] = '/(question\d)/g';

to

$patterns[0] = '/(question\d)/';

Fixes your issue. Besides the g PCRE modifier not existing in PHP, it doesn't make sense in this context. The documentation says (emphasis mine),

each pattern will be replaced by the replacement counterpart

So preg_replace won't just stop replacing after it replaces question1 with question. Rather, it continues replacing all instances of /question\d/ with question, which is what the g modifier does in Perl (but not PHP) anyways.

You can see the results at the PHP sandbox here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that worked beautifully. Now I just have to figure out how to make the Json valid (I'm using jsoneditoronline.org ) but your regex worked like a dream. Thanks for the PHP sandbox link too, that was great.

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.