Friday, 6 November 2015

PHP FORM HANDLING

02:07

PHP  FORM  HANDLING :

The PHP superglobals $_GET and $_POST are used to collect form-data.
PHP - A Simple HTML Form

The example below displays a simple HTML form with two input fields and a submit button:

Example

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.
To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this:
<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>
The output could be something like this:
Welcome John
Your email address is john.doe@example.com
The same result could also be achieved using the HTTP GET method:
Example

<html>
<body>

<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
and "welcome_get.php" looks like this:
<html>
<body>

Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>

</body>
</html>
The code above is quite simple. However, the most important thing is missing. You need to validate form data to protect your script from malicious code.
Think SECURITY when processing PHP forms!

This page does not contain any form validation, it just shows how you can send and retrieve form data.
However, the next pages will show how to process PHP forms with security in mind! Proper validation of form data is important to protect your form from hackers and spammers!
GET vs. POST

Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.
Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
$_GET is an array of variables passed to the current script via the URL parameters.
$_POST is an array of variables passed to the current script via the HTTP POST method.
When to use GET?

Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
GET may be used for sending non-sensitive data.
Note: GET should NEVER be used for sending passwords or other sensitive information!
When to use POST?

Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.
Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.



 PHP  FORM  VALIDATION  USING JAVASCRIPT  :



<html>
<head>
<title>
Simple Client Side Validation
</title>
<script  type="text/javascript">

function  valid()
{

if(myform.name.value=="")
{

alert("enter your name");

return false;

document.myform.name.focus();
}


if(myform.contact.value=="")
{
alert("enter your contact");
return false;
document.myform.contact.focus();
}
if(isNaN(myform.contact.value))
{

alert("enter numeric value in contact");
return false;
document.myform.contact.focus();
}

if(myform.city.value=="")
{

alert("enter your city");

return false;

document.myform.city.focus();

}

if(myform.email.value=="")
{

alert("enter your email");

document.myform.email.focus();

return false;

}



if(myform.address.value=="")
{

alert("enter your address");

document.myform.address.focus();

return false;

}

var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(!myform.email.value.match(mailformat))

{
alert("You have entered an invalid email address!");
document.myform.email.focus();
return false;
}


return true;
}

</script>

</head>
<body>
<form name="myform"  action="submit.php" method="post"  onsubmit="return(valid());" >
Name <input type="text" name="name"  >
contact<input type="text" name="contact">
city<input type=text   name="city">
email<input type=text name="email">
address<input type=text name="address"">
<input   type=submit     name=submit>
</form>
</body>
</html>

========================================================================

Form validation in  javascript  with   radio and checkbox validation code:

========================================================================
<html>
<head>
<title>
Simple Client Side Validation
</title>
<script  type="text/javascript">

function  valid()
{

if(myform.name.value=="")
{

alert("enter your name");

return false;

document.myform.name.focus();
}


if(myform.contact.value=="")
{
alert("enter your contact");
return false;
document.myform.contact.focus();
}
if(isNaN(myform.contact.value))
{

alert("enter numeric value in contact");
return false;
document.myform.contact.focus();
}

if(myform.city.value=="")
{

alert("enter your city");

return false;

document.myform.city.focus();

}

if(myform.email.value=="")
{

alert("enter your email");

document.myform.email.focus();

return false;

}



if(myform.address.value=="")
{

alert("enter your address");

document.myform.address.focus();

return false;

}

var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(!myform.email.value.match(mailformat))

{
alert("You have entered an invalid email address!");
document.myform.email.focus();
return false;
}

if (document.getElementById("hindi").checked == false &&  
        document.getElementById("english").checked == false && document.getElementById("marathi").checked == false)  
    {
        alert ('You did not choose any of the checkboxes yet.');
        return false;
    }

if(document.myform.gender.value=="")
{
alert ("select your gender");
return false;
}
return true;
}

</script>
</head>
<body>
<form name="myform"  action="submit.php" method="post"  onsubmit="return(valid());" >
Name <input type="text" name="name"  >
contact<input type="text" name="contact">
city<input type=text   name="city">
email<input type=text name="email">
address<input type=text name="address"">
gender <input type="radio"  name=gender value=male>MALE
<input type="radio" name=gender  value=female>FEMALE
language <input type="checkbox"  id=hindi name=language[]  value=hindi>HINDI
<input type="checkbox"  id=english name=language[]   value=english>ENGLISH
<input type="checkbox"  id=marathi name=language[]   value=marathi>MARATHI
<input   type=submit     name=submit>
</form>
</body>
</html>


FORM  VALIDATION  USING    PHP :

(1)first    write   code  for   "form.html"    file.

<form     action="validation.php"      method="post">

Name     <input type="text"         name="name">

Contact    <input type="text"          name="contact">

Email    <input type="text"             name="email">

<input type="submit"    name="submit"     value="submit"  >

</form>

(2)WRITE CODE FOR  “validation.php”  file:
<?php


$msg=array();






if(empty($_POST['email']))


{


$msg="enter your email";

}


$regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^";


if(!preg_match( $regex, $_POST['email'] ))


{

$msg="enter your valid email";



}




if(empty($_POST['contact']))

{



$msg="enter your contact";



}

if(strlen($_POST['contact'])>10)

{


$msg="accept only 10 digit";


}



if(!is_numeric($_POST['contact']))

{


$msg="accept only numbers only";


}



if(empty($_POST['name']))

{


$msg="enter your name";



}



if(empty($msg))

{


echo "successful";

}

else

{

echo $msg;


}


?>

From   Validation   to   Prevent   SQL   Injection   Attack  :
<html>
  <head>
      <style>
         .error {color: #FF0000;}
      </style>
   </head>
  
   <body>
      <?php
         // define variables and set to empty values
         $nameErr = $emailErr = $genderErr = $websiteErr = "";
         $name = $email = $gender = $class = $course = $subject = "";
        
         if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (empty($_POST["name"])) {
               $nameErr = "Name is required";
            }
            else
            {
               $name = test_input($_POST["name"]);
            }
           
            if (empty($_POST["email"])) {
               $emailErr = "Email is required";
            }
            else
            {
               $email = test_input($_POST["email"]);
              
               // check if e-mail address is well-formed
               if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                  $emailErr = "Invalid email format";
               }
            }
           
            if (empty($_POST["course"])) {
               $course = "";
            }
            else
            {
               $course = test_input($_POST["course"]);
            }
           
            if (empty($_POST["class"])) {
               $class = "";
            }
            else
            {
               $class = test_input($_POST["class"]);
            }
           
            if (empty($_POST["gender"])) {
               $genderErr = "Gender is required";
            }
            else
            {
               $gender = test_input($_POST["gender"]);
            }
           
            if (empty($_POST["subject"])) {
               $subjectErr = "You must select 1 or more";
            }
            else
            {
               $subject = $_POST["subject"];   
            }
         }
        
         function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
         }
      ?>
                               
      <h2>Absolute classes registration</h2>
     
      <p><span class="error">* required field.</span></p>
     
      <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         <table>
            <tr>
               <td>Name:</td>
               <td><input type="text" name="name">
                  <span class="error">* <?php echo $nameErr;?></span>
               </td>
            </tr>
           
            <tr>
               <td>E-mail: </td>
               <td><input type="text" name="email">
                  <span class="error">* <?php echo $emailErr;?></span>
               </td>
            </tr>
           
            <tr>
               <td>Time:</td>
               <td> <input type="text" name="course">
                  <span class="error"><?php echo $websiteErr;?></span>
               </td>
            </tr>
           
            <tr>
               <td>Classes:</td>
               <td> <textarea name="class" rows="5" cols="40"></textarea></td>
            </tr>
           
            <tr>
               <td>Gender:</td>
               <td>
                  <input type="radio" name="gender" value="female">Female
                  <input type="radio" name="gender" value="male">Male
                  <span class="error">* <?php echo $genderErr;?></span>
               </td>
            </tr>
           
            <tr>
               <td>Select:</td>
               <td>
                  <select name="subject[]" size="4" multiple>
                     <option value="Android">Android</option>
                     <option value="Java">Java</option>
                     <option value="C#">C#</option>
                     <option value="Data Base">Data Base</option>
                     <option value="Hadoop">Hadoop</option>
                     <option value="VB script">VB script</option>
                  </select>
               </td>
            </tr>
           
            <tr>
               <td>Agree</td>
               <td><input type="checkbox" name="checked" value="1"></td>
               <?php if(!isset($_POST['checked'])){ ?>
               <span class="error">* <?php echo "You must agree to terms";?></span>
               <?php } ?>
            </tr>
           
            <tr>
               <td>
                  <input type="submit" name="submit" value="Submit">
               </td>
            </tr>
           
         </table>
      </form>
     
      <?php
         echo "<h2>Your given values are as :</h2>";
         echo ("<p>Your name is $name</p>");
         echo ("<p> your email address is $email</p>");
         echo ("<p>Your class time at $course</p>");
         echo ("<p>your class info $class </p>");
         echo ("<p>your gender is $gender</p>");
        
         for($i=0; $i < count($subject); $i++)
         {
            echo($subject[$i] . " ");
         }
      ?>
 </body>

</html>
========================================================================
Another Example
========================================================================

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr =$commentErr= "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
  }
 
  if (empty($_POST["website"])) {
    $websiteErr = "enter your website url";
  } else {
    $website = test_input($_POST["website"]);
  }

  if (empty($_POST["comment"])) {
    $commentErr = "enter your comment";
  } else {
    $comment = test_input($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  Name: <input type="text" name="name">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  E-mail: <input type="text" name="email">
  <span class="error">* <?php echo $emailErr;?></span>
  <br><br>
  Website: <input type="text" name="website">
  <span class="error"><?php echo $websiteErr;?></span>
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <span class="error">* <?php echo $genderErr;?></span>
  <br><br>
  <input type="submit" name="submit" value="Submit">
</form>

<?php
if(empty($emailErr) && empty($nameErr) && empty($genderErr )&& empty($websiteErr))
{
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
}
?>

</body>
</html>

================================================================================================================================================
Form  using radio button:
================================================================================================================================================(1)write code for form.html:

<html>
<head><title>STUDENT RECORD</title></head>
<body>
<table>
<form action="form.php" method="post">
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>

</tr>

<tr>
<td>E-mail</td>
<td><input type="text" name="email"></td>
</tr>

<tr>
<td>Roll No</td>
<td><input type="text" name="roll"></td>
</tr>

<tr>
<td>Contact</td>
<td><input type="text" name="contact"></td>
</tr>

<tr>
<td>Gender</td>
<td><input type="radio" name="gender" value="Male"> Male<br>
<input type="radio" name="gender" value="Female">Female
</td>
</tr>

<tr>
<td>Total Marks</td>
<td><input type="text" name="marks"></td>
</tr>

<tr><td colspan=2><center><input type="submit" value="Insert Record"></center></tr>
</form>
</table>
</body>
</html>

output:


(2)write code for  "form.php"  file:

<?php
$con=mysql_connect("localhost","root","");
if($con)
{echo "It is connected";}
else{echo "not connected";}

mysql_select_db("batch",$con);

$name=$_POST["name"];
$email=$_POST["email"];
$roll=$_POST["roll"];
$contact=$_POST["contact"];
$gender=$_POST["gender"];
$marks=$_POST["marks"];

if($gender=="Male")
{
$gender="male";
}if($gender=="Female")
{
$gender="female";
}else
{
echo "select one option";
}



$sql="insert into student(name,email,roll,contact,gender,marks) values ('$name','$email','$roll','$contact','$gender','$marks')";

mysql_query($sql);

echo "record Inserted";

?>


========================================================================
  Now other practice with  Checkbox 
========================================================================

(1)write code for  form.html :
<html>
<head><title>STUDENT RECORD</title></head>
<body>
<table>
<form action="form2.php" method="post"  enctype="multipart/form-data">
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>E-mail</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Roll No</td>
<td><input type="text" name="roll"></td>
</tr>

<tr>
<td>Contact</td>
<td><input type="text" name="contact"></td>
</tr>

<tr>
<td>Gender</td>
<td><input type="radio" name="gender" value="Male"> Male<br>
<input type="radio" name="gender" value="Female">Female
</td>
</tr>

<tr><td>language</td><td><input type=checkbox  name=hindi  >Hindi<input type=checkbox  name=english>English<input type=checkbox name=marathi>Marathi</td></tr>

<tr>
<td>Job Title</td>
<td><input type="text" name="jobtitle"></td>
</tr>
<tr>
<td>upload your resume</td>
<td><input type="file" name="file"></td>
</tr>
<tr><td colspan=2><center><input type="submit" value="Insert Record"></center></tr>
</form>
</table>
</body>
</html>






output:

================================================================================================================================================
(2)write code for  form.php file :
<?php
$con=mysql_connect("localhost","root","");
if($con)
{echo "It is connected";}
else{echo "not connected";}

mysql_select_db("batch",$con);

$name=$_POST["name"];
$email=$_POST["email"];
$roll=$_POST["roll"];
$contact=$_POST["contact"];
$gender=$_POST["gender"];
$jobtitle=$_POST["jobtitle"];
if(isset($_POST['hindi']))
{
$language='hindi';

}
if(isset($_POST['english']))
{
$language='english';

}
if(isset($_POST['marathi']))
{
$language='marathi';

}
if(isset($_POST['hindi']) && isset($_POST['english']))
{
$language='hindi,english';
}
if(isset($_POST['hindi']) && isset($_POST['marathi']))
{
$language='hindi,marathi';
}
if(isset($_POST['english']) && isset($_POST['marathi']))
{
$language='english,marathi';
}
if(isset($_POST['hindi']) && isset($_POST['english']) && isset($_POST['marathi']))
{
$language='hindi,english,marathi';
}

if($gender=="Male")
{
$gender="Male";
}if($gender=="Female")
{
$gender="Female";
}else
{
echo "select one option";
}

$sql="insert into student(name,email,roll,contact,gender,jobtitle,language) values ('$name','$email','$roll','$contact','$gender','$jobtitle','$language')";

mysql_query($sql);

move_uploaded_file($_FILES['file']['tmp_name'],"resume/".$_FILES['file']['name']) or die(mysql_error());

echo "your record & resume uploaded successfully";


?>
================================================================================================================================================
Implode()  Function :
The implode function is used to "join elements of an array with a string".
The implode() function returns a string from elements of an array. It takes an array of strings and joins them together into one string using a delimiter (string to be used between the pieces) of your choice. 
The implode function in PHP is easily remembered as "array to string", which simply means that it takes an array and returns a string. It rejoins any array elements and returns the resulting string, which may be put in a variable.
Suppose you have an array like this $arr = Array ("A","E","I","O","U");

and you wish to combine it into a string, by putting the separator '-' between each element of the array.

How to do that?

$str = implode("-",$arr);

So your resulting string variable $str will be contain:
A-E-I-O-U

Syntax
implode (separator , array)
Example1

<html>
<
body bgcolor="pink">
<
h3>Implode Function</h3>
<?php
$arr=
array ('I','am','simple','boy!');
echo 
implode(" ",$arr);
?>
</
body>
</html>
Output
I am simple boy!




Explode () Function :
The explode function is used to "Split a string by a specified string into pieces i.e. it breaks a string into an array".
The explode function in PHP allows us to break a string into smaller text with each break occurring at the same symbol. This symbol is known as the delimiter. Using the explode command we will create an array from a string. The explode() function breaks a string into an array, but the implode function returns a string from the elements of an array.
For example you have a string 

$str="A E I O U";

now you want to make each name as an element of an array and access it individually so what you do:

$arr = explode(",", $str);

means : we have made pieces of string $text based on separator ','
and put the resulting array in variable $arr

So I used print_r ($arr); and the results are the following:

Array(
[0] => A
[1] => E
[2] => I
[3] => O
[4] => U
)

which is equal to:

$arr = Array ("A","E","I","O","U");

Syntax

explode (separator,string,limit)
 
 
 
Example1

<html>
<
body bgcolor="pink">
<
h3>Explode Function</h3>
<?php
$str="I am simple boy!";
print_r(explode(
" ",$str));
?>
</body>
</html>

Output:

Array ( [0] => I [1] => am [2] => simple [3] => boy! )


Simple example for multiple selection using dropdown   with  implode function :

(1)first write code for  select.html file:

<form action=select.php  method=post>

<select name=city[]  multiple="multiple">
<option  value=mumbai>mumbai</option>
<option  value=delhi>delhi</option>
<option  value=goa>goa</option>
</select>
<input type=submit name=submit>
</form>

output:



(2)now  write code for select.php file:

<?php

$data=implode(',',$_POST['city']);

$con=mysql_connect('localhost','root','');

mysql_select_db("batch",$con);

$sql="insert into  test(city)values('$data')";

mysql_query($sql) or die(mysql_error());

?>


 Simple example for multiple selection using  Checkboxes  with  implode function :

(1)first write code for checkbox.html file:

<form   action=checkbox.php     method=post>
mumbai<input type=checkbox  name=city[]  value=mumbai>
Delhi<input type=checkbox   name=city[]   value=delhi>
city<input type=checkbox   name=city[]      value=city>
<input type=submit  name=submit   value=submit>
</form>

output :



(2) write  code for checkbox.php  file  :
<?php

$data=implode(',',$_POST['city']);

$con=mysql_connect('localhost','root','');

mysql_select_db("batch",$con);

$sql="insert into  test(city)values('$data')";

mysql_query($sql) or die(mysql_error());

?>


======================================================================

How To Display Multiple Checkbox Value From Database In Php


Step 1:  

SQL>
create table  userrecord (
id int primary key ,
name   varchar(200)
);


Step 2: write code for checkbox.php file:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ankit";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn)
{
    die("Connection failed: " . mysqli_connect_error());
}

$sql="select * from userrecord";

$result=mysqli_query($conn,$sql);
while ($row=mysqli_fetch_array($result))
{

echo '<form action="select.php" method="post">

<input type="checkbox" name="check_list[]" value="'.$row['id'].'"><label> "'.$row['name'].'"</label><br/>';



}
echo '<input type="submit" name="submit" value="Submit"/>
</form>';

?>


Step 3: write code for   select.php  file:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ankit";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

if(isset($_POST['submit'])){//to run PHP script on submit


if(!empty($_POST['check_list'])){




 $data=implode(',',$_POST['check_list']);

echo $sql="select  *  from  userrecord  where  id  IN($data)";

$result=mysqli_query($conn,$sql);

while($row=mysqli_fetch_array($result))
{
echo "id=".$row['id'];
echo "name=".$row['name'];

}



// Loop to store and display values of individual checked checkbox.

}


}
?>

================================================================================================================================================





How to get the next auto-increment value


Do this simple things first create a table record:
For example:
Create table batch
(batchid int primary key atuo_increment,
name varchar(200),
contact varchar(200)
);
(2)insert a first record into your record table then your initial id will be 1 after that remaining code will atuomatically increment the next value that will be 2..3…4…5…6
<?php
$con=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“batch”,$con);
$get=mysql_query(“SELECT MAX(batchid) FROM batch”);
$got = mysql_fetch_array($get);
$next_id = $got[‘MAX(batchid)’] + 1;
echo ‘<form action=”” method=post>’;
echo ‘ID<input type=text name=id value=”‘.$next_id.'”>’;
echo ‘NAME<input type=text name=name value=””>’;
echo ‘CONTACT<input type=text name=contact value=””>’;
echo ‘<input type=submit name=submit value=”submit”>’;
echo ‘</form>’;
?>
output:
autoincrement on form

=======================================================================
HOW TO UPDATE CHECK BOX VALUES IN MYSQL DATABASE 
=======================================================================
(1)step one create  table :

CREATE TABLE IF NOT EXISTS `games` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `game_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
)

(2)now write code for updating checkbox  values   and give  file name  test.php  :

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("abhi",$con);
extract($_POST);
$check_exist_qry="select * from games";
$run_qry=mysql_query($check_exist_qry);
$total_found=mysql_num_rows($run_qry);
if($total_found >0)
{
    $my_value=mysql_fetch_assoc($run_qry);
    $my_stored_game=explode(',',$my_value['game_name']);
}

if(isset($submit))
{
    $all_game_value = implode(",",$_POST['games']);
    if($total_found >0)
    {
        //update
        $upd_qry="UPDATE games SET game_name='".$all_game_value."'";
        mysql_query($upd_qry);
        header('Location:test.php');

    }
    else
    {
        //insert
        $ins_qry="INSERT INTO games(game_name) VALUES('".$all_game_value."')";
        mysql_query($ins_qry);
    }
}

?>
<form method="post" action="">
Games You Like: <br/>
    <input type="checkbox" name="games[]"
value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
    <input type="checkbox" name="games[]"
value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
    <input type="checkbox" name="games[]"
value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>
    <input type="checkbox" name="games[]"
value="4" <?php if(in_array(4,$my_stored_game)){echo "checked";}?>><label>Rugby</label><br>
    <input type="checkbox" name="games[]"
value="5" <?php if(in_array(5,$my_stored_game)){echo "checked";}?>><label>Tennis</label><br>
    <input type="checkbox" name="games[]"
value="6" <?php if(in_array(6,$my_stored_game)){echo "checked";}?>><label>Cricket</label><br>
    <input type="checkbox" name="games[]"
value="7" <?php if(in_array(7,$my_stored_game)){echo "checked";}?>><label>Table Tennis</label><br>
    <input type="checkbox" name="games[]"
value="8" <?php if(in_array(8,$my_stored_game)){echo "checked";}?>><label>Hockey</label><br>
    <input type="submit" name="submit" value="submit">
</form>


output:





















How to submit form   without submit button:




(1)write   code for   without.php    file :
<html>
<head>
<script>
function submitform()
{
document.getElementById("myForm").submit();
alert("your form submitted");
}
</script>
</head>
<body>
<form method="post" action="yourdata.php" id="myForm">
<input type="text" name="n" placeholder="enter your name"/><br/>
<input type="text" name="e" placeholder="enter your email"/><br/>
<input type="text" name="m" placeholder="enter your mobile"/><br/>
<textarea name="reply" placeholder="enter your address"></textarea>
</form>
<div onclick="submitform()" style="color:blue;">Submit the form by clicking this</div>
</body>
</html>


(2)write code   for  “yourdata.php “  file:

<?php
echo "Welcome : ".$_POST['n']."<br/>";
echo "Your email : ".$_POST['e']."<br/>";
echo "Your mobile : ".$_POST['m']."<br/>";
echo "Your Adds : ".$_POST['reply'];

?>

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

6 comments:

  1. This is useful programming codes. Thanks for sharing this form handling blog post. Really useful for me. Best PHP training in Bangalore

    ReplyDelete
  2. hello to all of you. it is my first time that I have a comment on your post but I really wanted to say how great you are working. I am so happy for this post from your website.
    Link Building Company India

    ReplyDelete
  3. Hello, I am from Jonekest. Thank you so much, form all posts individual this one. I like your website and all your post. Your posts are informative for us.
    digital marketing company in india

    ReplyDelete
  4. I am glad to see this post on your website. It is beneficial for us and can be very useful in our daily life, and we always use your website information and

    experiences. Anyway, thank you so much.
    Seo Services Company In Delhi

    ReplyDelete
  5. Nutra Trials defines personal characteristics of different health products including skincare, weight loss, muscle and male enhancement. The study presented here is briefly described for reader convenience and to deliver them assurance with health standards. The best possible answers are given here regarding the selection of an ideal supplement or cream or serum that possibly remains to be safe for health and do not cause any side effects.

    ReplyDelete
  6. It is a great job, I like your posts and wish you all the best. and I hope you continue this job well.
    NutraT line

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top