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>
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:
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.
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;
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
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"
Code:
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");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}
Can you help me ?
Regards
Last edited by coderunner; 06-20-2012 at 04:06 PM.
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
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
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 this
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
case "";
///////////////////////////////////////////////////////////////
// READ TEXT FILE //
///////////////////////////////////////////////////////////////
$file_handle = fopen($data_file, "rb");
while (!feof($file_handle) )
{
///////////////////////////////////////////////////////////////
// DELETE LINE IN TEXT FILE //
///////////////////////////////////////////////////////////////
case "delete_data";
if (isset($_GET['id']))
{
$id = $_GET['id'];
}
if (isset($_GET['name']))
{
$name = $_GET['name'];
}
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
Last edited by bsmbahamas; 06-21-2012 at 10:11 AM.
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
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;
}
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&id=hhdgfbcjeffch&name=name 1 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
Last edited by coderunner; 06-21-2012 at 11:08 AM.
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&id=hhdgfbcjeffch&name=name 1 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
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
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: document.getElementById("id") is null 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
Last edited by coderunner; 06-21-2012 at 11:38 AM.
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.
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.
Bookmarks