Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Form Elements : PHP Dynamic Drop Down 'OnChange' Event

If you've gone through the last blog post about the creating a dynamic drop down element in PHP, this post might be even greater to go through as this is intended to explain the way to dynamically load the content to the drop down as well as make an 'onChange()' event to load further dynamic content from the back end. Let's go through the tutorial and you'd understand. 

First, create the tables and insert data with the following MySQL scripts.

create table load_content
(
    id VARCHAR(10) NOT NULL,
    name VARCHAR(250) NOT NULL,
    CONSTRAINT pk_load_content PRIMARY KEY(id)
);

INSERT INTO load_content VALUES('101','Samitha');
INSERT INTO load_content VALUES('102','Janitha');
INSERT INTO load_content VALUES('103','Samitha');
INSERT INTO load_content VALUES('104','Janitha');


After running the scripts and creating the necessary MySQL tables in the database, you've to create the necessary Java Script and PHP files.

First Create the '2.php' file, with the Code provided below.

<html>
<head>
<script type="text/javascript" src="drop.js"></script>
<title>Notes of Harsha</title>
</head>

<body>

    <?php
  
  if(!isset($_GET['get_value'])) {
   
   
   echo '<form>';
   echo '<select name="load" onchange="load_content(this)">';
   include('db_config.php');  
   $query = "SELECT DISTINCT name FROM load_content";  
   $result = mysql_query($query);
   
   while($row = mysql_fetch_array($result)) {
    
     echo '<option value="'.$row['name'].'">';
     echo $row['name'];
     echo '</option>'; 
    
   }
   
   echo '</select>';
   echo '</form>';
  }
 
  if(isset($_GET['get_value'])) {
   
   $get_value = $_GET['get_value'];
   include('db_config.php');
   $query = "SELECT id FROM load_content WHERE name='$get_value'";
   $result = mysql_query($query);
   while($row = mysql_fetch_array($result)) {
     echo $row['id'].'<br/>';
   }
   
  }
?>

<div id="content_load"></div>
</body>
</html>


Then, create the javascript code in the 'drop.js' file and place it in the same directory as '2.php' is. This javascript code is intended to fire the function at the OnChange() event. It loads the output of the PHP code to the DIV id load_content.

function load_content(b)
{
 var str = b.options[b.selectedIndex].text;
 
 if (str=="") {
  
  document.getElementById("content_load").innerHTML="";
  return;
 }
 
 
 if (window.XMLHttpRequest) {
  
  xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
 }
 
 else {
  
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
 }
 
 xmlhttp.onreadystatechange=function() {
  
   if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    
   document.getElementById("content_load").innerHTML=xmlhttp.responseText;
   }
   
 }
 
  xmlhttp.open("GET","2.php?get_value="+str,true);
  
  xmlhttp.send();
}



Make sure to keep 'db_config.php' file in the same directory as other files does as we did in the previous PHP example with the loading dynamic content to drop down.



;&lt?php

 $host = "127.0.0.1";
 $uname = "root";
 $pword = "";
 $db_name = "test";

 mysql_connect( $host , $uname , $pword ) or die (mysql_error());
 mysql_select_db($db_name) or die(mysql_error()); 

?;&gt

Form Elements : PHP Creating Dynamic Drop Down

In this blog post, tried to expalin the method of loading data from the database to the Drop Down which is a static html form element.

Before doing this, let's understand how a typical Drop Down form element works.


<select name="test">
 <option value="Manager 1"> Manager 01</option>
 <option value="Manager 2"> Manager 02</option>
</select>



So, to load dynamically data to the dropdown you need to output these tags as PHP echo statements to the web page.


First, lets's create a proper database called 'test' and in it create a table called 'example'. Here's the MySQL script to do that,


create database test;


create table example
(
  name varchar(200) not null  
);

insert into example values('Harsha');

insert into example values('Danika');



After executing the above MySQL Script, you may need to proceed with the PHP Coding.


Here's the Database Configuration PHP Code which should be included in the Project Directory of the Current Project that we're working on and later should be included in the PHP code itself with PHP 'include()' . Name it as db_conn.php.

<?php

 $host = "127.0.0.1";
 $username = "root";
 $pword = "";
 $db_name = "test";
 
 
 mysql_connect( $host , $username , $pword ) or die(mysql_error());
 mysql_select_db($db_name) or die(mysql_error());
 
?>

Here's the PHP Code in the index.php file in order to perform the required task.


<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
</head> 
<body> 
<form name="form">

This is the Static Form Element 

<select name="test_static"> 

       <option value="Manager 1"> Manager 01</option>

       <option value="Manager 2">Manager 02</option> 

</select>
<br/> 
This is the Dynamic Form Element 

<select name="test_dynamic"> 

<?php 

      include('db_conn.php'); 

      $query = "SELECT name FROM example"; 

      $result = mysql_query($query); 

      while($row= mysql_fetch_array($result)) 
      {
          echo '<option value="'.$row['name'].'">';
          echo $row['name'];
          echo '</option>'; 
      }  
?> </select> </form> </body> </html>

Basic Signup from with PHP ?

Hi!

This will be helpful if you're a newbie or desperately finding some code to copy paste during your very first site project experience with PHP.

Anyway, try to learn something out of this as it's easy to understand as well as simple to follow. As usual the guide is given in steps so that you don't need to bother reading long sentences to keep going with the guide.

Let's go.

1. First, you need to create a Directory(Windows/Linux) to save your work & you've to keep this in your server. (if you're using localhost, keep it inside htdocs directory).

2. Then, run the following MYSQL code & create table to enter data.


create database phpdb;

use phpdb;

create table test(
email varchar(100) not null,
password varchar(20) not null
);


3. Then, Create a index.php file & put the following code in it.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>This is PHP Example</title>
</head>
<body>
<form action="signup.php" method="post">
<table width="629" height="191" border="1">
  <tr>
    <td width="241">Enter Email Address </td>
    <td width="372"><input type="text" name="txt_email" /></td>
  </tr>
  <tr>
    <td>Enter Password </td>
    <td><input type="password" name="txt_password" /></td>
  </tr>
  <tr>
    <td> </td>
    <td><input type="submit" value="Submit" name="btn_submit" /></td>
  </tr>
</table>
</form>
</body>
</html>


4. Then, create redirect.php & put the following code there.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>This is PHP File</title>
</head>
<body>
<?php $red = $_REQUEST["msg"]; echo $red."<br/>"; ?>
<br/>
<a href="index.php" target="_parent">GO HOME</a>
</body>
</html>


5. Then,create signup.php file & put the follwing code.

<?php

 $con = mysql_connect("localhost","root","");
 
 if(!$con)
 {
  $msg="Can't Connect to the Database ".mysql_error();
 }
 else
 {
  $dbCon = mysql_select_db("phpDB");
  
  if(!$dbCon)
  {
   $msg = "Can't Connect to the Table ".mysql_error();
  }
  else
  {
   $email = $_POST["txt_email"];
   $pword = $_POST["txt_password"];
   
   $query = mysql_query("INSERT INTO test VALUES('$email','$pword')");
   
   if(!$query)
   {
    $msg= "Can't Insert Values to the Table".mysql_error();
   }
   else
   {
    $msg = "Successfully Added to the Table !"; 
   }
  }
 }
 
 header("Location: redirect.php?msg=".$msg);
?>

Now, File Seems like follows inside the directory.




6. Now, run Web Browser & type http://localhost/folder in the address bar.


7. There's Few notes to make. In signup.php  line 3 can be replaced with your site url, username & password. Naming text boxes in index.php like :  
<input type="text" name="txt_email" .. is important in order to get their values in signup.php.

Enjoy the Code !

Install XAMPP in Ubuntu Linux ?

Hi!

If you need to install xampp in Ubuntu linux, here's a quick guide of how to do that. It's in steps ! So, it'll be easy to go through the article as usual in my blog.


1. First go through this link and download xampp for Linux.

2 . Now, you'll be having a file named like xampp-linux-y.y.yy.tar

3. Now, get your terminal.


     Type sudo nautilus & hit Enter




4. Now, File Manager window will be opened & you've to go to your root directory & find a directory opt & get inside it.



5. Then, take your xampp-linux-y.y.yy.tar & extract it inside /opt.


You'll see a directory named lampp.

6. Now, take out terminal again & type cd /opt .


7. Then, Type sudo chmod 777 lampp. (Give Read,write & execute privileges to all users).


8. Tpe sudo naulius & take back file manager window & go to /opt/lampp/htdocs.


 Make a directory to store your php files. (Name it the way you want) Ex: MYPHP.

9.  Then, take terminal back & type sudo /opt/lampp/lampp start. 


10. Start Web Browser & type http://localhost in the addressbar.


11. Then, Select English & Type http://localhost/MYPHP to go to your php file directory.




Enjoy! PHP with XAMPP.

Change PHP Error Levels in Your Web Site ?

Hey!

Have you ever wonder whether the PHP Error Messaging is important to you (developer) but not the end user ? but, it keeps coming ? to both ?

or

Have you ever wanted to display your own error message to the User instead of PHP default ?

or

Completely remove PHP Errors?


Then, this is the way to go. To change the error levels you must be PHP admin as well as must be able to access your php.ini file. For now, let's imagine you use xampp installed  in your system & you use localhost.

if you use Linux, you'll find your php.ini in /opt/lampp/etc

or

if you use windows you'll find it C:\xampp\php.

Open the file with admin privileges. (In Linux use sudo nautilus command in terminal) Then, find something like follows.

error_reporting = E_ALL | E_STRICT

display_errors = On


This statement is responsible for invoking all error,warnings & notices. E_ALL is a built in php constant & it stands for all errors.

Now, change on to off. Try changing following as well.

display_startup_errors = On  

Specially, you can comment one of these lines & check whether it works. Example is given below.


;error_reporting = E_ALL | E_STRICT
;display_errors = On
;display_startup_errors = On

Standard Commenting inside php.ini file is done using  ';' (Semicolon). During this process you'll need to stop and restart already running web server.

So,if you need to Change PHP Error Levels in a Script not in your Web Site. Then take a look at following Code.

        
<?php
            
        error_reporting(E_ALL & ~E_NOTICE);

        $var="2012-11-30";

        echo $var;

        echo $var2;

?>

or
        
<?php
            
        error_reporting(0);

        $var="2012-11-30";

        echo $var;

        echo $var2;

?>

In the above examples error_reporting() fundamentally changes error levels in the script itself.

To Change PHP Error message to a log. Go to php.ini again 7 Check for the following.
log_errors = Off

Change Off to On. Then, tell PHP to send error messages to a particular location by editing following code. Remove ; at the first place.

;error_log = filename

Set it as follows.

error_log = c:\temp\php_error_log