Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts
If you are working with Iframes then definitely you might experience the auto height resizing problem. For all of you Here is a Simplest Tutorial which will solve the Iframe resize problem on the fly.
The Problem :
Lets  say,   We have    Two  Pages    Parent  Page  and  Child  Page.   You want to  load    Child  Page  in the  Parent  Page  in the form  of  Iframe.  But,  the Height of  Child  page  is  dynamic.    This  is  not  possible  by using   Height  auto  in css.  we have  to   Adjust  the  Child  Iframe  height  accordingly  using  Javascript.
The  Solution :
The Solution  for  this  Problem  Do  got    Two  Scenario’s
1.   When   Parent  Page  and  Child  page   Both  are  in the   Same  Domain
2.   When  Parent Page  and Child  page   Both  are  in  different  Domains
In  the above  Scenario’s   Solution   looks  similar   But   to Acheive  we have  to  do  a  Extra  call  and  We need  a  Extra  Page   when   The  Parent  page  and  Child  page  are in different  Domains .
Lets  get into the Solution  by  Scenario  basis.
1.  When  Parent Page  and  Child Page  are in  Same  Domain
To  resize  the  Iframe  height  in this scenario   is  quiet  simple  all  we need  to do have  2  JS  functions.
callResize()    and  resizeIframe ()
We will  make use of this  callResize()  in    Child  page    and   we will make use of the  resizeIframe() in  Parent  page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
< script type="text/javascript" >
//  Call  reSize
 
function  callResize()
{           
// get the  Body height of the iframe  page
 var height = document.body.scrollHeight;
//  send a request to parent  to reset the height of the height
 parent.resizeIframe(height);
 
}
// call  this  function on  page load 
 window.onload =callResize;
</script>
So, that was Script tag which we will place in the iframe page
1
2
3
4
5
6
7
8
<script type="text/javascript">
//   reSize Iframe when ever child  calls  it
function resizeIframe(dynheight)
        {
            document.getElementById("childframe").height=parseInt(dynheight)+10;
        }
 
</script>
Place this function in parent page. You are DONE .. SCENARIO 1 Complete
Here is the Complete Example Page.
Iframe Page :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script>
            function callResize()
            {
                var height = document.body.scrollHeight;
                parent.resizeIframe(height);
            }
            window.onload =callResize;
        </script>
    </head>
    <body>
        <h2>This is  Iframe</h2>
        <br/>
        <br/>
        <br/>
        <br/><br/><br/>
        <br/><br/><br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/><br/>
        <br/>
        <br/>
        <br/>
        <br/><br/>
        <h2>  This is  Final </h2>
 
    </body>
</html>
Parent Page ::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script>
        function resizeIframe(dynheight)
        {
            document.getElementById("childframe").height=parseInt(dynheight)+10;
        }
 
        </script>
    </head>
    <body>
 
        <iframe id="childframe" src="iframe.php" frameborder="no" scrolling="no" />
 
 
    </body>
</html>
1. When Parent Page and Child Page are in Different domain
Hmm, When Both the Pages are in Different Domains, We need to Take a Help of Another Page which should be present in the Same Domain as parent to Resize The Iframe Height.
So, We Require to add our small JS Functions 3 pages
1. Parent Page in Domain X
2. Child Page in Domain Y
3. Helper Page in Domain X
Why we require Helper page is, If we try to call parent.resizeIframe() function from the page which is in Domain Y we will get Permission Denied as they are in Different Domains ( Cross Domain Problem)
So, In this Scenario we will achieve our requirement Just By Adding a Empty Iframe Tag into the Child Page and passing the height of the Child Page as a Query String Parameter
Javascript Functions we will use for this scenario
resizeIframe() Function as same as in the above scenario
1
2
3
4
5
6
7
<script>
  // Resize iframe 
  function resizeIframe(dynheight)
  {
    document.getElementById('frame_name_here').height = parseInt(height)+10;
  }
</script>
Call reSize Function, This will change a Little. Just above the Script tag we need to add a Iframe tag and here we wont call parent.resizeIframe because of cross domain problem instead we will update the Helper url with height as a query string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<iframe  id="helperframe"   height="0"  width="0"  ></iframe>
< script type="text/javascript" >
//  Call  reSize
 
function  callResize()
{           
// get the  Body height of the iframe  page
 var height = document.body.scrollHeight;
 
var  helper =  document.getElementById('helperframe');
//  Send   height  to  the  Domain X   as  a  Query String  and  Do not forget to  add   a  random parameter 
helper.src =  "http://www.DomainX.com/helperpage.html?h="+height+"&r="+Math.random();
 
 
}
// call  this  function on  page load 
 window.onload =callResize;
</script>
Now, Call the Resize Iframe from the Helper Page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
<script> 
      // Tell the parent iframe what height the iframe needs to be
      function parentIframeResize()
      {
         var height = getParam('height');
         // This works as our parent's parent is on our domain..
         parent.parent.resizeIframe(height);
      }
 
      // Helper function, parse param from request string
      function getParam( name )
      {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
          return "";
        else
          return results[1];
      }
window.onload = parentIframeResize;
</script>
Scenario 2 is also DONE ..
Example Pages for Scenario 2
Parent Page in DomainX.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script>
        function resizeIframe(dynheight)
        {
            document.getElementById("childframe").height=dynheight;
        }
 
        </script>
    </head>
    <body>
 
        <iframe id="childframe" src="http://www.DomainY.com/iframe.php" frameborder="no" scrolling="no" />
 
 
    </body>
</html>
Child Page in DomainY.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script>
            function callResize()
            {
                var height = document.body.scrollHeight;
                var  helper =  document.getElementById('helperframe');
               helper.src  =  "http://www.DomainX.com/helper.html?h="+height+"&r="+Math.random();
            }
            window.onload =callResize;
        </script>
    </head>
    <body>
<iframe id="helperframe" height=0 width=0></iframe>
        <h2>This is  Iframe</h2>
        <br/>
        <br/>
        <br/>
        <br/><br/><br/>
        <br/><br/><br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/><br/>
        <br/>
        <br/>
        <br/>
        <br/><br/>
        <h2>  This is  Final </h2>
 
 
 
 
 
 
 
 
 
    </body>
</html>
Helper Page in Domain X.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
<html> 
  <body > 
    <script> 
      // Tell the parent iframe what height the iframe needs to be
      function parentIframeResize()
      {
         var height = getParam('height');
         // This works as our parent's parent is on our domain..
         parent.parent.resizeIframe(height);
      }
 
      // Helper function, parse param from request string
      function getParam( name )
      {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
          return "";
        else
          return results[1];
      }
 
window.onload = parentIframeResize;
    </script> 
  </body> 
</html>

This tutorial will teach you how to create a very simple contact form for HTML based website template.

First of all create 2 files: contact_form.html and contact.php. The first file will contain HTML code for the form and the second -will process the data from the form


HTML

Below is the HTML code of the contact form
   
<form action="contact.php" method="post">
    Your name
    <input type="text" name="cf_name">
    Your e-mail
    <input type="text" name="cf_email">
    Message
    <textarea name="cf_message">
    <input type="submit" value="Send">
    <input type="reset" value="Clear">
</form>

And this is how it will look in the browser

HTML contact form



Let’s have a quick look at some main aspects of it. The <form> tag should have 2 additional attributes:

action=”contact.php” – this attribute specifies where to send the data from the contact form fields, when it has been submitted

method=”post” – this attribute specifies how to send data from the form to the file specified in the action attribute

The <input> and <textarea> tags should have an attribute “name” with a unique identifier. This attribute is used to identify form data after it has been submitted to the server
And the 2 input elements that are used as Submit and Clear buttons, one should have type=”submit” assigned to it and the other type=”reset”


PHP

Now for the contact.php file that will actually grab the data from the fields, compose into a message and send to your email. Below is the code of the file with comments to its major sections.

Assigning the data sent from the contact form fields (cf_name, cf_email, cf_message) to php variables ($cf_message, $field_email, $field_message)
   
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to shall contain the site owner email, this is where the email is sent to. You can specify multiple emails by separating them with a comma (e.g. mail-one@template-help.com, mail-two@template-help.com)
   
$mail_to = 'test@test-mail.com';

Subject of the email you receive from the contact form

$subject = 'Message from a site visitor ' . $field_name;

Constructing the body of the message
   
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

Constructing the headers of the message
   
$headers = "From: $cf_email\r\n";
$headers .= "Reply-To: $cf_email\r\n";

Defining mail() function and assigning it to a variable $mail_status, which is used below to check whether the mail has been sent or not
   
$mail_status = mail($mail_to, $subject, $body_message, $headers);

If the mail() function executed successfully then do the code below
   
if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        // Print a message
        alert('Thank you for the message. We will contact you shortly.');
        // Redirect to some page of the site. You can also specify full URL, e.g. http://template-help.com
        window.location = 'contact_page.html';
    </script>
<?php
}

If the mail() function fails, then execute the following code
   
else { ?>
    <script language="javascript" type="text/javascript">
        // Print a message
        alert('Message failed. Please, send an email to gordon@template-help.com');
        // Redirect to some page of the site. You can also specify full URL, e.g. http://template-help.com
        window.location = 'contact_page.html';
    </script>
<?php
}?>