Tuesday, September 13, 2011

Php Form Processing

Before you can process the information, you need to create an HTML form that will send information to your PHP script. There are two methods for sending data: POST and GET. These two types of sending information are defined in your HTML form element's method attribute. Also, you must specify the location of the PHP file that will process the information.

Below is an HTML form that will send the data using the POST method. Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. Copy and paste this code and save it as form.html.

<html>
<head>
  <title>Process the HTML form data with the POST method</title>
</head>
<body>
  <form name="myform" action="process.php" method="POST">
    <input type="hidden" name="check_submit" value="1" />
    Name: <input type="text" name="Name" /><br />
    Password: <input type="password" name="Password" maxlength="10" /><br />
    Select something from the list: <select name="Seasons">
      <option value="Spring" selected="selected">Spring</option>
      <option value="Summer">Summer</option>
      <option value="Autumn">Autumn</option>
      <option value="Winter">Winter</option>
    </select><br /><br />
    Choose one:
      <input type="radio" name="Country" value="USA" /> USA
      <input type="radio" name="Country" value="Canada" /> Canada
      <input type="radio" name="Country" value="Other" /> Other
    <br />
    Choose the colors:
      <input type="checkbox" name="Colors[]" value="green" checked="checked" /> Green
      <input type="checkbox" name="Colors[]" value="yellow" /> Yellow
      <input type="checkbox" name="Colors[]" value="red" /> Red
      <input type="checkbox" name="Colors[]" value="gray" /> Gray
    <br /><br />
    Comments:<br />
    <textarea name="Comments" rows="10" cols="60">Enter your comments here</textarea><br />
    <input type="submit" />
  </form>
</body>
</head>
</html>

The example HTML page above includes different form elements: input fields, select list, text area, radio buttons, checkboxes and a submit button. When a user fills in this form and clicks on the submit button, the form data is sent to the process.php file.

Notice that we have added square brackets [] to the name of the checkbox element. The reason for the square brackets is that it informs PHP that the value may be an array of information. Users can select multiple values, and PHP will place them all into an array of the value of the name attribute.

For example, if you pick Green and Yellow and submit the form, then $_POST['Colors'] is itself an array. $_POST['Colors'][0] is Green and $_POST['Colors'][1] is Yellow. That's because the name attribute of the checkbox element is Colors[]. If the name was just Colors, then $_POST['Colors'] would be a string, holding only one of the selected values.

Back to top

Processing the Form Data ( PHP Code )

Next, we are going to create our PHP file that will process the data. When you submit your HTML form PHP automatically populates two superglobal arrays, $_GET and $_POST, with all the values sent as GET or POST data, respectively. Therefore, a form input called 'Name' that was sent via POST, would be stored as $_POST['Name'].

Copy and paste this code and save it as process.php in the same directory as form.html.

<?php
//Check whether the form has been submitted
if (array_key_exists('check_submit', $_POST)) {
   //Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)

   $_POST['Comments'] = nl2br($_POST['Comments']); 
   //Check whether a $_GET['Languages'] is set
   if ( isset($_POST['Colors']) ) { 
     $_POST['Colors'] = implode(', ', $_POST['Colors']); //Converts an array into a single string
   }

   //Let's now print out the received values in the browser
   echo "Your name: {$_POST['Name']}<br />";
   echo "Your password: {$_POST['Password']}<br />";
   echo "Your favourite season: {$_POST['Seasons']}<br /><br />";
   echo "Your comments:<br />{$_POST['Comments']}<br /><br />";
   echo "You are from: {$_POST['Country']}<br />";
   echo "Colors you chose: {$_POST['Colors']}<br />";
} else {
    echo "You can't see this page without submitting the form.";
}
?>

Let's give a little explanation. At the first line we check whether the form has been submitted and the php script has not been called directly. Next we convert the new line characters in the text area into HTML line breaks. Then we check whether a $_POST['Colors'] is set and if so we use implode() function to convert $_POST['Colors'] array into a single string. Finally, we print out all received values in the browser.


<a href="http://www.ny-aca.com">Hearing Aids Newyork</a>
<a href="http://www.ny-aca.com">Hearing Aids Staten island</a>
<a href="http://www.ny-aca.com">Hearing Aids NYC</a>
<a href="http://www.ny-aca.com">Advanced Care Audiology</a>
<a href="http://www.ny-aca.com">Audiology</a>
<a href="http://www.ny-aca.com">hearing aids</a>

Php Form Processing

Beneath is an HTML work that will ship the accumulation using the Mark method. Collection dispatched from a descriptor with the Place method is covert to others and has no limits on the turn of assemblage to displace. Make and paste this encipher and forbear it as spring.html.
<html>
<head>
  <title>Process the HTML gathering information with the Communicating method</title>
</head>
<body>
  <form name="myform" action="operation.php" method="Assemblage">
    <input type="concealed" name="check_submit" value="1" />
    Study: <input type="schoolbook" name="Jargon" /><br />
    Word: <input type="secret" name="Watchword" maxlength="10" /><br />
    Select something from the table: <select name="Seasons">
      <option value="Springiness" selected="elect">Spring</option>
      <option value="Summer">Summer</option>
      <option value="Season">Autumn</option>
      <option value="Season">Winter</option>
    </select><br /><br />
    Select one:
      <input type="broadcasting" name="Country" value="USA" /> USA
      <input type="radio" name="Region" value="Canada" /> Canada
      <input type="tuner" name="Country" value="Separate" /> Opposite
    <br />
    Take the colours:
      <input type="checkbox" name="Emblem[]" value="ketalar" checked="checkered" /> Greenness
      <input type="checkbox" name="Colors[]" value="yellow" /> Chromatic
      <input type="checkbox" name="Emblem[]" value="red" /> Red
      <input type="checkbox" name="Colors[]" value="botanist" /> Old
    <br /><br />
    Comments:<br />
    <textarea name="Comments" rows="10" cols="60">Enter your comments here</textarea><br />
    <input type="submit" />
  </form>
</body>
</head>
</html>

The representation HTML diplomat above includes distinguishable mould elements: signal fields, superior list, book area, radio buttons, checkboxes and a submit switch. When a soul fills in this constitute and clicks on the submit button, the alter accumulation is sent to the operation.php file.

Asking that we change another simple brackets [] to the personage of the checkbox environs. The present for the paddle brackets is that it informs PHP that the evaluate may be an regalia of assemblage. Users can select bigeminal values, and PHP will send them all into an regalia of the amount of the make construct.

Next, we are achievement to create our PHP record that leave operation the information. When you submit your HTML assemblage PHP automatically populates two superglobal arrays, $_GET and $_POST, with all the values sent as GET or Displace assemblage, respectively. Therefore, a gathering sign titled 'Institute' that was dispatched via Office, would be stored as $_POST['Denote'].

Copy and attach this cypher and foreclose it as affect.php in the selfsame directory as appearance.html.
<?php
//Check whether the spring has been submitted
if (array_key_exists('check_submit', $_POST)) 
   //Converts the new wares characters (\n) in the matter region into HTML route breaks (the <br /> tag)
   $_POST['Comments'] = nl2br($_POST['Comments']);
   //Check whether a $_GET['Languages'] is set
   if ( isset($_POST['Emblem']) )  
     $_POST['Colors'] = break(', ', $_POST['Colors']); //Converts an array into a undivided advance
   

   //Let's now indication out the conventional values in the application
   reverberate "Your label:  $_POST['Label'] <br />";
   echo "Your parole:  $_POST['Countersign'] <br />";
   echo "Your pick period:  $_POST['Seasons'] <br /><br />";
   nymph "Your comments:<br /> $_POST['Comments'] <br /><br />";
   reflexion "You are from:  $_POST['Country'] <br />";
   echo "Emblem you chose:  $_POST['Flag'] <br />";
  else 
    emit "You can't see this industrialist without submitting the become.";
 
?>

Monday, May 25, 2009

Building A New Website In PHP

New website owners and existing website owners alike consistently make a very common, yet extremely costly mistake. They find a web designer first and then an SEO second. Unfortunately, these entrepreneurs do not realize that literally every single thing that goes into building a new website will impact your SEO campaign. They commonly funnel thousands of dollars into a brand new website, only to find out that there are a lot of areas that must be rebuilt in order to have an optimum SEO campaign.

I will identify 6 major areas of concern, in hopes that even a small percentage of these website owners will come across this document at the right time, which is BEFORE they begin to develop their website.


1) Domain name registration & hosting. Make sure that when you register your website's address that you register it for at least 5 years. Sites that register their site for a short amount of time send up a red flag at Google, who end up thinking that site site has been registered short-term in the hopes of helping another website, that is owned by the same person/company, to rank well by linking to it. When choosing your domain name, do not choose a URL that is riddled with hyphens between all your keywords. It is more important that you target this to your visitors than to the search engines.


2) Creating static URL's. This is one of the most overlooked yet important things that you can do to ensure that your SEO campaign is a success. By eliminating dynamic parameters within your website's URLs, you are ensuring that search engine bots will have no problems indexing all of your pages. Creating static URL's can be accomplished by using the mod rewrite command in the .htaccess file in the root folder of your server. Here is an example of a dynamic URL (which you want to avoid), and a static URL (which you want):

Dynamic: http://www.yoursite.com/listings.php?ref=22
Static: http://www.yoursite.com/listings/22.html

Make sure that any potential programmer or designer that you hire understands that this will be a full requirement of the job.


3) Editing the head tag. There are three areas in the head tag that you will want to be able to either edit yourself or have your SEO edit. They are the page title, the description meta tag and the keywords meta tag. Having control over these for each of your top level pages (all the pages linked to from your home page), will be critical to your websites success in the search engines. You definitely do not want these to be the same on every page (they must be unique and reflect the nature of the content on the given page). For other pages that will be created in high volumes, you will want to make sure that there is a variable string (your web designer/programmer will understand what this is) in place for each of the three areas in question, so that they will automatically be filled with content that is the right length and reflects the content on the given page.

Again, make sure that your designer/programmer understands that this is a requirement of the job.

4) Clean, simple code. Ideally you want to define all aesthetic properties that different types of text on your site are going to have in a separate CSS file. This means that you want to avoid using as many tags as possible, especially font, size and color tags. You also want to avoid creating PHP scripts that are either two long, and contain a lot of unnecessary steps, or ones that rely heavily on javascript. It is best to avoid using javascript as much as possible.

Remember, the most important thing your site can be doing is making it easy for search engine bots to easy scroll through the code of your website and follow all the links that it finds. When there is unnecessary code and script on your site, it makes it a lot harder for them, thus hurting your SEO campaign.

Make sure that whoever is helping you build your site understands that there job is to output the cleanest, simplest code possible. If you have any questions about this or don't understand it, it is best to talk to an SEO expert about it, to ensure that it is done properly.


5) Ensuring that visible written content is editable. The writing within the body of your site is one of the most important areas for you or your Optimizer to help your site increase it's rankings. Ensuring that either of you can edit it at your own convenience is extremely critical to the entire SEO campaign. This because from time to time search engine algorithms will change, and that might mean that a strategy that was implemented in the past might not suffice, so you must be able to change it to keep up with the most up to date SEO techniques.


6) Site structure. Considering the nature of PHP, your site will most likely (and should) be created by a series of includes that puts all of the pieces of a given page together. You want to ensure that the layout and placement of graphics and navigational links within these includes is strategically correct. Remember, you don't want to have to pay someone to go back and redo this, so it is critical to get it right the first time.