Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 1861

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>PHP and HTML</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="faq.passwords.html">Password Hashing</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="faq.com.html">PHP and COM</a></div>
 <div class="up"><a href="faq.html">FAQ</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="faq.html" class="chapter">
  <h1>PHP and HTML</h1>

  

  <p class="para">
   PHP and HTML interact a lot: PHP can generate HTML, and HTML
   can pass information to PHP.  Before reading these faqs, it&#039;s
   important you learn how to retrieve <a href="language.variables.external.html" class="link">
   variables from external sources</a>.  The manual page on
   this topic includes many examples as well.  Pay close attention to
   what <em>register_globals</em> means to you too.
  </p>

  <div class="qandaset"><ol class="qandaset_questions"><li><a href="#faq.html.encoding">
     
      What encoding/decoding do I need when I pass a value through a form/URL?
     
    </a></li><li><a href="#faq.html.form-image">
     
      I&#039;m trying to use an &lt;input type=&quot;image&quot;&gt; tag, but 
      the $foo.x and $foo.y variables
      aren&#039;t available.  $_GET[&#039;foo.x&#039;] isn&#039;t existing
      either.  Where are they?  
     
    </a></li><li><a href="#faq.html.arrays">
     How do I create arrays in a HTML &lt;form&gt;?
    </a></li><li><a href="#faq.html.select-multiple">
     
      How do I get all the results from a select multiple HTML tag?
     
    </a></li><li><a href="#faq.html.javascript-variable">
     
      How can I pass a variable from Javascript to PHP?
     
    </a></li></ol></div>
   <dl class="qandaentry" id="faq.html.encoding">
    <dt><strong>
     
      What encoding/decoding do I need when I pass a value through a form/URL?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      There are several stages for which encoding is important. Assuming that
      you have a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> <var class="varname"><var class="varname">$data</var></var>, which contains
      the string you want to pass on in a non-encoded way, these are the
      relevant stages:
      <ul class="itemizedlist">
       <li class="listitem">
        <p class="para">
         HTML interpretation. In order to specify a random string, you
         <em class="emphasis">must</em> include it in double quotes, and 
          <span class="function"><a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a></span> the whole value.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         URL: A URL consists of several parts. If you want your data to be
         interpreted as one item, you <em class="emphasis">must</em> encode it with
          <span class="function"><a href="function.urlencode.html" class="function">urlencode()</a></span>.
        </p>
       </li>
      </ul>
     </p>
     <p class="para">
      <div class="example" id="example-5602">
       <p><strong>Example #1 A hidden HTML form element</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;input&nbsp;type="hidden"&nbsp;value="'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'"&nbsp;/&gt;'</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

      </div>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        It is wrong to  <span class="function"><a href="function.urlencode.html" class="function">urlencode()</a></span>
        <var class="varname"><var class="varname">$data</var></var>, because it&#039;s the browsers responsibility to
         <span class="function"><a href="function.urlencode.html" class="function">urlencode()</a></span> the data. All popular browsers do that
        correctly. Note that this will happen regardless of the method (i.e.,
        GET or POST). You&#039;ll only notice this in case of GET request though,
        because POST requests are usually hidden.
       </span>
      </p></blockquote>
      <div class="example" id="example-5603">
       <p><strong>Example #2 Data to be edited by the user</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"&lt;textarea&nbsp;name='mydata'&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/textarea&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

      </div>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        The data is shown in the browser as intended, because the browser will
        interpret the HTML escaped symbols.
       </span>
       <span class="simpara">
        Upon submitting, either via GET or POST, the data will be urlencoded
        by the browser for transferring, and directly urldecoded by PHP. So in
        the end, you don&#039;t need to do any urlencoding/urldecoding yourself,
        everything is handled automagically.
       </span>
      </p></blockquote>
      <div class="example" id="example-5604">
       <p><strong>Example #3 In a URL</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;a&nbsp;href="'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #DD0000">"/nextpage.php?stage=23&amp;data="&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">'"&gt;'</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

      </div>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        In fact you are faking a HTML GET request, therefore it&#039;s necessary to
        manually  <span class="function"><a href="function.urlencode.html" class="function">urlencode()</a></span> the data.
       </span>
      </p></blockquote>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        You need to  <span class="function"><a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a></span> the whole URL, because the
        URL occurs as value of an HTML-attribute. In this case, the browser
        will first un- <span class="function"><a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a></span> the value, and then pass
        the URL on. PHP will understand the URL correctly, because you
         <span class="function"><a href="function.urlencode.html" class="function">urlencode()</a></span>d the data.
       </span>
       <span class="simpara">
        You&#039;ll notice that the <em>&amp;</em> in the URL is replaced
        by <em>&amp;amp;</em>. Although most browsers will recover
        if you forget this, this isn&#039;t always possible. So even if your URL is
        not dynamic, you <em class="emphasis">need</em> to
         <span class="function"><a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a></span> the URL.
       </span>
      </p></blockquote>
     </p>
     
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.html.form-image">
    <dt><strong>
     
      I&#039;m trying to use an &lt;input type=&quot;image&quot;&gt; tag, but 
      the <var class="varname"><var class="varname">$foo.x</var></var> and <var class="varname"><var class="varname">$foo.y</var></var> variables
      aren&#039;t available.  <var class="varname"><var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET['foo.x']</a></var></var> isn&#039;t existing
      either.  Where are they?  
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      When submitting a form, it is possible to use an image instead of
      the standard submit button with a tag like:
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;input type=&quot;image&quot; src=&quot;image.gif&quot; name=&quot;foo&quot; /&gt;</pre>
</div>
      </div>

      When the user clicks somewhere on the image, the accompanying form
      will be transmitted to the server with two additional variables:
      <var class="varname"><var class="varname">foo.x</var></var> and <var class="varname"><var class="varname">foo.y</var></var>.
     </p>
     <p class="para">
      Because <var class="varname"><var class="varname">foo.x</var></var> and <var class="varname"><var class="varname">foo.y</var></var> would
      make invalid variable names in PHP, they are automagically converted to
      <var class="varname"><var class="varname">foo_x</var></var> and <var class="varname"><var class="varname">foo_y</var></var>. That is, the
      periods are replaced with underscores.  So, you&#039;d access these variables
      like any other described within the section on retrieving 
      <a href="language.variables.external.html" class="link">variables from external 
      sources</a>.  For example, <var class="varname"><var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET['foo_x']</a></var></var>.
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
         Spaces in request variable names are converted to underscores.
       </p>
      </p></blockquote>
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.html.arrays">
    <dt><strong>
     How do I create arrays in a HTML &lt;form&gt;?
    </strong></dt>
    <dd class="answer">
     <p class="para">
      To get your &lt;form&gt; result sent as an 
      <a href="language.types.array.html" class="link">array</a> to your PHP script
      you name the &lt;input&gt;, &lt;select&gt; or &lt;textarea&gt;
      elements like this:
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;input name=&quot;MyArray[]&quot; /&gt;
&lt;input name=&quot;MyArray[]&quot; /&gt;
&lt;input name=&quot;MyArray[]&quot; /&gt;
&lt;input name=&quot;MyArray[]&quot; /&gt;</pre>
</div>
      </div>

      Notice the square brackets after the variable name, that&#039;s what
      makes it an array. You can group the elements into different arrays
      by assigning the same name to different elements:
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;input name=&quot;MyArray[]&quot; /&gt;
&lt;input name=&quot;MyArray[]&quot; /&gt;
&lt;input name=&quot;MyOtherArray[]&quot; /&gt;
&lt;input name=&quot;MyOtherArray[]&quot; /&gt;</pre>
</div>
      </div>

      This produces two arrays, MyArray and MyOtherArray, that gets sent
      to the PHP script.  It&#039;s also possible to assign specific keys
      to your arrays:
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;input name=&quot;AnotherArray[]&quot; /&gt;
&lt;input name=&quot;AnotherArray[]&quot; /&gt;
&lt;input name=&quot;AnotherArray[email]&quot; /&gt;
&lt;input name=&quot;AnotherArray[phone]&quot; /&gt;</pre>
</div>
      </div>

      The AnotherArray array will now contain the keys 0, 1, email and phone.
      </p>
      <p class="para">
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
         Specifying an arrays key is optional in HTML.  If you do not specify
         the keys, the array gets filled in the order the elements appear in
         the form.  Our first example will contain keys 0, 1, 2 and 3.
        </p>
       </p></blockquote>
      </p>
      <p class="para">
      See also
      <a href="ref.array.html" class="link">Array Functions</a> and
      <a href="language.variables.external.html" class="link">Variables From External
      Sources</a>.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.html.select-multiple">
    <dt><strong>
     
      How do I get all the results from a select multiple HTML tag?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      The select multiple tag in an HTML construct allows users to
      select multiple items from a list. These items are then passed
      to the action handler for the form. The problem is that they
      are all passed with the same widget name. I.e.
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;select name=&quot;var&quot; multiple=&quot;yes&quot;&gt;</pre>
</div>
      </div>

      Each selected option will arrive at the action handler as:
      <div class="example-contents"><div class="cdata"><pre>
var=option1
var=option2
var=option3
      </pre></div></div>

      Each option will overwrite the contents of the previous
      <var class="varname"><var class="varname">$var</var></var> variable. The solution is to use
      PHP&#039;s &quot;array from form element&quot; feature. The following
      should be used:
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;select name=&quot;var[]&quot; multiple=&quot;yes&quot;&gt;</pre>
</div>
      </div>

      This tells PHP to treat <var class="varname"><var class="varname">$var</var></var> as an array and
      each assignment of a value to var[] adds an item to the array.
      The first item becomes <var class="varname"><var class="varname">$var[0]</var></var>, the next
      <var class="varname"><var class="varname">$var[1]</var></var>, etc. The  <span class="function"><a href="function.count.html" class="function">count()</a></span>
      function can be used to determine how many options were selected,
      and the  <span class="function"><a href="function.sort.html" class="function">sort()</a></span> function can be used to sort
      the option array if necessary.
     </p>
     <p class="para">
      Note that if you are using JavaScript the <em>[]</em>
      on the element name might cause you problems when you try to
      refer to the element by name. Use it&#039;s numerical form element
      ID instead, or enclose the variable name in single quotes and
      use that as the index to the elements array, for example:
      <div class="example-contents"><div class="cdata"><pre>
variable = documents.forms[0].elements[&#039;var[]&#039;];
      </pre></div></div>

     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.html.javascript-variable">
    <dt><strong>
     
      How can I pass a variable from Javascript to PHP?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      Since Javascript is (usually) a client-side technology, and
      PHP is (usually) a server-side technology, and since HTTP is a
      &quot;stateless&quot; protocol, the two languages cannot directly share
      variables.
     </p>
     <p class="para">
      It is, however, possible to pass variables between the two.
      One way of accomplishing this is to generate Javascript code
      with PHP, and have the browser refresh itself, passing specific
      variables back to the PHP script. The example below shows
      precisely how to do this -- it allows PHP code to capture screen
      height and width, something that is normally only possible on
      the client side.
     </p>
     <p class="para">
      <div class="example" id="example-5605">
       <p><strong>Example #4 Generating Javascript with PHP</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'width'</span><span style="color: #007700">])&nbsp;AND&nbsp;isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'height'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;output&nbsp;the&nbsp;geometry&nbsp;variables<br />&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Screen&nbsp;width&nbsp;is:&nbsp;"</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'width'</span><span style="color: #007700">]&nbsp;.</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Screen&nbsp;height&nbsp;is:&nbsp;"</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'height'</span><span style="color: #007700">]&nbsp;.</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;pass&nbsp;the&nbsp;geometry&nbsp;variables<br />&nbsp;&nbsp;//&nbsp;(preserve&nbsp;the&nbsp;original&nbsp;query&nbsp;string<br />&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;--&nbsp;post&nbsp;variables&nbsp;will&nbsp;need&nbsp;to&nbsp;handled&nbsp;differently)<br /><br />&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"&lt;script&nbsp;language='javascript'&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;location.href=\"</span><span style="color: #007700">${</span><span style="color: #0000BB">_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'SCRIPT_NAME'</span><span style="color: #007700">]}</span><span style="color: #DD0000">?</span><span style="color: #007700">${</span><span style="color: #0000BB">_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'QUERY_STRING'</span><span style="color: #007700">]}</span><span style="color: #DD0000">"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&amp;width=\"&nbsp;+&nbsp;screen.width&nbsp;+&nbsp;\"&amp;height=\"&nbsp;+&nbsp;screen.height;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/script&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;exit();<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
       </div>

      </div>
     </p>
    </dd>
   </dl>

  
 </div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="faq.passwords.html">Password Hashing</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="faq.com.html">PHP and COM</a></div>
 <div class="up"><a href="faq.html">FAQ</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>