/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Alert box – parsing values

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>
[/code]

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”));
}[/code]

Problem is in –> function removeCustomAlert() <–
I hope somebody can help me.

Thanks in advance

Regards

to post a comment
JavaScript

35 Comments(s)

Copy linkTweet thisAlerts:
@bsmbahamasJun 20.2012 — you'd need to show more code, this seems to be ok. but why not just have the button be an image that links directly to the php file with the query string already hardcoded like this:

<a href=''delete.php?do=delete_data&id=id&name=name'><img src='yes.jpg'></a>

even if the query needs to be dynamic, you can just use javascript to dynamically change the url or php to load the page with the query already in place.
Copy linkTweet thisAlerts:
@coderunnerauthorJun 20.2012 — Hello bsmbahamas,

I changed the code in my first post.

Does this give you a better idea of my code ?

Can you help me please to solve my problem ?

Regards
Copy linkTweet thisAlerts:
@bsmbahamasJun 20.2012 — that's much better, but with regard to the removeCustomAlert() function,

where are the html elements with the ids of 'id' and 'name' that you are trying to get the value of? i assume that this info is coming from form fields since you are using .value instead of .innerHTML in this case, but the code you provided does not have any elements with an id of 'id' or 'name'. when you mouse over the yes/no buttons you'll see that no query string is coming up either

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;


}
Copy linkTweet thisAlerts:
@bsmbahamasJun 20.2012 — do you really want to dynamically create a custom alert via dom manipulation versus just hard coding the whole custom alert in the html of the page and using javascript and css to toggle it's visibility or display property, and moving it into place on the screen? you can still dynamically change the url of the buttons with javascript when the alert box is called
Copy linkTweet thisAlerts:
@coderunnerauthorJun 20.2012 — Hello bsmbahamas,

I have feeling that my case "delete_data"; doesn't read the values in the url because I can see the values in the url

delete.php?do=delete_data&id=0012&name=Johson

But how can I make my case "delete_data"; get the values out of the url

So far I have this but how to get the id in my delete script ?

case "delete_data" 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('&amp;', $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&lt;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> }


Can you help me ?

Regards
Copy linkTweet thisAlerts:
@bsmbahamasJun 20.2012 — try echoing out the info so you can see what's coming through:

echo "<p><pre>"; print_r($_GET); echo "</pre></p>";

echo "<p><pre>"; print_r($_
POST); echo "</pre></p>";

echo "<p><pre>"; print_r($_REQUEST); echo "</pre></p>";

if they are not submitting a form then it would come though via GET otherwise it would come through POST, (REQUEST shows both) you could probably just use $id = $_GET['id'] and $name = $_GET['name'] to retrieve the values from the url after the link is clicked.

this code would check that the values are set and not empty/false in the url.

if(

isset( $_GET['id'] ) && !empty( $_GET['id'] ) &&

isset( $_GET['name'] ) && !empty( $_GET['name'] )

){

$id = $_GET['id'] ;

$name = $_
GET['name']

} else {

die("<p>MSG: both id and name are not set!</p>");

}

gotta run, i'll check your progress later or tomorrow
Copy linkTweet thisAlerts:
@coderunnerauthorJun 21.2012 — Hello bsmbahamas,

Thank you for helping me.

I already echo-ed the values and it worked.

I think I got a little closer to a solution but the problem is that the javascript in the alert script sends the wrong value in the url ... always the first one so I tried to give the id an unique value with adding id="'.$id.'" in the form
[code=php]<input type="hidden" name="id" id="'.$id.'" value="'.$id.'" />[/code]
but then Firebug says that the id is undefined in the alert script line 54 and 55

How can I fix this?

I send you the complete code

The text database looks like thishhdgfbcjeffch | 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


The delete.php I have now is[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]


The alert.js is in one of my earlier posts.

I hope you (or somebody else of course) can tell me or give a working code.

Thanks in advance

Regards
Copy linkTweet thisAlerts:
@bsmbahamasJun 21.2012 — the php code for the form is not correct...

this code:

<input type="hidden" name="id" id="'.$id.'" value="'.$id.'" />

should be:

<input type="hidden" name="id" id="<?php echo $id; ?>" value="<?php echo $id; ?>" />

or alternatively:

<?php

echo '<input type="hidden" name="id" id="$id" value="$id" />';

?>

otherwise you won't have any value being passed as the php must use echo at some point.

also you don't need to include 'id' and 'name' in the query string of your forms action attribute,

since you already have them in the hidden fields, they may over write each other.

i'd also seperate the scripts so you'd have one page that shows the the text file contents(records.php)

and a second script that handles the deleting (delete.php) and then redirects back to records.php
Copy linkTweet thisAlerts:
@bsmbahamasJun 21.2012 — i have 2 other questions:

  • 1. why are you displaying the data in forms if you are only giving an option to delete the info? you could just show plain text and give a plain text delete link.


  • 2. why are you using a switch statement if the only option is to delete?


  • i'd restructure the whole thing to simply read the flat file database and show the records onscreen as plain text with a delete link next to it in records.php, if they click delete they could still be shown the javascript custom popup and then be redircted to delete.php where the deleting happens then redirect them back to records.php

    if you are planningon alowing them to update and delete records from the same page then the current structure is logical, but you'll need to add a good bit more code to handle updating.

    something like this...

    records.php

    <html>

    <head>

    <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>

    </head>

    <body bgcolor="000080">

    <?php

    /* READ TEXT FILE */

    $data_file = 'address_book.txt';

    $rows = file($data_file);

    if( $_GET['m'] == 1 ){

    echo '<p class="success">Record Deleted!</p>';

    }

    echo '<table width="400" cellpadding="5" cellspacing="0" align="center">';

    echo '<tr><td>Name</td><td>Street</td><td>City</td><td>Country</td><td>Options</td></tr>';

    for( $i = 0; $i < count($rows); $i++ ){

    $row = explode(' | ', trim($rows[$i]));

    $id = $row[0];

    $name = $row[1];

    $street = $row[2];

    $city = $row[3];

    $country = $row[4];

    echo "

    <tr>

    <td>$name</td>

    <td>$street</td>

    <td>$city</td>

    <td>$country</td>

    <td><a href="edit.php?id=$id">Edit</a> | <a onclick="alert('Are you sure?');return false;" href="delete.php?id=$id">Delete</a></td>

    </tr>

    ";

    }

    echo '</table>';

    </body>

    </html>


    -----------------------------

    delete.php

    <?php

    /* DELETE LINE IN TEXT FILE */

    if( isset($_GET['id']) ){

    $id = $_
    GET['id'];

    }

    $data_file = 'address_book.txt';

    $rows = file($data_file);


    $rows = array_map('trim',$rows); //trim all rows

    for( $i = 0; $i < count($rows); $i++ ){

    $row = explode(' | ', $rows[$i]);

    /* if submitted id matches the id in the current row delete it using unset */

    if( $id == $row[0] ){

    unset($rows[$i]);
    $fh = fopen($data_file,'w');
    fwrite($fh,implode("n",$rows));
    fclose($fh);

    header("location: records.php?m=1");


    }

    }


    ?>
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — 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 filefunction 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]

    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
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — Oops ... I see you have posted some codes while I was replying to your earlier post.

    Going to check the codes you posted and reply after I tested it.

    Thanks again for helping me and finding a solution for this.

    Best regards
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — the code above reads the text file and creates a table of all the records, the delete/edit links take them to another page and passes the id via GET. the delete.php page

    creates an array containing the rows from the db and loops over them comparing the id in the db to the id passed via GET from records.php if it finds a match it unsets/deletes that record from the array, then it implodes the array back into the db and redirects them back to records.php with a query string of m=1, records.php detects the m=1 and displays a success message.
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — 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]


    i'd only pass the id via get, then have the delete.php script explicitly match the id before deleting the record, otherwise as you database changes you'll be deleting by row number which can change, instead of based on the id of the row which will stay the same whther rows are added or removed later.

    in any event if you are gonna stick with forms you should pass the data via the hidden form fields, and not as part of the query string in the action part of the form
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — Hello bsmbahamas,

    I made the 2 files (records.php and delete.php) as you said in your post.

    Do I have to change something is the file alert.js ?

    Results of testing:

    I get the same error message as before ?

    errror message: [COLOR="Red"]document.getElementById("id") is null[/COLOR] in alert.js file

    So it stops and does nothing. It stays on the records.php page and didn't delete any line in the database.

    Best regards
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — a simple way of doing this with javascript is to have the link you click on call a javascript function that passes the id of the row you want to delete.

    so the delete link would look like this:

    <a onclick="alert('Are you sure?','hhddhfydsbfju');return false;" href="delete.php?id=$id">Delete</a>

    then your custom alert will grab the second parameter from the custom alert function and then send it to delete.php as part of the query string.

    function removeCustomAlert() {

    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));

    var id = document.getElementById("id").value;

    var name = document.getElementById("name").value;

    [COLOR="DarkRed"]window.location='delete.php?do=delete_data&id='+id+'&name='+name;[/COLOR]

    }

    set the id portion of line in red above to the second paremeter passed by the alert() function when the link is clicked, and you won't need to use hidden fieds for name or id at all.

    in other words have php set the link code as the page is loading to include the id as a parameter of the custom alert function, so each link would pull up you custom alert box and at the same time pass the id to the 'yes' button, so once you confirm deletion it will redirect and pass the id from the function parameter to delete.php all in one shot, and you won't need to read name.value and id.value or use hidden forms at all
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — function removeCustomAlert() {

    var id = document.getElementById("id").value;

    var name = document.getElementById("name").value;

    [COLOR="Red"]document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));[/COLOR]

    window.location='delete.php?do=delete_data&id='+id+'&name='+name;

    }

    hmmm, have you tried removing the 'modalContainer' after setting the id and name? doesn't look like it would affect the forms on the page though, i tried the script as is on my end but my domain is blocked as i'm not at home at the moment, lol.
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — function removeCustomAlert() {

    var id = document.getElementById("id").value;

    var name = document.getElementById("name").value;

    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));

    window.location='delete.php?do=delete_data&id='+id+'&name='+name;

    }

    try changing the redirect to an alert to see what it shows:

    function removeCustomAlert() {

    var id = document.getElementById("id").value;

    var name = document.getElementById("name").value;

    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));

    [COLOR="red"]alert('delete.php?do=delete_data&id='+id+'&name='+name);[/COLOR]}
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — i got your pm, but i'm not able to test my code as i'm stuck at work and my server's domain is also blocked.

    it seems to me that the custom alert is not finding the name.value or id.value, i was suggesting above that you remove the id and name from the action part of the form, and also remove the modalConatiner child after setting the variables instead of before.

    the workaround i suggested was to have php construct the buttons with the id parameter already in the alert() function, so each button's onclick will look like this:

    onclick="alert('message goes here','row id goes here')"

    so when the alert box pops up the url that it redirects to can grab the id passed into the alert function, and if you go this route you won't need to use hidden fields since the id is set by php as the page loads on each record and being passed via the alert function to the modalContainer pop up box
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — Hello bsmbahamas,

    Everything I change in the codes, I always get the same.

    Or the right line gets deleted but NO alert

    Or the alert shows and delete the first line in the database.

    So the problem is that the function in the alert.js script keeps sending the wrong url ... (and always the first line of the database gets deleted). So the getElementById('id') in the function doesn't receive the right value from the onclick event and sends the value of the first entry to the alert script.

    So there must be a way that it gets the right value ... but how ?

    Thanks for you patience !!!

    Regards
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — 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);

    }

    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.

    you really don't need to pass the name, just the id, and the delete.php should then match the id passed via get to the actual id in the row to decide which row to delete.

    if 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.

    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.

    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?
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — 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]

    I already did that and it shows the url of the first line

    so if I click on the delete link of eg naam 7 the alert shows

    delete.php?do=delete_data&id=hhdgfbcjeffch&name=name 1

    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]
    With this code in the onclick event[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 shows

    [COLOR="SeaGreen"]Delete hhddhfydsbfju - name 7 ... Are you sure? Yes - No[/COLOR]

    When I click in the alert box on "Yes" Firebug says document.getElementById('id') is null or undefined and the second alert (in alert.js) appears it shows

    [COLOR="Red"]delete.php?do=delete_data&id=hhdgfbcjeffch&name=name 1 Yes - No[/COLOR]

    so of name 1 which is wrong and it deletes name 1

    if 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 ?

    Regards
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — change the php so that the source code comes up like this:

    <a onclick="[COLOR="darkred"]alert('Delete hhdgfbcjeffch - name 1 ');id='hhdgfbcjeffch';[/COLOR]return false;" href="delete_records.php?id=hhdgfbcjeffch">Delete</a>

    take out the id= part from alert function parameter.

    and also change your remove custom alert function to this:

    function removeCustomAlert() {

    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));

    window.location='delete.php?do=delete_data&id=' + id;

    }

    so basically when you click on any of the delete buttons it sets var id; which should be global in this case to whatever is in the source code at the time the button is clicked, then when you confirm it redirects to delete.php and tacks on the value fromthe global id.

    otherwise you'd have to modify your createCustomAlert(textalert) function to use two paremeters like this createCustomAlert(textalert,id); if you don't want to use a global variable
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — that should do it, either use a global id variable and set it in the onclick for each button,

    or if you don't want to use a global variable you can pass it as a second parameter in your custom alert function like this onclick="alert('Delete hhdgfbcjeffch - name 1 ','hhdgfbcjeffch'); but don't use 'id=hhdgfbcjeffch' as that is a string not a variable assignment, assignment would be id='hhdgfbcjeffch' not 'id=hhdgfbcjeffch' and if you choose to pass it as a parameter you will also have to rewrite your function to use the second parameter or it will just be ignored.
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — Hello again,

    I changed what you suggested (onclick and deleted the 2 var=... in alert.js) and now I get error in alert script

    id undefined or null in

    window.location='delete.php?do=delete_data&id=' + id;

    Regards
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — personally i would create a custom function to pop up my custom alert box rather than overwriting the native alert() function, for example:

    and assign this function to the onclick event of the button, within the function

    you can do stuff withthe message and id passed in as parameters

    function confirmDelete(msg,id){

    }
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — Hello bsmbahamas,

    Thank you very much for your help, time and patience.

    I have a feeling I will not find the solution even with your good help.

    I'm trying to get this alert to work for days now but with no luck. Maybe one day soon (I hope) I can say EUREKA!!! but so far it's just a lot of stress and frustration. ?

    Best regards ?
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — ok, make sure you declare var id; at the top of your page in the js code, then have your button set the id in the onclick like this:

    <a onclick="[COLOR="Red"]id='hhdgfbcjeffch';alert('Delete hhdgfbcjeffch - name 1 ');[/COLOR]return false;" href="delete_records.php?id=hhdgfbcjeffch">Delete</a>

    make sure the id='hhdgfbcjeffch'; comes before the alert(); i gave it to you inthe wrong order, so it was probably setting the id after calling the alert();

    if it still doesn't work, pm me the urls to the pages/scripts on your site if it is a live site, or the current code from the javascript and php files
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — i just tried a file with only this in it and it pops up the message and id:

    <a onclick="id='hhdgfbcjeffch';alert('Delete hhdgfbcjeffch - name 1 '+','+id);return false;" href="delete_records.php?id=hhdgfbcjeffch">Delete</a>

    make sure you have it like this:

    id='hhdgfbcjeffch' before the alert()

    if they are in the wrong order then the id will be set after the alert and therefore undefined/null:

    <a onclick="alert('Delete hhdgfbcjeffch - name 1 '+','+id);id='hhdgfbcjeffch';return false;" href="delete_records.php?id=hhdgfbcjeffch">Delete</a>
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — from my testing using a custom alert function seems to work best:

    <script type="text/javascript">

    function customAlert(msg,id){

    alert( 'msg: ' + msg + ', id: ' + id );

    }

    </script>

    <a onclick="customAlert('Delete 1','1'); return false;" href="delete_records.php?id=hhdgfbcjeffch">Delete 1</a>

    <a onclick="customAlert('Delete 2','2'); return false;" href="delete_records.php?id=hhdgfbcjeffch">Delete 2</a>

    the above code allows you to pass both a custom message and id as parameters right in the onclick attribute of the button/link.

    your script would then just need to confirm the users intention and then redirect them to delete.php with the id passed in when they clicked 'yes' or cancel if they click 'no'

    your php code would prepopulate the parameters in customAlert(msg,id) before the page is loaded into the browser
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 21.2012 — Hello again,

    If it only was the old fashion browser alert I wouldn't have started this topic ? I have such a script but that is the upgly gray browser alert box and that only gives you the option OK in the browsr and not the options YES and NO.

    I wanted a more up-to-date alert box that has colors that suits the colors of the site.

    The page isn't online yet but I'm running it on my local host. If you really want all the files I can send them as attachment in an email to you but therefore I need your email address. You can always send me a pm with your email address. Do not write it here.

    Regards

    PS the onclick code you sent me, also gives an undefined error even when I put the vars in the very beginning of the allert.js script and remove them out of the fuction where they were.
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 21.2012 — i understand that you want to make a pretty alert box, i was saying to display it with a custom function instead of rewriting the normal alert box, so it would still pull up your custom box, but would not change the native alert() code. i'll send you a pm with my email
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 22.2012 — Okay thanks again !!!!

    I just sent you all the files in an email.

    I hope I didn't forget anything ...

    Best regards
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 26.2012 — Give credit where credit is due!!

    Thanks to the excellent help AND a lot of patience of [B][COLOR="Green"][SIZE="3"]bsmbahamas[/SIZE][/COLOR][/B], my problem has been solved. Hurray !!!

    For those who are interested and who want to use this alert box ...


    in [B]alert.js[/B]

    addvar row;"
    remove return false in btn.onclick = function() { removeCustomAlert();[COLOR="Red"]return false; }[/COLOR][/quote]

    change function removeCustomAlert() to function removeCustomAlert(row) {
    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
    window.location='delete.php?do=delete_data&amp;id='+row+'&amp;name='+name;
    }



    in [B]delete.php[/B]

    &lt;form method="post" action="'.$self.'?do=delete_data&amp;id='.$id.'&amp;name='.$name.'" onclick="alert('Delete '.$id.' - '.$name.' ... Are you sure?');row='.$id.';return false;" &gt;
    and
    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 &lt; 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);


    Regards
    Copy linkTweet thisAlerts:
    @bsmbahamasJun 26.2012 — Give credit where credit is due!!

    Thanks to the excellent help AND a lot of patience of [B][COLOR="Green"][SIZE="3"]bsmbahamas[/SIZE][/COLOR][/B], my problem has been solved. Hurray !!!
    [/quote]


    thanks it was fun.

    one important change is that you should use $_REQUEST instead of GET or POST, if javascript is turned off the data will be sent via POST since the form used method="post" instead of method="get", if javascript is turned on then the form won't submit at all as the custom alert function will block the form submission and instead send the data via GET when it calls window.location,

    so if you use GET or POST instead of REQUEST only the javascript version will work, i.e. if javascript is off, the form will submit via POST and the delete script will ignore the data sent by POST as it is expecting GET, using REQUEST will work in both cases.


    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 &lt; 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]


    finally, i'd advise that instead re-writing the native alert() function and using a global variable to instead create a custom function like this...

    function confirmDelete(msg,id){

    /* code goes here */

    }

    ...as this will let you pass the parameters directly to the function instead of using globals, and altering the default alert() functionality.

    cheers
    Copy linkTweet thisAlerts:
    @coderunnerauthorJun 26.2012 — one important change is that you should use $_REQUEST instead of GET or POST, ...[/QUOTE]
    Oops ... you told me that but I forgot to change that in the code I posted but I used it in my code ... ?

    Sorry ... :o
    ×

    Success!

    Help @coderunner spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.21,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...