NYCPHP Meetup

NYPHP.org

[nycphp-talk] Automatically create an object from a form

Dan Cech dcech at phpwerx.net
Tue Oct 19 11:46:13 EDT 2004


Christopher Merlo wrote:
> Hello, folks.  I'm not sure that what I want to do is possible.  If it
> is, I'm sure it takes a lot more JavaScript than I know (which is
> precious little).
> 
> PHP 4.3.2 on Apache on Linux.  I would like to create an object of a
> PHP class by choosing an element from an HTML select drop-down box,
> and using the selected value to specify a parameter to the object's
> constructor.  (Then, if the selection changes, throw out the old
> object, and create a new one in its place, with the newly-selected
> value being sent to the constructor.)
> 
> Can I do this?  TIA,
> -c
> 

I'm not sure if this is what you mean, but here is a simple example:

Dan

<?php

if (isset($_REQUEST['param'])) {
   $_SESSION['myobj'] = new myclass($_REQUEST['param']);
}

class myclass
{
   var $value;

   function myclass($value)
   {
     $this->value = $value;
   }
}

?>
<html>
   <head>
     <title>Test</title>
   </head>
   <body>
     <form>
       <select name="param">
         <option value="1">1</option>
         <option value="2">2</option>
       </select>
       <input type="submit" value="Submit" />
     </form>
     <pre>
<?php
   if (isset($_SESSION['myobj'])) {
     print_r($_SESSION['myobj']);
   } else {
     print 'hit submit to create an object';
   }
?>
     </pre>
   </body>
</html>



More information about the talk mailing list