Forum

Thread tagged as: Question, Forms

retrieve the value of a select field in the perch2_form_resonses table

Hi

is it possible using SQL to retrive the value of a select box in a form

I know you can get the JSON strin but is there a way of just getting the entered value

so in my case i have a select box for an Australian state

<div class="state">
<perch:label for="state">State:</perch:label>
<perch:input type="select" options="VIC, NSW, QLD, SA, WA, NT, TAS " id="state" label="State" required="true" placeholder="Select"/>
</div>

which ends up in the database in the perch2_form_responses table and the responseJSON column as

{"fields":{"state":{"attributes":{"type":"select","options":"VIC, NSW, QLD, SA, WA, NT, TAS ","id":"state","label":"State","required":"true","placeholder":"Select"},"value":"NT"}},"files":[],"page":{"id":"1","title":"Home page","path":"\/","navtext":"Home page"}}

can i access the value:NT part as i am trying to do a report for a customer.

Regards,

Brett

Brett Warne

Brett Warne 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, use json_decode() to turn the string into an array.

Hi Drew

Thanks for the above,

I can get the data from the file by

 $sql = "SELECT responseID, responseJSON FROM `perch2_forms_responses` ORDER BY responseID DESC LIMIT 1" ;
    $q = $conn->query($sql);
    $q->setFetchMode(PDO::FETCH_ASSOC);

and then

 <tbody>
 <?php while ($r = $q->fetch()): ?>
 <tr>
   <td><?php echo htmlspecialchars($r['responseID'])?></td>
 <td><?php echo htmlspecialchars($r['responseJSON'])?></td>
 <td><?PHP


?>
 </td>

which gives the whole string

122     {"fields":{"state":{"attributes":{"type":"select","options":"VIC, NSW, QLD, SA, WA, NT, TAS ","id":"state","label":"State","required":"true","placeholder":"Select"},"value":"QLD"}},"files":[],"page":{"id":"1","title":"Home page","path":"\/","navtext":"Home page"}}

i then try and get the value by

<?php
 var_dump(json_decode($json, true));

which gives me

array (size=3)
  'fields' => 
    array (size=1)
      'state' => 
        array (size=2)
          'attributes' => 
            array (size=6)
              ...
          'value' => string 'QLD' (length=3)
  'files' => 
    array (size=0)
      empty
  'page' => 
    array (size=4)
      'id' => string '1' (length=1)
      'title' => string 'Home page' (length=9)
      'path' => string '/' (length=1)
      'navtext' => string 'Home page' (length=9)

i then tried

 echo $json->fields->state->attributes->value;

but get "trying to get property of non object"

how do i get the value of QLD out of the array

Regards,

Brett

hi Drew

i can get it with

$json=$r['responseJSON'];

 $myarray= json_decode($json, true);


 $result = ($myarray["fields"]["state"]["value"]);

 echo $result;

is this the best way

regards,

Brett

Drew McLellan

Drew McLellan 2638 points
Perch Support

This isn't really anything to do with Perch at this point - if it's working then it sounds like you have a solution.

Thanks

As you build more complex sites you just need to learn a bit more about how perch stores its data.

You can close this now.

Regards,

Brett