-2

PHP Class

<?php

class food {
    public function __construct ($parameter){
        $path = array("something") ;
        $this->getPath($path);
    }
    protected function getPath ($array){
        $json = json_encode($array);
    }
}

$print = new food(x);

HTML

<!-- ... -->
<form name="form1" action="" method="GET" >
 <input type="text" id="hiddenbox" value="<?php $json ?>" />
</form>
<!-- ... -->

I want to load the $json value into the textbox. However, the value is not displayed. How do I get the return of $this->getPath() into the textbox?

0b10011
  • 18,397
  • 4
  • 65
  • 86
known reg
  • 47
  • 1
  • 2
  • 8

2 Answers2

0

First of all, your class name is $print, and not $json. Secondly to run a function in a class you have to write $print->getPath($array) (and thus also define that $array somewhere). And third, you don't echo anything in getPath() function, so you can't just write $json or the function call there, you need to either have getPath 'echo' it or return $json variable.

In short? This question is horribly formatted.

Shoe
  • 74,840
  • 36
  • 166
  • 272
kingmaple
  • 4,200
  • 5
  • 32
  • 44
0

I assume you are wanting to create a field $json in your food class. Here is how your getPath function should look:

protected function getPath ($array){
    $this->json = json_encode($array);
}

You will then want to replace your value ="<?php $json ?>" with the following (using the $print object you instantiated): value ="<?php echo htmlspecialchars($print->json); ?>". The htmlspecialchars bit is especially important as it "converts special characters to HTML entities".

adamdunson
  • 2,635
  • 1
  • 23
  • 27