Mateobus Posted April 25, 2006 Share Posted April 25, 2006 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...} } Quote Link to comment https://forums.phpfreaks.com/topic/8341-php-and-javascript/ Share on other sites More sharing options...
GBS Posted April 25, 2006 Share Posted April 25, 2006 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,, Quote Link to comment https://forums.phpfreaks.com/topic/8341-php-and-javascript/#findComment-30449 Share on other sites More sharing options...
Mateobus Posted April 25, 2006 Author Share Posted April 25, 2006 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 pressedif (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. Quote Link to comment https://forums.phpfreaks.com/topic/8341-php-and-javascript/#findComment-30676 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.