Passing variable from select element to JSON update function - PHP


#1

I'm trying to update individually loaded .json files according to whichever option in the <select> is chosen. It loads the .json file and then updates an individual file fine as long as I define $myFile as a specific .json file such as $myFile = "data/data.json";

But when I attempt to pass the select option as the filename, nothing saves. Where am I messing this up?

Here is the HTML select:

<form action="/server/php/data/process.php" method="post">
    <select class="ui mini fluid search dropdown" id="templateSelection" type="text" name="selectTemplate" onchange="loadTemplate()">
        <option value="" selected="selected">Select Template</option>
        <?php
            foreach(glob(dirname(__FILE__) . '/server/php/data/*') as $filename){
                $filename = basename($filename);
                echo "<option value='" . $filename . "'>".$filename."</option>";
            }
        ?>
   </select>
</form>

Here are the options that get populated:

<option value="data.json">data.json</option>
<option value="data2.json">data2.json</option>
<option value="data3.json">data3.json</option>

And here is process.php:

<?php

//$myFile = "data/data.json";
$filename = $_POST['selectTemplate'];
$myFile = "data/" . $filename;
$arr_data = array(); // create empty array
try
{
    //Get form data
    $formdata = array(
        'ID'=> $_POST['ID'],
        'attributeName'=> $_POST['attributeName'],
        'valueX'=> $_POST['valueX']
    );
    //Get data from existing json file
    $jsondata = file_get_contents($myFile);
    // converts json data into array
    $arr_data = json_decode($jsondata, true);
    $updateKey = null;
    foreach ($arr_data as $k => $v) {
        if ($v['ID'] == $formdata['ID']) {
            $updateKey = $k;
        }
    }
    if ($updateKey === null) {
        array_push($arr_data,$formdata);
    } else {
        $arr_data[$updateKey] = $formdata;
    }

    $jsondata = json_encode($arr_data);

    //write json data into data.json file
    if(file_put_contents($myFile, $jsondata)) {
        echo 'Data successfully saved';
    }
    else
        echo "error";
}
catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

EDIT: I have the console outputting the variable values now. $filename is outputting as null, and $myFile is outputting as data/. So it seems like $filename = $_POST['selectTemplate']; is not picking up the value from the <select>


#2

Are you sending the data using Ajax, as you have the onchange specification? If so, can you post that part too? I can't otherwise see why the $_POST variable you are using would be null.


#3

I'm submitting it (with a submit button) via another form on the same page with the same action. This other form allows me to change some of the values of the json that is loaded when an option in the first form is selected. Here is what this second form looks like:

<form class="ui form" action="server/php/process.php" method="POST" target="frame">
....
<input type="submit" value="Save">
</form>

Perhaps that is where the issue is coming from? I figured a submit on this 2nd form would pick up the selected option in the first form and store the option in a variable.


#4

No, I don't think that will work - the form will only submit stuff that's within those form tags. Otherwise if you had multiple forms on a single page to do entirely separate tasks, it would get confusing.

You might be able to code around it with some JavaScript to get the values from the other form, combine the two and submit them.


#5

Thanks for the input!...it helped me solve the issue. I ended up copying the value of the selected option (in the first form) and inserted it onchange into a hidden input in the second form. That way, upon submit, it's able to pick up the value and then store it in a variable.


#6

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.