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)
gmodifier in PHP so that could be an issue.