<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1868691262964271219</id><updated>2024-11-01T03:37:55.516-07:00</updated><category term="MYSQL"/><category term="PHP"/><category term="Confirm to leave the page"/><category term="Deleting Duplicate data from MySQL"/><category term="Remove Duplicate data row from MySQL"/><category term="javascript"/><category term="jquery"/><category term="onbeforeunload"/><title type='text'>PHP SOLVER</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://phpsolver.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1868691262964271219/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://phpsolver.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>shariful hasan</name><uri>http://www.blogger.com/profile/06857863212498289341</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1868691262964271219.post-3674868345750561679</id><published>2011-11-21T21:04:00.001-08:00</published><updated>2011-11-21T21:20:41.125-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Deleting Duplicate data from MySQL"/><category scheme="http://www.blogger.com/atom/ns#" term="MYSQL"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><category scheme="http://www.blogger.com/atom/ns#" term="Remove Duplicate data row from MySQL"/><title type='text'>Deleting Duplicate data from MySQL database in PHP</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I am making an application from data import from excel file. In application user may be import several time or different file may be contains same data. But I always need unique data row from other calculation. So I badly need delete Duplicated row from table after importing data from excel file.&lt;br /&gt;

For Solve this problem I found couple of solution on web.
&lt;br /&gt;
&lt;br /&gt;
  &lt;span class=&quot;cl_title&quot;&gt;Solution&lt;/span&gt;:1  If table contain a primary key &lt;br /&gt;
  Table : &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt; &lt;code class=&quot;CICodeFormatter&quot;&gt;
  create table TableName1 (
  ID int(11) NOT NULL AUTO_INCREMENT ,
  CODE varchar(20) , 
  NAME varchar(100) ,
  PRIMARY KEY (ID)
  ) 
  &lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;cs&quot;&gt;
Data&lt;/div&gt;
&lt;table border=&quot;0&quot;&gt;
  &lt;tbody&gt;
&lt;tr&gt;
    &lt;td&gt;ID&lt;/td&gt;
    &lt;td&gt;CODE&lt;/td&gt;
    &lt;td&gt;NAME&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;1&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1001&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Mahfuz&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;2&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1002&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Lemon&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;3&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1001&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Mahfuz&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;4&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1002&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Lemon&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;5&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1003&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Babu&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt; &lt;code class=&quot;CICodeFormatter&quot;&gt;&amp;lt;?php
 $conn = mysql_connect(&quot;localhost&quot;, &quot;mysql_user&quot;, &quot;mysql_password&quot;);
if (!$conn) {
    echo &quot;Unable to connect to DB: &quot; . mysql_error();
    exit;
}
if (!mysql_select_db(&quot;mydbname&quot;)) {
    echo &quot;Unable to select mydbname: &quot; . mysql_error();
    exit;
}



$sql = &quot;DELETE FROM TableName1
USING TableName1, TableName1 AS vtable
WHERE vtable.id &amp;gt; TableName1.id
AND TableName1.CODE = vtable.CODE
And TableName1.NAME = vtable.NAME&quot;;

mysql_query($sql);

$sql_check = &quot;SELECT NAME,
COUNT(NAME) AS NumOccurrences
FROM TableName1
GROUP BY NAME
HAVING ( COUNT(NAME) &amp;gt; 1 )&quot;;
$result = mysql_query($sql_check);

if (mysql_num_rows($result) == 0) {
    echo &quot;No Duplicate rows found&quot;;
}
?&amp;gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;span class=&quot;cl_title&quot;&gt;Solution 2&lt;/span&gt;: If table have no primary key &lt;br /&gt;
  &lt;span class=&quot;cs&quot;&gt;Table &lt;/span&gt;: &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt; &lt;code class=&quot;CICodeFormatter&quot;&gt;create table TableName1 ( 
  CODE varchar(20) , 
  NAME varchar(100)
)&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;cs&quot;&gt;
Data&lt;/div&gt;
&lt;table border=&quot;0&quot;&gt;
  &lt;tbody&gt;
&lt;tr&gt;
  
    &lt;td&gt;CODE&lt;/td&gt;
    &lt;td&gt;NAME&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
  
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1001&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Mahfuz&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
   
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1002&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Lemon&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
  
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1001&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Mahfuz&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
   
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1002&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Lemon&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;A1003&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;data&quot;&gt;Babu&lt;/span&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt; &lt;code class=&quot;CICodeFormatter&quot;&gt;&amp;lt;?php

$conn = mysql_connect(&quot;localhost&quot;, &quot;mysql_user&quot;, &quot;mysql_password&quot;);
if (!$conn) {
    echo &quot;Unable to connect to DB: &quot; . mysql_error();
    exit;
}
if (!mysql_select_db(&quot;mydbname&quot;)) {
    echo &quot;Unable to select mydbname: &quot; . mysql_error();
    exit;
}



$sql1 = &quot; CREATE TEMPORARY TABLE temp_TableName1 as select * from TableName1 group By code,name&quot;;

mysql_query($sql1);

$sql2 = &quot; TRUNCATE TABLE TableName1 &quot;;

mysql_query($sql2);

$sql3 = &quot; INSERT INTO TableName1 SELECT * FROM temp_TableName1 &quot;;

mysql_query($sql3);

$sql_check = &quot;SELECT NAME,
COUNT(NAME) AS NumOccurrences
FROM TableName1
GROUP BY NAME
HAVING ( COUNT(NAME) &amp;gt; 1 )&quot;;
$result = mysql_query($sql_check);

if (mysql_num_rows($result) == 0) {
    echo &quot;No Duplicate rows found&quot;;
}

?&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;
 ul li {
 color: #00F;
}
 ul li h2{
 padding:0;
 margin: 0;
 font-size: 14px;
}
 ul li span {
 color: #000;
}
.cs {
 font-family: &quot;Courier New&quot;, Courier, monospace;
 color: #09F;
}
pre.CICodeFormatter{
 font-family:arial;
 font-size:12px;
 border:1px dashed #CCCCCC;
 width:99%;
 height:auto;
 overflow:auto;
 background:#f0f0f0;
 line-height:20px;
 background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgac-7jzNrWJPMw9ot4kuHDJeqTeugIt2rs2_Vf5YaSlXG-jwEp1mPJD4Rx74AVUJOZ786IGPZZv16tJdowkZ2igwbSBIatSeDdgtQFOUrsSia4ZPnnJwa4RAX7QBGfhaHFV3EXR7ELPZ6Q/s320/codebg.gif);
 padding:0px;
 color:#000000;
 text-align:left;
}
pre.CICodeFormatter code{
 color:#000000;
 word-wrap:normal;
}
.code_red {
 color: #FF0000;
}
.cl_title {
 font-weight: bold;
 color: #006;
 font-size: 18px;
}
.data {
 font-family: &quot;Courier New&quot;, Courier, monospace;
}
&lt;/style&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://phpsolver.blogspot.com/feeds/3674868345750561679/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://phpsolver.blogspot.com/2011/11/deleting-duplicate-data-from-mysql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1868691262964271219/posts/default/3674868345750561679'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1868691262964271219/posts/default/3674868345750561679'/><link rel='alternate' type='text/html' href='http://phpsolver.blogspot.com/2011/11/deleting-duplicate-data-from-mysql.html' title='Deleting Duplicate data from MySQL database in PHP'/><author><name>shariful hasan</name><uri>http://www.blogger.com/profile/06857863212498289341</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1868691262964271219.post-6003209229118000893</id><published>2011-11-19T08:02:00.001-08:00</published><updated>2011-11-19T13:56:32.653-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MYSQL"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>Difference between mysql_fetch_assoc, mysql_fetch_row,mysql_fetch_object and mysql_fetch_array</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;style type=&quot;text/css&quot;&gt;
 ul li {
 color: #00F;
}
 

 ul li span {
 color: #000;
}
.cs {
 font-family: &quot;Courier New&quot;, Courier, monospace;
 color: #09F;
}
pre.CICodeFormatter{
 font-family:arial;
 font-size:12px;
 border:1px dashed #CCCCCC;
 width:99%;
 height:auto;
 overflow:auto;
 background:#f0f0f0;
 line-height:20px;
 background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgac-7jzNrWJPMw9ot4kuHDJeqTeugIt2rs2_Vf5YaSlXG-jwEp1mPJD4Rx74AVUJOZ786IGPZZv16tJdowkZ2igwbSBIatSeDdgtQFOUrsSia4ZPnnJwa4RAX7QBGfhaHFV3EXR7ELPZ6Q/s320/codebg.gif);
 padding:0px;
 color:#000000;
 text-align:left;
}
pre.CICodeFormatter code{
 color:#000000;
 word-wrap:normal;
}
.code_red {
 color: #FF0000;
}
.cl_title {
 font-weight: bold;
 color: #006;
 font-size: 18px;
}
&lt;/style&gt;

&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
&lt;h1 class=&quot;cl_title&quot;&gt;












Definition : &lt;/h1&gt;
&lt;h2&gt;




mysql_fetch_array():-&lt;/h2&gt;
Fetch a result row as an associative  array, a numeric array, or both by default it fetches both.
&lt;br /&gt;
&lt;blockquote&gt;
Returns  an array of strings that corresponds to the fetched row, or&amp;nbsp;&lt;span class=&quot;cs&quot;&gt;FALSE&lt;/span&gt; &amp;nbsp;if there are no more rows. The type of  returned array depends on how&amp;nbsp;&lt;em&gt;result_type&lt;/em&gt;is defined. By  using&amp;nbsp;&lt;strong class=&quot;cs&quot;&gt;MYSQL_BOTH&lt;/strong&gt;&amp;nbsp;(default), you&#39;ll get an array with  both associative and number indices. Using&amp;nbsp;&lt;strong class=&quot;cs&quot;&gt;MYSQL_ASSOC&lt;/strong&gt;, you only  get associative indices (as&amp;nbsp;mysql_fetch_assoc()&amp;nbsp;works),  using&amp;nbsp;&lt;strong class=&quot;cs&quot;&gt;MYSQL_NUM&lt;/strong&gt;, you only  get number indices (as&amp;nbsp;mysql_fetch_row()&amp;nbsp;works).&lt;/blockquote&gt;
&lt;h2&gt;
mysql_fetch_row() :- &lt;/h2&gt;
Get a result row as an numeric array
&lt;blockquote&gt;
Returns an numerical array of strings that corresponds to the  fetched row, or&lt;span class=&quot;cs&quot;&gt; FALSE&lt;/span&gt;&amp;nbsp;if there are no more rows.&lt;br /&gt;
&lt;span class=&quot;cs&quot;&gt;mysql_fetch_row()&lt;/span&gt;&amp;nbsp;fetches one row of data from the result  associated with the specified result identifier. The row is returned as an  array.&lt;/blockquote&gt;
&lt;h2&gt;mysql_fetch_object :- &lt;/h2&gt;
Fetch a result row as an object
&lt;blockquote&gt;
Returns an&amp;nbsp;object&amp;nbsp;with  string properties that correspond to the fetched row, or&lt;span class=&quot;cs&quot;&gt;FALSE&lt;/span&gt;;if  there are no more rows. &lt;/blockquote&gt;
&lt;h2&gt;
mysql_fetch_assoc() :-&lt;/h2&gt;
Fetch a result row as an associative  array
&lt;blockquote&gt;
Returns an  associative array that corresponds to the fetched row and moves the internal  data pointer ahead.&amp;nbsp;&lt;span class=&quot;cs&quot;&gt;mysql_fetch_assoc()&lt;/span&gt;&amp;nbsp;is equivalent to callingmysql_fetch_array() with &lt;span class=&quot;cs&quot;&gt;MYSQL_ASSOC&lt;/span&gt; for the  optional second parameter. It only returns an associative array.&lt;/blockquote&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h1 class=&quot;cl_title&quot;&gt;


Example:&lt;/h1&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt;&lt;code class=&quot;CICodeFormatter&quot;&gt;&lt;span class=&quot;code_red&quot;&gt;&amp;lt;?php&lt;/span&gt;

$conn = mysql_connect(&quot;localhost&quot;, &quot;mysql_user&quot;, &quot;mysql_password&quot;);

if (!$conn) {
    echo &quot;Unable to connect to DB: &quot; . mysql_error();
    exit;
}
  
if (!mysql_select_db(&quot;mydbname&quot;)) {
    echo &quot;Unable to select mydbname: &quot; . mysql_error();
    exit;
}

$sql = &quot;SELECT id as userid, fullname, userstatus 
        FROM   sometable
        WHERE  userstatus = 1&quot;;

$result = mysql_query($sql);

if (!$result) {
    echo &quot;Could not successfully run query ($sql) from DB: &quot; . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo &quot;No rows found, nothing to print so am exiting&quot;;
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you&#39;re expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you&#39;ll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_array ($result)) {
    echo $row[0];
    echo $row[1];
    echo $row[2];
}

while ($row = mysql_fetch_array ($result)) {
    echo $row[&quot;userid&quot;];
    echo $row[&quot;fullname&quot;];
    echo $row[&quot;userstatus&quot;];
}

while ($row = mysql_fetch_array ($result)) {
    echo $row[0];
    echo $row[&quot;fullname&quot;];
    echo $row[2];
}

while ($row = mysql_fetch_row($result)) {
    echo $row[0];
    echo $row[1];
    echo $row[2];
}

while ($row = mysql_fetch_object($result)) {
    echo $row-&amp;gt;userid;
    echo $row-&amp;gt;fullname;
    echo $row-&amp;gt;userstatus;
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row[&quot;userid&quot;];
    echo $row[&quot;fullname&quot;];
    echo $row[&quot;userstatus&quot;];
}


mysql_free_result($result);
&lt;span class=&quot;code_red&quot;&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;cl_title&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;cl_title&quot;&gt;
Performance&lt;/span&gt;

&lt;br /&gt;
&lt;blockquote&gt;
Using both &amp;nbsp;&lt;strong&gt;mysql_fetch_array() &lt;/strong&gt;and&lt;strong&gt; mysql_fetch_assoc()&lt;/strong&gt;&amp;nbsp;&amp;nbsp;is&amp;nbsp;&lt;em&gt;&lt;strong&gt;not  significantly&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;slower than using&amp;nbsp;&lt;strong&gt;mysql_fetch_row()&lt;/strong&gt;,&lt;/blockquote&gt;
&lt;blockquote&gt;
Speed-wise, the function is identical to&amp;nbsp;&lt;strong&gt;mysql_fetch_array()&lt;/strong&gt;, and almost as  quick as&amp;nbsp;&lt;strong&gt;mysql_fetch_row()&lt;/strong&gt;&amp;nbsp;(the  difference is insignificant).&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;strong&gt;mysql_fetch_object()&lt;/strong&gt;&amp;nbsp;is similar to&amp;nbsp;&lt;strong&gt;mysql_fetch_array()&lt;/strong&gt;, with one  difference - an object is returned, instead of an array. Indirectly, that means  that you can only access the data by the field names, and not by their offsets  (numbers are illegal property names).&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://phpsolver.blogspot.com/feeds/6003209229118000893/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://phpsolver.blogspot.com/2011/11/difference-between-mysqlfetchassoc.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1868691262964271219/posts/default/6003209229118000893'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1868691262964271219/posts/default/6003209229118000893'/><link rel='alternate' type='text/html' href='http://phpsolver.blogspot.com/2011/11/difference-between-mysqlfetchassoc.html' title='Difference between mysql_fetch_assoc, mysql_fetch_row,mysql_fetch_object and mysql_fetch_array'/><author><name>shariful hasan</name><uri>http://www.blogger.com/profile/06857863212498289341</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1868691262964271219.post-761373744817149348</id><published>2011-11-16T02:55:00.001-08:00</published><updated>2011-11-17T00:42:11.712-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Confirm to leave the page"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="jquery"/><category scheme="http://www.blogger.com/atom/ns#" term="onbeforeunload"/><title type='text'>How can I Confirm to leave the page when someone insert / editing a form in php</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;style type=&quot;text/css&quot;&gt;
pre.CICodeFormatter{
 font-family:arial;
 font-size:12px;
 border:1px dashed #CCCCCC;
 width:99%;
 height:auto;
 overflow:auto;
 background:#f0f0f0;
 line-height:20px;
 background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgac-7jzNrWJPMw9ot4kuHDJeqTeugIt2rs2_Vf5YaSlXG-jwEp1mPJD4Rx74AVUJOZ786IGPZZv16tJdowkZ2igwbSBIatSeDdgtQFOUrsSia4ZPnnJwa4RAX7QBGfhaHFV3EXR7ELPZ6Q/s320/codebg.gif);
 padding:0px;
 color:#000000;
 text-align:left;
}
pre.CICodeFormatter code{
 color:#000000;
 word-wrap:normal;
}
&lt;/style&gt;


&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Helvetica Neue&#39;, Arial, Helvetica, sans-serif;&quot;&gt;I&#39;m creating an PHP web application and I want to implement logic to warn the user when they are navigating away from a page they&#39;ve edited. I found some solution on the web .&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Helvetica Neue&#39;, Arial, Helvetica, sans-serif;&quot;&gt;For this solution we bound a JavaScript function to the unload event:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt;&lt;code class=&quot;CICodeFormatter&quot;&gt; window.onbeforeunload  
&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Helvetica Neue&#39;, Arial, Helvetica, sans-serif;&quot;&gt;I use following code in different aspect&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Helvetica Neue&#39;, Arial, Helvetica, sans-serif;&quot;&gt;For all type of Input .
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt;&lt;code class=&quot;CICodeFormatter&quot;&gt;
$(document).ready(function() { 
    $(&quot;:input&quot;).one(&quot;change&quot;, function() { 
        window.onbeforeunload = function() { return &#39;You will lose data changes.&#39;; } 
    }); 
    $(&#39;.noWarn&#39;).click(function() { window.onbeforeunload = null; }); 
});
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Your input elements probably do not exist when the code is executed. Try using the .live function to detect changes on all input elements, or wrap your code in a $(document).ready() handler.
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt;&lt;code class=&quot;CICodeFormatter&quot;&gt;
$(&#39;input&#39;).live(&quot;change&quot;, function () {
    window.onbeforeunload = function () { return &quot;Your changes have not been saved?&quot; };
});
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
For specific input type on body
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt;&lt;code class=&quot;CICodeFormatter&quot;&gt;
$(document).ready(function() {

    //----------------------------------------------------------------------
    // Don&#39;t allow us to navigate away from a page on which we&#39;re changed
    //  values on any control without a warning message.  Need to class our 
    //  save buttons, links, etc so they can do a save without the message - 
    //  ie. CssClass=&quot;noWarn&quot;
    //----------------------------------------------------------------------
    $(&#39;input:text,input:checkbox,input:radio,textarea,select&#39;).one(&#39;change&#39;, function() {
        $(&#39;BODY&#39;).attr(&#39;onbeforeunload&#39;,
        &quot;return &#39;Leaving this page will cause any unsaved data to be lost.&#39;;&quot;);
    });

    $(&#39;.noWarn&#39;).click(function() { $(&#39;BODY&#39;).removeAttr(&#39;onbeforeunload&#39;); });

});
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Best solution for form base:
In this solution if you are change something in your form then when you want to leave from this page user can get a message for confirmation. 
&lt;br /&gt;
&lt;pre class=&quot;CICodeFormatter&quot;&gt;&lt;code class=&quot;CICodeFormatter&quot;&gt;
function formIsDirty(form)
      {
          for (var i = 0; i &amp;lt; form.elements.length; i++)
          {
              var element = form.elements[i];
              var type = element.type;
              if (type == &quot;checkbox&quot; || type == &quot;radio&quot;)
              {
                  if (element.checked != element.defaultChecked)
                  {
                      return true;
                  }
              }
              else if (type == &quot;hidden&quot; || type == &quot;password&quot; || type == &quot;text&quot; ||
                       type == &quot;textarea&quot;)
              {
                  if (element.value != element.defaultValue)
                  {
                      return true;
                  }
              }
              else if (type == &quot;select-one&quot; || type == &quot;select-multiple&quot;)
              {
                  for (var j = 0; j &amp;lt; element.options.length; j++)
                  {
                      if (element.options[j].selected !=
                          element.options[j].defaultSelected)
                      {
                          return true;
                      }
                  }
              }
          }
          return false;
      }

      window.onbeforeunload = function(e)
      {
          e = e || window.event;  
// You put your from ID
          if (formIsDirty(document.forms[&#39;fromid&#39;])) 
          {
              // For IE and Firefox
              if (e)
              {
                  e.returnValue = &quot;You have unsaved changes.&quot;;
              }
              // For Safari
              return &quot;You have unsaved changes.&quot;;
          }
      };

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://phpsolver.blogspot.com/feeds/761373744817149348/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://phpsolver.blogspot.com/2011/11/how-can-i-confirm-to-leave-page-when.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1868691262964271219/posts/default/761373744817149348'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1868691262964271219/posts/default/761373744817149348'/><link rel='alternate' type='text/html' href='http://phpsolver.blogspot.com/2011/11/how-can-i-confirm-to-leave-page-when.html' title='How can I Confirm to leave the page when someone insert / editing a form in php'/><author><name>shariful hasan</name><uri>http://www.blogger.com/profile/06857863212498289341</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>