Hello forum readers,
I have a problem when I click on a custimizable alert box.
This alert box shows “Yes” and “No” as options.
When you click “No” the box closes.
When you click “Yes” the box closes and it has to do a submit form values and delete line in flat file. Without the alert box everything works just fine
What am I doing wrong?
delete.php
[code]<html>
<head>
<script type=”text/javascript” src=”Alert.js”></script>
</head>
<body>
<!– code to read flat file here – case “”; –>
<form method=”post” action=”‘.$self.’?do=delete_data&id=’.$id.’&name=’.$name.'” enctype=”multipart/form-data” onclick=”alert(‘Are you sure?’);return false;” >
<input type=”hidden” name=”do” value=”delete_data”>
<input type=”hidden” name=”name” id=”name” value=”‘.$name.'” />
<input type=”hidden” name=”id” id=”id” value=”‘.$id.'” />
<input type=”submit” name=”submit” value=”Delete” />
</form>
<!– code to delete file – case “delete_data”;
</body>
</html>
Alert.js code
[code]var ALERT_TITLE = “Conformation”;
var ALERT_BUTTON_TEXT = “YES”;
var ALERT_BUTTON_TEXT_CANCEL = “NO”;
if(document.getElementById) {
window.alert = function(textalert) {
createCustomAlert(textalert);
}
}
function createCustomAlert(textalert) {
d = document;
if(d.getElementById(“modalContainer”)) return;
mObj = d.getElementsByTagName(“body”)[0].appendChild(d.createElement(“div”));
mObj.id = “modalContainer”;
mObj.style.height = d.documentElement.scrollHeight + “px”;
alertObj = mObj.appendChild(d.createElement(“div”));
alertObj.id = “alertBox”;
if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + “px”;
alertObj.style.left = (d.documentElement.scrollWidth – alertObj.offsetWidth)/2 + “px”;
alertObj.style.visiblity=”visible”;
h1 = alertObj.appendChild(d.createElement(“h1”));
h1.appendChild(d.createTextNode(ALERT_TITLE));
p = alertObj.appendChild(d.createElement(“p”));
p.appendChild(d.createTextNode(textalert));
p.innerHTML = textalert;
btn = alertObj.appendChild(d.createElement(“a”));
btn.id = “closeBtn”;
btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
btn.href = “#”;
btn.focus();
btn.onclick = function() { removeCustomAlert();return false; }
alertObj.style.display = “block”;
btn_cancel = alertObj.appendChild(d.createElement(“a”));
btn_cancel.id = “closeBtn”;
btn_cancel.appendChild(d.createTextNode(ALERT_BUTTON_TEXT_CANCEL));
btn_cancel.href = “#”;
btn_cancel.focus();
btn_cancel.onclick = function() { cancelCustomAlert();return false; }
alertObj.style.display = “block”;
}
function removeCustomAlert() {
document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”));
var id = document.getElementById(“id”).value;
var name = document.getElementById(“name”).value;
window.location=’delete.php?do=delete_data&id=’+id+’&name=’+name;
}
function cancelCustomAlert() {
document.getElementsByTagName(“body”)[0].removeChild(document.getElementById(“modalContainer”));
}
Problem is in –> function removeCustomAlert() <–
I hope somebody can help me.
Thanks in advance
Regards
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
window.location='delete.php?do=delete_data&id='+id+'&name='+name;
case "delete_data";
// Get values of id and name out of url
if(strpos($_SERVER['REQUEST_URI'], '?') !== false)
{
list($file, $query) = explode('?', $_SERVER['REQUEST_URI']);
$pairs = explode('&', $query);
foreach($pairs as $p)
{
$value = $val = '';
list($value, $val) = explode('=', $p);
if($value=='id')
{
$id = $val;
}
if($value=='name')
{
$name = $val;
}
}
}
//Delete the line with id
$id = $_POST['id'];
$name = $_POST['name'];
$file = file($data_file);
$data = '';
for ($i=0; $i<count($file); $i++)
{
if (!strstr($file[$i],$id))
{
$data .= $file[$i];
}
}
$fp = fopen($data_file,"w");
<i> </i> if ($fp)
<i> </i> {
<i> </i> fwrite($fp,$data);
<i> </i> fclose($fp);
<i> </i> }
[code=php]<input type="hidden" name="id" id="'.$id.'" value="'.$id.'" />[/code]
hhdgfbcjeffch | name 1 | street 1 | city 1 | country 1
hhdfllhigjfch | name 2 | street 2 | city 2 | country 2
hhdgfbcjeffkg | name 3 | street 3 | city 3 | country 3
hhdgfbcjnsogj | name 4 | street 4 | city 4 | country 4
hhdgdjiiadmgf | name 5 | street 5 | city 5 | country 5
hhdgfbchsufkf | name 6 | street 6 | city 6 | country 6
hhddhfydsbfju | name 7 | street 7 | city 7 | country 7
hhdgfmcbhdyaq | name 8 | street 8 | city 8 | country 8
hhdgfbjfhfbds | name 9 | street 9 | city 9 | country 9
hhdopdjjrhsbj | name 10 | street 10 | city 10 | country 10
[code=php]<html>
<head>
<script type="text/javascript" src="Alert.js"></script>
<link rel="stylesheet" type="text/css" href="alert.css" />
</head>
<body bgcolor="000080">
<?php
$do = isset($_REQUEST['do']) ? trim($_REQUEST['do']) : "";
$self = $_SERVER['PHP_SELF'];
$data_file = 'address_book.txt';
switch ($do)
{
case "";
///////////////////////////////////////////////////////////////
// READ TEXT FILE //
///////////////////////////////////////////////////////////////
$file_handle = fopen($data_file, "rb");
while (!feof($file_handle) )
{
$line_of_text = fgets($file_handle);
$row = explode(' | ', $line_of_text);
$id = $row[0];
$name = $row[1];
$street = $row[2];
$city = $row[3];
$country = $row[4];
echo '<div width="400px" align="center">
<form method="post" action="'.$self.'?do=delete_data&id='.$id.'&name='.$name.'" onclick="alert('Are you sure?');return false;" >
<input type="hidden" name="do" value="delete_data">
<input type="text" name="name" id="name" value="'.$name.'" />
<input type="hidden" name="id" id="id" value="'.$id.'" />
<input type="submit" name="submit" value="Delete" />
</form>
</div>';
}
break;
///////////////////////////////////////////////////////////////
// DELETE LINE IN TEXT FILE //
///////////////////////////////////////////////////////////////
case "delete_data";
if (isset($_GET['id']))
{
$id = $_GET['id'];
}
if (isset($_GET['name']))
{
$name = $_GET['name'];
}
$file = file($data_file);
$data = '';
for ($i=0; $i<count($file); $i++)
{
if (!strstr($file[$i],$id))
{
$data .= $file[$i];
}
}
$fp = fopen($data_file,"w");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}
redir("$self","<span style="font-family: Century Gothic, Arial, Helvetica;color:#FFFFFF;font-size:14px;">$name deleted!</span><br /><br />");
break;
}
function redir($target,$msg)
{
global $background;
///////////////////////////////////////////////////////////////
// REDIRECT SHOWS UPDATE MSG //
///////////////////////////////////////////////////////////////
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="2; url=<?=$target?>">
</head>
<body>
<div align="center">
<span style="font-family: Century Gothic, Arial, Helvetica;color:#FFFFFF;font-size:14px">
<?=$msg?>Data being updated ...
</span>
</div>
</body>
</html>
<?php
exit;
}
?>[/code]
<script type="text/javascript" src="Alert.js"></script>
<link rel="stylesheet" type="text/css" href="alert.css" />
<style type="text/css">
.success { border: 1px solid green; }
.warning { border: 1px solid orange; }
.error { border: 1px solid red; }
</style>
unset($rows[$i]);
$fh = fopen($data_file,'w');
fwrite($fh,implode("n",$rows));
fclose($fh);
header("location: records.php?m=1");
function removeCustomAlert() {
document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
[COLOR="Red"]var id = document.getElementById("id").value;
var name = document.getElementById("name").value;[/COLOR]
window.location='delete.php?do=delete_data&id='+id+'&name='+name;
}[/quote]
Hello bsmbahamas,
I changed what you said but Firebug still says that the id is undefined or nul in the alert.js file lines 54.
With the code, I posted here, it works BUT the code in the alert.js file
doesn't take the right line to delete, it aways takes the first line in the text file (address_book.txt)
E.g
I want to delete "naam 7"
The url the alert.js script generates is
delete.php?do=delete_data&[COLOR="Red"]id=hhdgfbcjeffch&name=name 1[/COLOR] and line 1 (= name 1) is deleted in stead of line 7.
I hope you understand what I mean.
So something is wrong with generating the url but what ...
Hope I can find/get a solution for my problem.
Best regards[/QUOTE]
try changing the removeCustomAlert() function to alert the url and see if the url is correct..
function removeCustomAlert() {
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
alert('delete.php?do=delete_data&id='+id+'&name='+name);
}
[/QUOTE]
and also view source in the php page that display all the delete buttons and make sure that the id's are correct, usually if you put your mouse over the link or button it will show you the url, each should have a unique id.[/QUOTE]
[CODE]<a onclick="alert('Delete '.$id.' - '.$name.' ', 'id='.$id.'');return false;" href="delete_records.php?id='.$id.'">Delete</a>[/CODE]
it shows the right id and name. It showsif you echo out the $_GET at the top of delete.php and the correct id is being passed, then your delete.php script is not deleting the correct row.[/QUOTE]It gets the correct id and name sent in the url --> name 1 which is right because it's in the url generated in the alert box (delete.php?do=delete_data&id=hhdgfbcjeffch&name=name 1 Yes - No
so you gotta make sure the right id is being displayed in the source of the buttons, and also that your modal box is passing the right id via javascript, and also that delete.php is getting the right value by echoing out the $_GET or $_REQUEST, if all three of them are passing the id correctly then the delete part of the switch statement is the cause of the problem.[/QUOTE]The sourse code shows the right values in every field
[CODE]<a onclick="alert('Delete hhdgfbcjeffch - name 1 ', 'id=hhdgfbcjeffch');return false;" href="delete_records.php?id=hhdgfbcjeffch">Delete</a>
<a onclick="alert('Delete hhdfllhigjfch - name 2 ', 'id=hhdfllhigjfch');return false;" href="delete_records.php?id=hhdfllhigjfch">Delete</a>
<a onclick="alert('Delete hhdgfbcjeffkg - name 3 ', 'id=hhdgfbcjeffkg');return false;" href="delete_records.php?id=hhdgfbcjeffkg">Delete</a>
etc...[/CODE]
can you pm me the page address the page so i can have a look or post the current delete.php code along with the alert.js code so i can see the current setup?[/QUOTE]Do you mean the online url of the pages on a server ?
var row;"
btn.onclick = function() { removeCustomAlert();[COLOR="Red"]return false; }[/COLOR] [/quote]
function removeCustomAlert(row) {
document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
window.location='delete.php?do=delete_data&id='+row+'&name='+name;
}
<form method="post" action="'.$self.'?do=delete_data&id='.$id.'&name='.$name.'" onclick="alert('Delete '.$id.' - '.$name.' ... Are you sure?');row='.$id.';return false;" >
case "delete_data";
<i> </i> if (isset($_GET['id']))
<i> </i>{
<i> </i> $id = $_GET['id'];
<i> </i>}
<i> </i>if (isset($_GET['name']))
<i> </i>{
<i> </i> $name = $_GET['name'];
<i> </i>}
<i> </i> $data = file($data_file);
<i> </i> $data = array_map('trim',$data);
<i> </i> for ($i=0; $i < count($data); $i++)
<i> </i> {
<i> </i> $temp = explode(" | ",$data[$i]);
<i> </i> if( $temp[0] == $id ){
<i> </i> unset($data[$i]);
<i> </i> $entryName = $temp[1];
<i> </i> }
<i> </i> }
<i> </i> $fp = fopen($data_file,"w");
<i> </i> fwrite($fp,implode("n",$data));
<i> </i> fclose($fp);
Give credit where credit is due!!
Thanks to the excellent help AND a lot of patience of [B][COLOR="Green"] [/B], my problem has been solved. Hurray !!![SIZE="3"]bsmbahamas[/SIZE] [/COLOR]
[/quote]
in [B]delete.php[/B]
<i>
</i> case "delete_data";
<i> </i> if (isset($_REQUEST['id']))
<i> </i>{
<i> </i> $id = $_REQUEST['id'];
<i> </i>}
<i> </i>if (isset($_REQUEST['name']))
<i> </i>{
<i> </i> $name = $_REQUEST['name'];
<i> </i>}
<i> </i> $data = file($data_file);
<i> </i> $data = array_map('trim',$data);
<i> </i> for ($i=0; $i < count($data); $i++)
<i> </i> {
<i> </i> $temp = explode(" | ",$data[$i]);
<i> </i> if( $temp[0] == $id ){
<i> </i> unset($data[$i]);
<i> </i> $entryName = $temp[1];
<i> </i> }
<i> </i> }
<i> </i> $fp = fopen($data_file,"w");
<i> </i> fwrite($fp,implode("n",$data));
<i> </i> fclose($fp);
[/QUOTE]
one important change is that you should use $_REQUEST instead of GET or POST, ...[/QUOTE]
0.1.9 — BETA 5.21