IT Community - Software Programming, Web Development and Technical Support

PHP and HTML

This is a discussion on PHP and HTML within the PHP Programming forums, part of the Web Development category; Hi, PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. we start ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 03-12-2008, 10:56 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default PHP and HTML

Hi,
PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. we start discussing on this regards
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 03-12-2008, 11:00 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP and HTML

Encoding/decoding the value to the html form component
Assuming that you have a string $data, which contains the string you want to pass on in a non-encoded way

HTML interpretation. In order to specify a random string, you must include it in double quotes, and htmlspecialchars() the whole value.

URL: A URL consists of several parts. If you want your data to be interpreted as one item, you must encode it with urlencode().

Here the example for A hidden HTML form element
PHP Code:
<?php
    
echo "<input type='hidden' value='" htmlspecialchars($data) . "' />\n";
?>
__________________
With,
J. Jeyaseelan

Everything Possible

Last edited by Jeyaseelansarc : 03-12-2008 at 11:06 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 03-12-2008, 11:02 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP and HTML

Hi,
From the previous example, It is wrong to urlencode() $data, because it's the browsers responsibility to urlencode() the data.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 03-12-2008, 11:05 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP and HTML

Example for data to be edited by the user:

PHP Code:
<?php
    
echo "<textarea name='mydata'>\n";
    echo 
htmlspecialchars($data)."\n";
    echo 
"</textarea>";
?>
From the above example, The data is shown in the browser as intended, because the browser will interpret the HTML escaped symbols.

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't need to do any urlencoding/urldecoding yourself, everything is handled automagically.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 03-12-2008, 11:08 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP and HTML

Here example to send in URL:

PHP Code:
<?php
    
echo "<a href='" htmlspecialchars("/nextpage.php?stage=23&data=" .
        
urlencode($data)) . "'>\n";
?>
In the above example You need to htmlspecialchars() the whole URL, because the URL occurs as value of an HTML-attribute. In this case, the browser will first un-htmlspecialchars() the value, and then pass the URL on. PHP will understand the URL correctly, because you urlencoded() the data.

You'll notice that the & in the URL is replaced by &amp;. Although most browsers will recover if you forget this, this isn't always possible. So even if your URL is not dynamic, you need to htmlspecialchars() the URL.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 03-12-2008, 11:11 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP and HTML

Create arrays in a HTML <form>?

To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />

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.

Note: 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.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 03-13-2008, 12:44 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP and HTML

To get all the 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">

This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.

Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it'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:
variable = documents.forms[0].elements['var[]'];
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 03-13-2008, 12:46 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP and HTML

To pass a variable from Javascript to PHP

Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, 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 Code:

<?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();
}
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 03-13-2008, 01:41 AM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 289
Falcon is on a distinguished road
Default Re: PHP and HTML

Hi Buddies

Here another one example to embed HTML into PHP

PHP Code:
<?php
echo "<html>";
echo 
"<title>HTML with PHP</title>";
echo 
"<b>My Example</b>";

//your php code here

Print "<i>Print works too!</i>";
?>
Regards
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10  
Old 03-13-2008, 01:56 AM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 289
Falcon is on a distinguished road
Default Re: PHP and HTML

Hi Buddies

use cookie to passing javascript variable to php variable.

see this example.

HTML Code:
<HTML> <TITLE>Getting screen resolution........ Please wait....................</TITLE> <!--
no personal urls please
--> <HEAD> <script language="javascript"> <!--
writeCookie();

function writeCookie()
{
var today = new Date();
var the_date = new Date("December 31, 2099");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "ur_width=" + screen.width ;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie
var the_cookie = "ur_height=" +screen.height;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie

location = 'b.php';
}
//--> </script> </HEAD> <BODY>

Detecting your screen resolution .........<br>Please wait.....................................

</BODY> </HTML>
File b.php
PHP Code:
<?php
if(isset($_COOKIE["ur_width"]) and isset($_COOKIE["ur_height"])){
$ur_width $HTTP_COOKIE_VARS["ur_width"];
$ur_height $HTTP_COOKIE_VARS["ur_height"];
} else {
header("Location: a.html");
}
//delete the cookie
setcookie("ur_width",'',time()-3600);
setcookie("ur_height",'',time()-3600);
?>
Regards
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
html pav HTML, CSS and Javascript Coding Techniques 0 08-01-2008 04:09 AM
HTML or CSS capture Web Design Help 2 03-15-2007 06:21 PM
html djree HTML, CSS and Javascript Coding Techniques 1 03-08-2007 08:12 PM
swf in html nssukumar Flash Actionscript Programming 2 03-06-2007 04:10 AM
HTML vs. CSS Joe HTML, CSS and Javascript Coding Techniques 3 03-03-2007 07:12 PM


All times are GMT -7. The time now is 10:52 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0