Jump to content


Photo

PHP and Javascript


  • Please log in to reply
2 replies to this topic

#1 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 April 2006 - 05:56 AM

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

#2 GBS

GBS
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 25 April 2006 - 09:07 AM

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"
<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>
& the "confirmation.php" file could be:
<?
$todel = addslashes($_POST["vartodel"]);
// TODO your database connection
$sql = "DELETE FROM table WHERE id='$todel'";
echo "TODO,, deletion & query is >$sql<";
?>
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,,

Sorry for my english,... I do my best, but sometimes,...
1st intention: understand the problem
2nd intention: try to solve it
3rd intention: try to understand how to solve it :)

#3 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 April 2006 - 06:21 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users