Saturday, 28 September 2013

PHP Dynamic Select options Via Ajax


PHP Dynamic Select options Via Ajax is tutorial for those who want to make 2 dynamic select options in form. one is dependent on other and data fetched from MySQL Database.

Website for Complete Tutorial is Crewow




  1. Open your Dreamweaver or Notepad editor and create new PHP Page and name it myform.php
  2. Now Create only one Table for Categories like this
    • CREATE TABLE IF NOT EXISTS `category` (
        `SrNo` int(11) NOT NULL AUTO_INCREMENT,
        `Category` varchar(50) NOT NULL,
        `SubCategory` varchar(100) NOT NULL,
        PRIMARY KEY (`SrNo`)
      ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 
  3. Now insert some sample values like this
    • INSERT INTO `category` (`SrNo`, `Category`, `SubCategory`) VALUES
      (1, 'Electronics', 'Mobile'),
      (2, 'Electronics', 'Tablet'),
      (3, 'Electronics', 'IPad'),
      (4, 'Electronics', 'ITab'),
      (5, 'Electronics', 'Television'),
      (6, 'Electronics', 'Camera'),
      (7, 'Electronics', 'Laptop'),
      (8, 'Electronics', 'LCD'),
      (9, 'Electronics', 'Computer'),
      (10, 'Books', 'Magazines'),
      (11, 'Books', 'Journals'),
      (12, 'Books', 'Course Books'); 
  4. In myform.php for making Category Field use below PHP Lines to get data from Category Table. please write these lines inside <form></form> tags.
    • <?php 
      echo "<select name='Category' id='Category'>";
      echo "<option value='' disabled='' selected='' >--Select Category--</option>"; 
      $q6=mysql_query("select DISTINCT Category from category");
      while($r6=mysql_fetch_array($q6))
      {
      echo "<option value='$r6[0]' >$r6[0]</option>"; 
      }
      echo "</select>";
      ?>
  5. Now make Sub Category select options like below. DO NOT Worry about Sub category options, we will load sub category via Ajax later.
    • <?php 
      echo "<select name='SubCategory' id='SubCategory'>";
      echo "<option value='' disabled='' selected='' >--Select Sub Category--</option>";  
      echo "</select>";
      ?>
  6. Now we need to make another PHP Page in which we will run query for sub category fields. Name it Ajax_SubCategory.php
    • <?php
      Config.php file for connecting to PHP.
      include('config.php');
      id will be posted via Ajax script. please see point no.7
      if($_POST['id'])
      {
      $id=$_POST['id'];
      $sql=mysql_query("select SubCategory from category where Category='$id'");
       while($row=mysql_fetch_array($sql))
       {
       $data=$row['SubCategory'];
       echo '<option value="'.$data.'">'.$data.'</option>';
       }
      }
      ?>
  7. Now we will call Ajax_SubCategory.php via Ajax and Jquery in myform.php. please includeJquery in your myform.php
    • <script type="text/javascript">
       $(document).ready(function(){
      Below line will get value of Category and store in id
      $("#Category").change(function()
      {
      var id=$(this).val();
      var dataString = 'id='+ id;
      $.ajax
      ({
      type: "POST",
      url: "Ajax_SubCategory.php",
      data: dataString,
      cache: false,
      success: function(html)
      {
      This will get values from Ajax_SubCategory.php and show in Subcategory Select option
      $("#SubCategory").html(html);
      } 
      });
      });       
       });
      </script>    
      
Above tutorial can be used for making dynamic select option or options. You can also use some features of this tutorials. For more information please visit Crewow | Free Website Tutorials

Saturday, 7 September 2013

CSV Importer in MySQL

CSV importer is a PHP file which can help you to import all your data in MySQL Database using CSV File. Source File csv_importer.php is also available with this tutorial.



Source File link is Here

  1. Open your Dreamweaver or Notepad editor and create new PHP Page and name it import.php
  2. Now include csv_importer.php file in your page like below.
    • include ( "csv_importer.php" );
  3. For Connecting MySQL Database please write following line
    • $conn = @mysql_connect("localhost","root","password");
  4. Now choose your database like below
    • @mysql_select_db("yourdbname",$conn);
  5. Now write below lines . you just need to provide your table name where you want to import data. leave all other lines as it is.
    • create new importer object for importing data
      $c = new CSV_Importer;
      display log errors at end           
      $c->log_errors = true;
      skip the very first row in CSV file         
      $c->skip_top   = true;
      Type of Server (default MYSQL), you can also use this  MSSQL and PGSQL          
      $c->server     = MYSQL; 
      Database Table where File will be imported         
      $c->table      = "yourtablename";
  6. Now you have to set columns of table according to your CSV file Template
    • $c->SetColumnSequence("Field1,Field2,Field3,Field4,Field5");
  7. Here you will write csv file reference from which your data will be imported to Table
    • $result = $c->import("Your_CSV_Name.csv",$conn);
  8. Now include csv_importer.php file in your page like below.
    • if($result === FALSE)
      {
      there was some error importing data
      $c->DumpErrors();
      }
      else
      {
      Your data imported successfully, it will print number of rows inserted.
      print "Total records inserted are $result in table $c->table";
      } 
  9. in the end you will close MySQL Connection
    • @mysql_close();
Above tutorial is based on scriptbaba 2009 scripts. Now that website is not working so I write this tutorial for you. For more information please open  Crewow Website

Monday, 2 September 2013

PHP File Load Via Jquery into DIV

This Tutorial will show you how you can load PHP File via Jquery in any of your DIV for making your code neat and clean.



  1. You need to create a PHP Page.
  2. After that You will Downlaod Latest Jquery from Jquery Site and keep it in a folder name JS and include Link in your head section.
  3. Now in your page body Tag you will make a div and set id to showphpfile
  4. For Loading PHP Page in this Div you will write below code in script tag
    • $(document).ready(function(){
      $("#showphpfile").load('URL of your PHP Page');
      });
  • .load function is used for loading page in a div which id is showphpfile.
  • $(document).ready(function() function will do this when page load.
Website is Crewow