Page 1 of 1

How do I display a form as content in a class ' or " or ??

Posted: Tue May 12, 2015 9:10 am
by hybris
EDIT: SOLVED
Thanks a bunch guys!

Hi,
some year ago I did a homepage where i put the "frame" of the page as a class and for the main field i thought I'd use public $content to just type the text for the page there. Now I need to put a form as content using both html and php inside.

Code: Select all

In the class I have
Public $content;
Public $title = "MyHomePage";
/../

Public function Display()
/.../
$this->DisplayMain($this->content);
Then in the page file I have

Code: Select all

$homepage = new page();
$homepage->content="TEST";
$homepage->Display()
I might have done some spelling error above but the concept works so in this case the main area of the page will display TEST just as intended.

The problem is I have a form (with a mix of html and php) I want to put in there and it wont work.

I know I had alot of trouble with this before with when to use ' or " to "jump in and out of echo"..

Below is the code for the form (I have to strip it some since the footer and stuff is now handled by the class homepage.

Code: Select all

$homepage->content ="TEST";
/* <div id="midelement">
    <div class="topheading" align="center">Avvikelse Rapportering</div>            
    <pskapaavvikelse>            
    <div id ="Avvikelse_formular_bakgrund">                      
     <form id="<? //php echo $u_id; ?>" name="form1" method="post" action="Lagra_avvikelse_i_DB.php">                     
      <div id ="Avvikelse_visningsruta">
        <table id="dataTable" table class="fixed">
         <tr>
          <td><b>Artikel_nr: </b></td>
          <td><input name="artnr" type="text" id="artnr" style="width:200px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Kund: </b></td>
          <td><input name="kund" type="text" id="kund" style="width:200px; height:18px;" value="" /></td>
         </tr>                            
         <tr>
          <td><b>Kontaktuppg:</b></td>
          <td><input type="radio" name="kont" id="kont_val" onclick="document.getElementById('kontnamn').value = '';document.getElementById('kontnr').value = '';document.getElementById('kontmail').value = ''; document.getElementById('kont1').hidden = true;document.getElementById('kont2').hidden = true;document.getElementById('kont3').hidden = true; " value="0" checked> Nej <input type="radio" name="kont" id="kont_val" onclick="document.getElementById('kont1').hidden = false;document.getElementById('kont2').hidden = false;document.getElementById('kont3').hidden = false; " value="1"> Ja<br></td>
         </tr>                        
         <tr id="kont1" hidden>
          <td><b>Kundens namn: </b></td> 
          <td><input name="kontnamn" type="text" id="kontnamn" style="width:200px; height:18px;" value="" /></td>
         </tr>
         <tr id="kont2" hidden>
          <td><b>Telefon nr: </b></td> 
          <td><input name="kontnr" type="text" id="kontnr" style="width:200px; height:18px;" value="" /></td>
         </tr>
         <tr id="kont3" hidden>
          <td><b>Email:</b></td>
          <td><input name="kontmail" type="text" id="kontmail" style="width:200px; height:18px;" value="" /></td>
         </tr>                                                        
         <tr>
          <td><b>Grossist: </b></td>
          <td><input name="grossist" type="text" id="grossist" style="width:200px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Producerad: </b></td>
          <td><input name="prodar" type="text" id="prodar" style="width:30px; height:18px;" maxlength="4" min="2011" max="2100" value="" /> - <input name="prodman" type="text" id="prodman" style="width:15px; height:18px;" maxlength="2" min ="01" max ="12" value="" /> - <input name="proddag" type="text" id="proddag" style="width:15px; height:18px;" maxlength="2" min ="01" max="31" value="" /> (aaaa-mm-dd)</td>
         </tr>
         <tr>
          <td><b>Bastfore: </b></td>
          <td><input name="bastfar" type="text" id="bastfar" style="width:30px; height:18px;" maxlength="4" min="01" value="" /> - <input name="bastfman" type="text" id="bastfman" style="width:15px; height:18px;" maxlength="2" value="" /> - <input name="bastfdag" type="text" id="bastfdag" style="width:15px; height:18px;" maxlength="2" value="" /> (aaaa-mm-dd)</td>                            
         </tr>
         <tr>
          <td><b>Batch: </b></td>
          <td><input name="batchnr" type="text" id="batchnr" style="width:120px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Fabrik: </b></td>
          <td><input name="fabrikskod" type="text" id="fabrikskod" style="width:120px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Mangd: </b></td>
          <td><input name="mangd" type="text" id="mangd" size="5" value="" /><input type="radio" name="enhet" value="Kg" checked> Kg <input type="radio" name="enhet" value="Kolli"> Kolli</td>
         </tr>
         <tr>
          <td><b>Allvarlighet:<b></td>
          <td> (lag)<input type="radio" name="allvarlighet" value="1" checked> 1<input type="radio" name="allvarlighet" value="2"> 2<input type="radio" name="allvarlighet" value="3"> 3<input type="radio" name="allvarlighet" value="4"> 4<input type="radio" name="allvarlighet" value="5"> 5 (mkt hog)</td>
         </tr>
         <tr>
          <td><b>Beskriv avvikelsen i detalj:</b></td>
          <td><textarea name="beskrivning" cols="30" rows="3" id="beskrivning"></textarea></td>
         </tr>
         <tr>
          <td><b>Sagt till kund:</b></td>
          <td><textarea name="sagttillkund" cols="30" rows="3" id="sagttillkund"></textarea></td>
         </tr>
         <tr>
          <td><b>Skall det krediteras:</b></td>
          <td><input type="radio" name="krediteras" id="kredit_val" onclick="document.getElementById('kredit_kostnad').value = '';document.getElementById('v_k').value = '';document.getElementById('kred_besk').value = ''; document.getElementById('kred1').hidden = true;document.getElementById('kred2').hidden = true;document.getElementById('kred3').hidden = true; " value="Nej" checked> Nej<br><input type="radio" name="krediteras" id="kredit_val" onclick="document.getElementById('kred1').hidden = false;document.getElementById('kred2').hidden = false;document.getElementById('kred3').hidden = false; " value="Ja"> Ja<br></td>
         </tr>    
         <tr id="kred1" hidden>
          <td><b>Kreditmangd: </b></td>
          <td><input name="kredit_kostnad" type="text" id="kredit_kostnad" style="width:120px; height:18px;" value="" /><input type="radio" name="kenhet" id="kenhet" value="Kg" checked> Kg <input type="radio" name="kenhet" id="kenhet1" value="Kolli" > Kolli<input type="radio" name="kenhet" id="kenhet" value="Sek" > Sek</td></td>
         </tr>
         <tr id="kred2" hidden>
          <td><b>Vem krediterar: </b></td> 
          <td><select name="vem_krediterar" id ="v_k" size="1" ><?php
              foreach ($userarray as $value) { ?>
               <option value="<?php echo $value[0]; ?>"><?php echo $value[1]; echo " - "; echo $value[3];?></option> 
               <?php } ?>                   
              </select></td>
         </tr>
         <tr id="kred3" hidden>
          <td><b>Beskrivning</b></td>
          <td><textarea name="kred_besk" cols="30" rows="3" id="kred_besk"></textarea></td>
         </tr>
         <tr>
          <td><b>Skall det hamtas:</b></td>
          <td><input type="radio" name="hamtas" id="hamtas_val" onclick="document.getElementById('v_h').value = '';document.getElementById('hamta_var').value = '';document.getElementById('hamta_till').value = ''; document.getElementById('hamta_vad').value = '';  document.getElementById('hamta1').hidden = true;document.getElementById('hamta2').hidden = true;document.getElementById('hamta3').hidden = true;document.getElementById('hamta4').hidden = true;document.getElementById('hamta5').hidden = true; " value="Nej" checked> Nej<br><input type="radio" name="hamtas" id="hamtas_val" onclick="document.getElementById('hamta1').hidden = false;document.getElementById('hamta2').hidden = false;document.getElementById('hamta3').hidden = false;document.getElementById('hamta4').hidden = false;document.getElementById('hamta5').hidden = false; " value="Ja"> Ja<br></td>
         </tr>
         <tr id="hamta1" hidden>
          <td><b>Vem ordnar hamtning: </b></td> 
          <td><select name="vem_hamtar" id ="v_h" size="1" ><?php
              foreach ($userarray as $value) { ?>
                <option value="<?php echo $value[0]; ?>"><?php echo $value[1]; echo " - "; echo $value[3];?></option> 
                <?php } ?>                   
              </select></td>
         </tr>
         <tr id="hamta2" hidden>
          <td><b>Hamtas var: </b></td>
          <td><input name="hamta_var" type="text" id="hamta_var" style="width:250px; height:18px;" value="" /></td>
         </tr>
         <tr id="hamta3" hidden>
          <td><b>Levereras till: </b></td>
          <td><input name="hamta_till" type="text" id="hamta_till" style="width:250px; height:18px;" value="" /></td>
         </tr>
         <tr id="hamta4" hidden>
          <td><b>Vad (och hur mycket) skall hamtas:</b></td>
          <td><textarea name="hamta_vad" cols="30" rows="3" id="hamta_vad"></textarea></td>
         </tr>                                                    
         <tr id="hamta5" hidden>
          <td><b>Till lagerstatus:</b></td>
          <td><select name="hamta_till_lager_status" id ="hamta_till_lager_status" size="1" >                                
                <option value="Ordinarie_lager">Ordinarie lager</option>
                <option value="Sparrat_lager">Sparrat</option>
                <option value="Stoppat_lager">Stoppat lager</option>
              </select></td>
         </tr>
         <tr>
          <td><input type="submit" name="Submit" value="Skicka"/> </td>
         </tr>
        </table>     
       </div>
      </form>
    </div>
   </div>            
   <div id="midelement">
    <div class="midheading" align="center">Slut</div>                
   </div>
  </div>
 
  <div id="footer"></div>         
 </div>        
</body><?php

"; */


Anyone? Thanks in advance.

Re: How do I display a form as content in a class ' or " or

Posted: Tue May 12, 2015 11:55 am
by Christopher
You probably want to use output buffering:

Code: Select all

<?php
ob_start();
?>
<p>Your HTML here.</p>
<?php
$homepage->content = ob_get_contents();
ob_end_clean();

Re: How do I display a form as content in a class ' or " or

Posted: Tue May 12, 2015 12:54 pm
by requinix
Consider making the ->content be a method instead.

Code: Select all

abstract class Page {

	public function getContent() {
		ob_start();
		$this->output();
		return ob_get_clean();
	}

	public abstract function output();

}

Code: Select all

class SimplePage extends Page {

	private $content = "";

	public function getContent() {
		return $this->content;
	}

	public function output() {
		echo $this->getContent();
	}

	public function setContent($content) {
		$this->content = $content;
	}

}
Making it a method means you can jump between PHP and HTML mode:

Code: Select all

<?php

class Homepage extends Page {

	public function output() { ?>
All
your
output
goes
here
<?php	}

}

Re: How do I display a form as content in a class ' or " or

Posted: Wed May 13, 2015 1:29 am
by hybris
Sweet,

Thank You both!

I'll try both ways later today to see which works best for me.

I need to update my page but haven't had the time to code much last year (fulltime job + little kids do that to you sometimes :O) so I'm a bit rusty atm.

Re: How do I display a form as content in a class ' or " or

Posted: Wed May 13, 2015 4:47 am
by hybris

Code: Select all

<?php
ob_start();
?>
<p>Your HTML here.</p>
<?php
$homepage->content = ob_get_contents();
ob_end_clean();
This worked very good. The only problem is the cells in the table turns black for some reason. If i mark it with my mouse I can see the text (also black) is there as it should but I cant get the background for the cells to turn white.

I can change the table background (for the whole table) but not for the cells? I tried <td BGCOLOR="#ffff00"><b>Artikel_nr: </b></td> but no luck ? :O

EDIT: I really cannot get this to work. The background color in the cells is black and I cannot change it by setting a bgcolor in the <TD> which usually works for tables. I can however change the textcolor to white so I can use the table (with inverted colors) but it looks very ugly. Is this a bug or am I'm doing something wrong?

Re: How do I display a form as content in a class ' or " or

Posted: Wed May 13, 2015 2:41 pm
by Christopher
Output buffering should have no effect on something like the table background color. Check your styles like Avvikelse_visningsruta and fixed to see which one is setting the background color.

Re: How do I display a form as content in a class ' or " or

Posted: Tue May 19, 2015 6:30 am
by hybris
Hi,

thanks but I still cannot get it to work. I removed the divs in my code and I also removed fixed from the table.

I do not use black background for any tables in my styles.

I tried the page in both chrome and IE but its still black :O

In the code below I tried to make the first cell white and also make the font color white. The result is a black cell with white text.
This is slowly driving me insane :O

Code: Select all

ob_start();
?>
<p>
                 
     <form id="<? //php echo $u_id; ?>" name="form1" method="post" action="Lagra_avvikelse_i_DB.php">                     
        <table id="dataTable">
         <tr>
          <td bgcolor="#ffffff"><b><FONT COLOR="#ffffff">Artikel_nr:</FONT> </b></td>
          <td><input name="artnr" type="text" id="artnr" style="width:300px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Kund: </b></td>
          <td><input name="kund" type="text" id="kund" style="width:300px; height:18px;" value="" /></td>
         </tr>                            
         <tr>
          <td><b>Kontaktuppg:</b></td>
          <td><input type="radio" name="kont" id="kont_val" onclick="document.getElementById('kontnamn').value = '';document.getElementById('kontnr').value = '';document.getElementById('kontmail').value = ''; document.getElementById('kont1').hidden = true;document.getElementById('kont2').hidden = true;document.getElementById('kont3').hidden = true; " value="0" checked> Nej <input type="radio" name="kont" id="kont_val" onclick="document.getElementById('kont1').hidden = false;document.getElementById('kont2').hidden = false;document.getElementById('kont3').hidden = false; " value="1"> Ja<br></td>
         </tr>                        
         <tr id="kont1" hidden>
          <td><b>Kundens namn: </b></td> 
          <td><input name="kontnamn" type="text" id="kontnamn" style="width:300px; height:18px;" value="" /></td>
         </tr>
         <tr id="kont2" hidden>
          <td><b>Telefon nr: </b></td> 
          <td><input name="kontnr" type="text" id="kontnr" style="width:300px; height:18px;" value="" /></td>
         </tr>
         <tr id="kont3" hidden>
          <td><b>Email:</b></td>
          <td><input name="kontmail" type="text" id="kontmail" style="width:300px; height:18px;" value="" /></td>
         </tr>                                                        
         <tr>
          <td><b>Grossist: </b></td>
          <td><input name="grossist" type="text" id="grossist" style="width:300px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Producerad: </b></td>
          <td><input name="prodar" type="text" id="prodar" style="width:40px; height:18px;" maxlength="4" min="2011" max="2100" value="" /> - <input name="prodman" type="text" id="prodman" style="width:20px; height:18px;" maxlength="2" min ="01" max ="12" value="" /> - <input name="proddag" type="text" id="proddag" style="width:20px; height:18px;" maxlength="2" min ="01" max="31" value="" /> (aaaa-mm-dd)</td>
         </tr>
         <tr>
          <td><b>Bastfore: </b></td>
          <td><input name="bastfar" type="text" id="bastfar" style="width:40px; height:18px;" maxlength="4" min="01" value="" /> - <input name="bastfman" type="text" id="bastfman" style="width:20px; height:18px;" maxlength="2" value="" /> - <input name="bastfdag" type="text" id="bastfdag" style="width:20px; height:18px;" maxlength="2" value="" /> (aaaa-mm-dd)</td>                            
         </tr>
         <tr>
          <td><b>Batch: </b></td>
          <td><input name="batchnr" type="text" id="batchnr" style="width:300px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Fabrik: </b></td>
          <td><input name="fabrikskod" type="text" id="fabrikskod" style="width:300px; height:18px;" value="" /></td>
         </tr>
         <tr>
          <td><b>Mangd: </b></td>
          <td><input name="mangd" type="text" id="mangd" size="5" value="" /><input type="radio" name="enhet" value="Kg" checked> Kg <input type="radio" name="enhet" value="Kolli"> Kolli</td>
         </tr>
         <tr>
          <td><b>Allvarlighet:<b></td>
          <td> (lag)<input type="radio" name="allvarlighet" value="1" checked> 1<input type="radio" name="allvarlighet" value="2"> 2<input type="radio" name="allvarlighet" value="3"> 3<input type="radio" name="allvarlighet" value="4"> 4<input type="radio" name="allvarlighet" value="5"> 5 (mkt hog)</td>
         </tr>
         <tr>
          <td><b>Beskriv avvikelsen i detalj:</b></td>
          <td><textarea name="beskrivning" cols="39" rows="3" id="beskrivning"></textarea></td>
         </tr>
         <tr>
          <td><b>Sagt till kund:</b></td>
          <td><textarea name="sagttillkund" cols="39" rows="3" id="sagttillkund"></textarea></td>
         </tr>
         <tr>
          <td><b>Skall det krediteras:</b></td>
          <td><input type="radio" name="krediteras" id="kredit_val" onclick="document.getElementById('kredit_kostnad').value = '';document.getElementById('v_k').value = '';document.getElementById('kred_besk').value = ''; document.getElementById('kred1').hidden = true;document.getElementById('kred2').hidden = true;document.getElementById('kred3').hidden = true; " value="Nej" checked> Nej<br><input type="radio" name="krediteras" id="kredit_val" onclick="document.getElementById('kred1').hidden = false;document.getElementById('kred2').hidden = false;document.getElementById('kred3').hidden = false; " value="Ja"> Ja<br></td>
         </tr>    
         <tr id="kred1" hidden>
          <td><b>Kreditmangd: </b></td>
          <td><input name="kredit_kostnad" type="text" id="kredit_kostnad" style="width:120px; height:18px;" value="" /><input type="radio" name="kenhet" id="kenhet" value="Kg" checked> Kg <input type="radio" name="kenhet" id="kenhet1" value="Kolli" > Kolli<input type="radio" name="kenhet" id="kenhet" value="Sek" > Sek</td></td>
         </tr>
         <tr id="kred2" hidden>
          <td><b>Vem krediterar: </b></td> 
          <td><select name="vem_krediterar" id ="v_k" size="1" ><?php
              foreach ($userarray as $value) { ?>
               <option value="<?php echo $value[0]; ?>"><?php echo $value[1]; echo " - "; echo $value[3];?></option> 
               <?php } ?>                   
              </select></td>
         </tr>
         <tr id="kred3" hidden>
          <td><b>Beskrivning</b></td>
          <td><textarea name="kred_besk" cols="39" rows="3" id="kred_besk"></textarea></td>
         </tr>
         <tr>
          <td><b>Skall det hamtas:</b></td>
          <td><input type="radio" name="hamtas" id="hamtas_val" onclick="document.getElementById('v_h').value = '';document.getElementById('hamta_var').value = '';document.getElementById('hamta_till').value = ''; document.getElementById('hamta_vad').value = '';  document.getElementById('hamta1').hidden = true;document.getElementById('hamta2').hidden = true;document.getElementById('hamta3').hidden = true;document.getElementById('hamta4').hidden = true;document.getElementById('hamta5').hidden = true; " value="Nej" checked> Nej<br><input type="radio" name="hamtas" id="hamtas_val" onclick="document.getElementById('hamta1').hidden = false;document.getElementById('hamta2').hidden = false;document.getElementById('hamta3').hidden = false;document.getElementById('hamta4').hidden = false;document.getElementById('hamta5').hidden = false; " value="Ja"> Ja<br></td>
         </tr>
         <tr id="hamta1" hidden>
          <td><b>Vem ordnar hamtning: </b></td> 
          <td><select name="vem_hamtar" id ="v_h" size="1" ><?php
              foreach ($userarray as $value) { ?>
                <option value="<?php echo $value[0]; ?>"><?php echo $value[1]; echo " - "; echo $value[3];?></option> 
                <?php } ?>                   
              </select></td>
         </tr>
         <tr id="hamta2" hidden>
          <td><b>Hamtas var: </b></td>
          <td><input name="hamta_var" type="text" id="hamta_var" style="width:250px; height:18px;" value="" /></td>
         </tr>
         <tr id="hamta3" hidden>
          <td><b>Levereras till: </b></td>
          <td><input name="hamta_till" type="text" id="hamta_till" style="width:250px; height:18px;" value="" /></td>
         </tr>
         <tr id="hamta4" hidden>
          <td><b>Vad (och hur mycket) skall hamtas:</b></td>
          <td><textarea name="hamta_vad" cols="30" rows="3" id="hamta_vad"></textarea></td>
         </tr>                                                    
         <tr id="hamta5" hidden>
          <td><b>Till lagerstatus:</b></td>
          <td><select name="hamta_till_lager_status" id ="hamta_till_lager_status" size="1" >                                
                <option value="Ordinarie_lager">Ordinarie lager</option>
                <option value="Sparrat_lager">Sparrat</option>
                <option value="Stoppat_lager">Stoppat lager</option>
              </select></td>
         </tr>
         <tr>
          <td><input type="submit" name="Submit" value="Skicka"/> </td>
         </tr>
        </table>     
      </form>
    
</p>
<?php
$homepage->content = ob_get_contents();
ob_end_clean();

//$homepage->content ="Test";
/*  */

$homepage->Display();

Re: How do I display a form as content in a class ' or " or

Posted: Tue May 19, 2015 6:36 am
by hybris
Ok I finally found it out:

<td style="background-color: white;">

damn, lol.

Thanks so much for the output buffering tip. It saves me tons of work. You guys rock!