PDA

View Full Version : JavaScript and PHP


Mark_W
07-23-2007, 04:06 PM
I swear I had already made this thread in the early hours of this morning, but I must have dreamt it.

I'm currently developing a small custom CMS to my own specifications. For the parts of my CMS where it is possible to delete data from the database I want a small alert box to pop up asking to confirm the deletion.

I have no experience of JavaScript and this is the first time I have created my own JavaScript code.

I have the alert box sort out, however when the user clicks confirm I want the JavaScript to then run the following code:

<?php //////////////
$query = sprintf("DELETE FROM newscontent where news_id='$del'");
mysql_query($query) or die (mysql_error());
//////////////?>I thought it would be as simple as this:

<script type="text/javascript">
function messagepopup() {
var answer = confirm("Delete User?")
if (answer){
("<?php //////////////
$query = sprintf("DELETE FROM newscontent where news_id='$del'");
mysql_query($query) or die (mysql_error());
//////////////?>")
}
else{
}
}
</script>

But obviously it is not, so could someone possibly help me with how to do such a task?

Thank you,

Mark

davidj
07-23-2007, 04:50 PM
not a bad attempt

ok...

user clicks the delete button and you have your confirm box appear

when confirmed just do a function...


function messagepopup() {

var answer = confirm("Delete User?");

if (answer){

document.location.href="thispage.php?del=y";

}


thispage.php is th page your in but notice i pass a del value pair.

add your delete statement in your php above and just wrap it in an IF to detect if del is set before your delete fires (it doesnt matter what its set to)


if($_GET['del']){

// perform delete here

}

Mark_W
07-23-2007, 05:14 PM
Right I have got the code working, the only problem I have come across is the fact that I am using the JavaScript within a do while loop.

So when I click to delete ID 5, ID 1 is deleted as thats the first ID to be pulled from the database.

This is the do while loop code:

<?php do { ?>

<tr>
<td class="tablecontents"><input name="" type="checkbox" value="" /></td>
<td class="tablecontents"><?php echo $row['news_id']; ?></td>
<td class="tablecontentsA"><?php echo substr($row['news_name'], 0, 30); ?>... </td>
<td class="tablecontents"><?php echo $row['news_user']; ?></td>
<td class="tablecontents"><?php echo $row['news_date']; ?></td>
<td class="tablecontents">
<a href="01viewnews.php?id=<?php echo $row['news_id']; ?>">View news</a>
<a href="01editnews.php?id=<?php echo $row['news_id']; ?>">Edit</a>
<a href onclick="messagepopup()">Delete</a>
</td>
</tr>

<?php }while ($row = mysql_fetch_array($result)); ?>The script code is now this:

<script type="text/javascript">
function messagepopup() {
var answer = confirm("Delete News Item?")
if (answer){
document.location.href="01newsadmin.php?del=<?php echo $row['news_id'];?>";
}
else{
}
}
</script>And the delete query this:

////////////// Variables //////////////
$del = $_GET['del'];
////////////// Variables //////////////

////////////// If delete field selected run delete query //////////////
if($del){

$del_query = sprintf("DELETE FROM newscontent where news_id='$del'");
mysql_query($del_query) or die (mysql_error());
}
////////////// If delete field selected run delete query //////////////Thank you for your help.

davidj
07-23-2007, 08:01 PM
pass the delete id as an argument from the javascript call in the loop...

<a href onclick="messagepopup('<?php echo $row['news_id']; >')" ?> Delete</a>


catch the argument in the javascript function (id)


function messagepopup(id) {

var answer = confirm("Delete News Item?");

if (answer){

document.location.href="01newsadmin.php?del=<?php echo $row['news_id'];?>&id="+id; //<< notice the id value pair
}


** you dont need an else unless your doing something else.

now just catch the value in PHP


////////////// Variables //////////////
$del = $_GET['del'];
$id = $_GET['id']; //<<< new id caught from javascript function
////////////// Variables //////////////

////////////// If delete field selected run delete query //////////////
/// notice the $id var

if($del){

$del_query = sprintf("DELETE FROM newscontent where news_id='$id");
mysql_query($del_query) or die (mysql_error());
}
////////////// If delete field selected run delete query //////////////