tags:

views:

79

answers:

8
echo "<a href=#> Delete </a>";

Whenever a user hits Delete, a javascript function should be called for confirmation. Somewhere in the Javascript function, php code should be used for delete operation. How do I do that? Use something like "some php code goes here" and "some javascript function();" for me to know where to put what. Thanks.

+1  A: 

JavaScript functions execute on the client (in the browser) and PHP executes on a server. So, the JavaScript must send a message - via HTTP - to the server to be handled by PHP. The PHP would perform the delete. Make sense?

The message sent to the server might be sent via AJAX.

Upper Stage
So there is new request method for HTTP protocol called `AJAX`? Don't take it personally but... learn a bit about AJAX.
Crozin
I meant to suggest that the writer look into AJAX; it seemed she might not be familiar with it. (And your comment might easily be considered offensive.)
Upper Stage
A: 

Please see Deleting Table Rows Using JQuery and PHP.

Sarfraz
you can't call php code directly in client side, you must send a message to the server with direct HTTP POST/GET or through AJAX that it should run given script.
raceCh-
Hello All, i have updated my answer, consider again. Thanks
Sarfraz
A: 

Maybe you should use Ajax: http://en.wikipedia.org/wiki/Ajax_%28programming%29

Notinlist
+2  A: 

This assumes that you are using jQuery...

<a href='javascript:delete();'>Delete</a>

<script type="text/javascript">

function delete()
{
     $.post("/your_script.php", {}, function(result) {
     });
}

</script>
tambler
a call to confirm() would be used here to determine if $.post() executes or not. you'd also want to cancel the default action for the click event.
lincolnk
A: 

PHP is a server-side technology, while JS is a client-side. They cannot interact with each other - in other words: they're completely independent.

PHP can only output code that is a JS code:

echo 'document.getElementById("test").appendChild(document.createTextNode("' . $myVar . '");';

It's all PHP can do. JavaScript cannot direct interact with PHP as well. You'll have to use AJAX to send a new HTTP request and process returned data.

Crozin
A: 

PHP is a server-side language, thus you can not output PHP script to the browser and expect that it will parse it with the PHP engine.

What you're looking for is probably AJAX, or simply redirecting the user to another page (with different URL parameters) or submitting a form. AJAX doesn't require from the browser to reload the page, while the two other methods does.

Anyway, you can execute a JS script with the "onclick" method, that's executed when the user clicks on the element: <a href="#" onclick="myFunc();">Delete</a> But the following approach looks better and considered as an ideal one:

<a href="#" id="myId">Delete</a>
<script type="text/javascript">
document.getElementById("myId").onclick = myFunc;
</script>
Dor
A: 

Since this involves Ajax, let's assume you can use jQuery to handle the XHR an so on.

<script>
$('#del').click(function(ev){
    ev.preventDefault();
    var del_conf=confirm('Are you sure you want to delete this item?');
    if(del_conf){ $.post('delete.php',{'del':1,'id':123123},function(data){
      alert(data.result);},'json');
      }
    });
 </script>
 <a id='del'>Delete</a>

Okay, so that's some JS and HTML. Now, you need a separate PHP script to handle the post. To go with the example, this would be saved in the same directory, named 'delete.php'.

<?php
$del=(int)$_POST['del'];
$id=(int)$_POST['id']

if($del<1 || $id<1){ exit; }
else{
  //do your DB stuff
  }
if($db_success){
  echo json_encode(array('result'=>'success'));
  }
else{
  echo json_encode(array('result'=>'error'));
  }
Alex JL
A: 

here is another example using jQuery:

<div id="message"></div>
<a class="action" type="delete" rel="1234567">delete</a>
<script type="text/javascript">
$('a.action').click(function(){
var $this = $(this);

var processResponse = function(data){
    //optionaly we can display server response
    $('#message').html(data);
    return; 
};

var postPparams = {
    module:'my_module_name',
    action:$this.attr('type'), 
    record_id: $this.attr('rel')
};
$.post('/server.php',postPparams, processResponse);
});
</script>
Nazariy