Cant delete from form with Javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Cant delete from form with Javascript

Post by JimiH »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello

I have a form which lists Name's & email's from a table in mySQL ("engineer")

On the form there are Add & Update record hyperlinks and checkboxes which when
checked will delete the corresponding record when the delete hyperlink is clicked.

However the record is not getting deleted?

Can anyone help.

Engineer.php

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Branch</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="ENGscript.js"> </script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>

<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td><hr size="1" noshade></td>
  </tr>
  <tr>
    <td>
      <form action="" method="post" name="" id="">
        <table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
          <tr>
            <td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All &nbsp;&nbsp;&nbsp;</td>
            <td colspan="3" align="center"><a href="EngineerADMIN.php?mode=add">Add New Branch </a></td>
          </tr>
          <tr>
            <td><strong><a href="javascript:goDel()">Delete</a></strong></td>
            <td><strong>Engineer</strong></td>
            <td><strong>Email</strong></td>
          </tr>
		  <?
		  include("conn.php");
		  $sql="select * from engineer order by Name";
		  $result=mysql_query($sql,$connection) or die(mysql_error());
		  while($row=mysql_fetch_array($result)) {
		  ?>
          <tr>
            <td><input name="<? echo $row['Name']; ?>" type="checkbox" class="check"></td>
            <td><? echo $row['Name']; ?></td>
		<td width="125"><? echo $row['email']; ?>&nbsp;</td>

            <td><a href="<? echo "EngineerADMIN.php?Name=".$row['Name']."&mode=update"; ?>">Update</a></td>
          </tr>
		  <? } ?>
          <tr>
            <td><strong><a href="javascript:goDel()">Delete</a></strong></td>
          </tr>
        </table>
    </form></td>
  </tr>
</table>
</body>
</html>
ENGdelete.php

Code: Select all

//error_reporting(E_ALL);
include("conn.php");
$recsno=$_GET["recsno"];
$data=trim($recsno);
$ex=explode(" ",$data);
$size=sizeof($ex);
for($i=0;$i<$size;$i++) {
	$id=trim($ex[$i]);
	$sql="delete from engineer where Name='$id'";
	$result=mysql_query($sql,$connection) or die(mysql_error());

//echo $result;
//echo $id;
//echo $recsno;
	
}
header("location: index.htm");
?>


ENGscript.js

Code: Select all

function goDel()
{
	var recslen =  document.forms[0].length;
	var checkboxes=""
	for(i=1;i<recslen;i++)
	{
		if(document.forms[0].elements[i].checked==true)
		checkboxes+= " " + document.forms[0].elements[i].name
	}
	
	if(checkboxes.length>0)
	{
		var con=confirm("Are you sure you want to delete");
		if(con)
		{
			document.forms[0].action="ENGdelete.php?recsno="+checkboxes
			document.forms[0].submit()
		}
	}
	else
	{
		alert("No record is selected.")
	}
}

function selectall()
{
//		var formname=document.getElementById(formname);

		var recslen = document.forms[0].length;
		
		if(document.forms[0].topcheckbox.checked==true)
			{
				for(i=1;i<recslen;i++) {
				document.forms[0].elements[i].checked=true;
				}
	}
	else
	{
		for(i=1;i<recslen;i++)
		document.forms[0].elements[i].checked=false;
	}
}


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Hello

Ok I fixed it for some reason the code was formatting the 'recsno'
trim, explode etc.

When this was matched with whats in the DB it didn't exist.

Example - SMITH JACK, would be passed as

Code: Select all

POST: Array
(
    [SMITH_JACK] => on
)

sql: delete from engineer where Name='SMITH'

POST: Array
(
    [SMITH_JACK] => on
)

sql: delete from engineer where Name='JACK'
I needed to delete 'SMITH JACK', not 'SMITH' or 'JACK'

Code: Select all

<?
error_reporting(E_ALL);
include("conn.php");
$recsno=$_GET["recsno"];
$data=trim($recsno);
$recsno=$data;
//$ex=explode(" ",$data);
//$size=sizeof($ex);
//for($i=0;$i<$size;$i++) {
	//$id=trim($ex[$i]);
    
	$sql="delete from engineer where Name='$recsno'";
	$result=mysql_query($sql,$connection) or die(mysql_error());
    
    
//echo '<pre>POST: '; print_r($_POST); echo "\n</pre>\n"; 
//echo '<pre>sql: '; print_r($sql); echo "\n</pre>\n"; 

//echo $result;
//echo $id;
//echo $recsno;
	
//}
header("location: index.htm");
?>
Thanks

Geoff
Post Reply