2

I have write a code to create a select with PHP Object which is:

<?php

class Modulo  {

    var $nD;
    var $nC;
    var $nA;

    function __construct($nD,$nC,$nA) {
        $this->nD=$nD;
        $this->nC=$nC;
        $this->nA=$nA;
    }
    function toString(){
        return $this->nD."-".$this->nC."-".$this->nA."<br>";
    }
}

?>

Here is the method to build the select:

function loadModuleValid(){
    $mod1 = new Modulo(4,3,3);
    $mod2 = new Modulo(5,3,2);
    $mod3 = new Modulo(4,4,2);
    $mod4 = new Modulo(5,4,1);
    $mod5 = new Modulo(4,5,1);
    $mod6 = new Modulo(3,5,2);
    $mod7 = new Modulo(3,4,3);
    $mods = array($mod1,$mod2,$mod3,$mod4,$mod5,$mod6,$mod7);
    $i = 0;
    print "<h3>Seleziona il modulo</h3>";
    print "<select id=\"d1\">";
    foreach ($mods as $key ) {

                  print "<option value=\"{$i}\">{$key->toString()}</option>";
                $i++;
    }
     print "</select>";

}

Now I have to retrive the selected object by the user when the she/he presses a button. How can I do ? I want the actual object not just it's value !

3
  • 1
    You can't pass an object through a form. Commented Feb 11, 2015 at 13:42
  • you cant work with an indentifier for you object Commented Feb 11, 2015 at 13:44
  • 1
    You could serialize it I guess and retrieve it later down the line, after the from. Not exactly sure what you're trying to achieve. Commented Feb 11, 2015 at 13:44

2 Answers 2

1

You can't pass an object through a form... well you could if you were to serialize it, pass the string and then unserialize it but you don't want to do that.

What you want to do is reference the object with an identifier:

function loadModuleValid(){
    array(
       'mod1' => new Modulo(4,3,3),
       'mod2' => new Modulo(5,3,2),
       // your other objects
    )
   
    $i = 0;
    print "<h3>Seleziona il modulo</h3>";
    print "<select id=\"d1\">";
    foreach ($mods as $key => $object ) {

                  print "<option value=\"{$key}\">{$object->toString()}</option>";
                $i++;
    }
     print "</select>";

}

Then on the backend you would fetch the object based on mod1 or whatever the array key is. However this requires creating this array of objects in a place where they can be fetched again.

So let's do an example of that:

function getModulos($id = null) {
    $mods = array(
       'mod1' => new Modulo(4,3,3),
       'mod2' => new Modulo(5,3,2),
       // your other objects
    );

    if (null !== $id) {
       return isset($mods[$id]) ? $mods[$id] : false;
    } else {
      return $mods;
    }
}


function loadModuleValid(){
    $mods = getModulos();

    $i = 0;
    print "<h3>Seleziona il modulo</h3>";
    print "<select id=\"d1\" name=\"modulo\">";
    foreach ($mods as $key => $object ) {

                  print "<option value=\"{$key}\">{$object->toString()}</option>";
                $i++;
    }
     print "</select>";

}

// this would be an example of your form handling

if (isset($_POST['modulo'])) {
   $modulo = getModulos($_POST['modulo']);
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is a dark tricks, you can serialize your object :

print "<option value=\"" . serialize($key) . "\">{$key->toString()}</option>";

Then unserialize it to retrieve your object :

$mod = unserialize($_POST['yourSelectName']);

Comments

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.