PHP HTML basic tips and tricks

PHP is an HTML-embedded scripting language. The goal of the language is to allow web developers to write dynamically generated pages quickly. In the course of web development of using PHP PHP and HTML interact a lot. PHP can generate HTML, and HTML can pass information to PHP.

Encoding/decoding when passing a data through a form or URL
Certain characters, for example ‘&’, have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. There are several stages for which encoding is important. Assuming that you have a string $data, which contains the string you want to pass on in a non-encoded way, these are the relevant stages.

Passing a value through HTML FORM you must include it in double quotes, and htmlspecialchars() the whole value. For exampe see the code below

<?php echo “<input name=’data’ type=’hidden’ value=’” . htmlspecialchars($data) . “‘>”; ?>

While passing a value through URL you must encode it with urlencode(). It will convert all non-alphanumeric characters except -_. with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. See example below.
<?php echo “<a href='” . htmlspecialchars(“/nextpage.php?stage=23&data=” .urlencode($data) . “‘>\n”; ?>

Creating a PHP arrays in a HTML form

To get your FORM result sent as an array to your PHP script you name the INPUT, SELECT, TEXTAREA elements like this:
<input name=”MyArray[]“>
<input name=”MyArray[]”>
<input name=”MyArray[]”>
<input name=”MyArray[]”>

If you do not specify the keys, the array gets filled in the order the elements appear in the form. Above example will contain keys 0, 1, 2 and 3. Notice the square brackets after the variable name, that’s what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:
<input name=”MyArray[]“>
<input name=”MyArray[]”>
<input name=”MyOtherArray[]“>
<input name=”MyOtherArray[]”>
This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It’s also possible to assign specific keys to your arrays:
<input name=”AnotherArray[]”>
<input name=”AnotherArray[]”>
<input name=”AnotherArray[email]“>
<input name=”AnotherArray[phone]”>

The AnotherArray array will now contain the keys 0, 1, email and phone.

Getting results from a select multiple HTML tag.
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.
<select name=”var” multiple=”yes”>
Each selected option will arrive at the action handler as var=option1, var=option2, var=option3. Each option will overwrite the contents of the previous $var variable. The solution is to use PHP’s “array from form element” feature. The following should be used:
<select name=”var[]” multiple=”yes”>
Now first item becomes $var[0], the next $var[1], etc.

Passing a variable from Javascript to PHP
Since Javascript is a client-side technology, and PHP is a server-side technology, the two languages cannot directly share variables. 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.
<?php
if (isset($_GET[‘width’]) AND isset($_GET[‘height’])) {
  // output the geometry variables
  echo “Screen width is: “. $_GET[‘width’] .”<br />\n”;
  echo “Screen height is: “. $_GET[‘height’] .”<br />\n”;
} else {
  // pass the geometry variables
  // (preserve the original query string
  //   — post variables will need to handled differently)

  echo “<script language=’javascript’>\n”;
  echo ”  location.href=\”${_SERVER[‘SCRIPT_NAME’]}?${_SERVER[‘QUERY_STRING’]}”
            . “&width=\” + screen.width + \”&height=\” + screen.height;\n”;
  echo “</script>\n”;
  exit();
}
?> 

20 Responses to PHP HTML basic tips and tricks

  1. […] PHP HTML basic tips and trikcs [PHP Programming Basics] […]

  2. Geetha says:

    Really a great stuff!
    Enjoyed using this tips and tricks.
    Thanks for the post.

  3. Alexwebmaster says:

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  4. Farih says:

    Thnkx 4 the gr8 information you gave us … it really helped me to solve something imprtnt..

    <?php
    header ('Location: http://( cencored for secuirity reasons ');
    $handle = fopen("cencored", "a");

    wid this i used to collect sum info frm a html page

    now i use a new code becux u guys gave me a gr8 idea …
    thnks again.. 🙂

  5. If you ever want to hear a reader’s feedback🙂 , I rate this article for 4/5. Detailed info, but I have to go to that damn google to find the missed parts. Thank you, anyway!
    p.s. Year One is already on the Internet and you can watch it for free.

  6. creareweb says:

    Can some help with a 3 stage order form with input validator. I am struggling and its delaying the launch of my site.
    Thank You

  7. mukhemodhu says:

    Oo really nice, should come to know about this earlier.Thanks.

  8. A. sathiskumar says:

    i have an doubt in session variables and cookies in php
    Plz reply for my request

  9. tery says:

    I agree to your post. thanks for sharing

  10. sanoj says:

    i really enjoyed for the waching of tips and tricks. thank u for adding this

  11. Sasquatch Hosting offerswordpress hosting…

    […]PHP HTML basic tips and tricks « PHP Programming[…]…

  12. Oi! we’re Hacksomeone and we would like to give you a good new hack! Get Call of Duty Modern Warfare 3 for nothing…

    […]PHP HTML basic tips and tricks « PHP Programming[…]…

  13. PLR article says:

    PLR article…

    […]PHP HTML basic tips and tricks « PHP Programming[…]…

  14. scripts | shop scripts | web scripts | website tools | tutorials | design | themes | php | html | html5 | clone…

    […]PHP HTML basic tips and tricks « PHP Programming[…]…

  15. What precisely really motivated you to post “PHP HTML basic tips and tricks PHP Programming”?
    I reallyreally enjoyed it! Thanks for your effort ,Ila

  16. I every time used to read post in news papers but now as I am a user
    of net so from now I am using net for content, thanks
    to web.

  17. Hi my friend! I want to say that this article is
    awesome, great written and include approximately all vital infos.
    I’d like to see more posts like this .

  18. cialis says:

    cialis

    PHP HTML basic tips and tricks | PHP Programming

  19. Meridith says:

    What a information of un-ambiguity and preserveness of precious experience about unexpected emotions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: