Jump to content

PHP and Javascript


Mateobus

Recommended Posts

OK, I am trying to use php and javascript together and I have made more progress than I thought originally. The idea is that I want to have a delete option for some data in my mySQL database. IF the user clicks the delete link, it passes the ID to the confirmation(id) function. I have this working. Now I want to make the query that uses that ID, not as easy. Here is the code I have, anyone got the rest...

function confirmation(id) {
var answer = confirm("Delete data: "+id+"?")
if (answer){
<?php
$sql = "DELETE FROM table WHERE id='";
?>
//I want to say ID here...
}
}
Link to comment
Share on other sites

Hi there,

You can't use your code as you wanted to do, because php is interpreted before the javascript part, (server-side/client-side).
The solution could be then to use AJAX or JPSPAN, but it's maybe not the easier for you,
The usual solution is to pass javascript value thrue PHP using GET/POST methods, you could try something like:

"test.html"
[code]
<html>
<body>
<form name="frame1" method="POST" action="confirmation.php">
<input type="hidden" id="vartodel" name="vartodel" >
</form>
<script>
var id='testing';
function confirmation(id)
{
if (confirm("Delete data: "+id+"?"))
    {
    document.getElementById('vartodel').value=id;
    document.frame1.submit();
    }
else
    {
    alert("operation aborted,,");
    }
}
confirmation(id);
</script>
</body>
</html>
[/code]
& the "confirmation.php" file could be:
[code]
<?
$todel = addslashes($_POST["vartodel"]);
// TODO your database connection
$sql = "DELETE FROM table WHERE id='$todel'";
echo "TODO,, deletion & query is >$sql<";
?>
[/code]
So, the plan could be to include the confirmation function in your main html page & then call the confirmation.php file to execute the php/mysql part,,

Good luck,

l8tr,,
Link to comment
Share on other sites

Ok, I have made progress, but I didn't quite get it yet using your method, I have changed things a bit the biggest thing to note is that it is all on one page:

<body>
<form name="frame1" method="POST" action="this_same_page">
<input type="hidden" id="vartodel" name="vartodel" >
</form>
<script type="text/javascript">
function confirmation(id) {
var answer = confirm("Delete Game "+id+"?")
if (answer){
document.getElementById('vartodel').value=id;
document.frame1.submit();
}
}
</script>

//If delete has been pressed
if (A CHECK TO SEE IF FRAME1 has been submitted){ // THIS LINE I CANT FIGURE OUT
$vartodel=$_POST['vartodel'];
$thesql = "DELETE FROM table WHERE id='$vartodel' LIMIT 1";
if(mysql_query($thesql))
echo "<font color=\"red\"> Deleted</font><br />";
}
$id = 6;
<a href="this_page" onclick="confirmation('$id')">Delete</a>


Someone let me know if this will work all on one page like so.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.