Php blok ve tersi içinde JavaScript çağırıyor

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

Bir kullanıcı hits silme zaman, bir javascript işlevi onay aranmalıdır. Somewhere Javascript fonksiyonu, php kod silme işlemi için kullanılmalıdır. Bunu nasıl yapabilirim? "Bazı php kodu buraya" gibi bir şey kullanın "bazı javascript function ();" Bana nerede ne koymak bilmek için. Teşekkürler.

8 Cevap

Bu jQuery kullanarak varsayar ...

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

<script type="text/javascript">

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

</script>

JavaScript fonksiyonları (tarayıcıda) istemci üzerinde çalıştırmak ve PHP bir sunucu üzerinde yürütür. Yani, JavaScript bir mesaj göndermek zorunda - HTTP üzerinden - PHP ile ele alınması için sunucuya. PHP silme gerçekleştirmek olacaktır. Mantıklı?

message sent sunucuya AJAX yoluyla gönderilen olabilir.

Belki Ajax kullanmanız gerekir: http://en.wikipedia.org/wiki/Ajax_%28programming%29

JS istemci tarafı ise PHP, bir sunucu tarafı teknolojisidir. Onlar birbirleri ile etkileşim olamaz - diğer bir deyişle: Onlar tamamen bağımsız konum.

PHP bir JS kodu sadece çıkış kodu yapabilirsiniz:

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

PHP tüm yapabileceğimiz bu. JavaScript de PHP ile etkileşime doğrudan olamaz. Sen yeni bir HTTP isteği ve süreç döndürülen veri göndermek için AJAX kullanmak gerekecek.

PHP böylece tarayıcıya çıktı PHP komut dosyası olamaz ve PHP motoru ile bunu ayrıştırmak bekliyoruz, bir sunucu tarafı dilidir.

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>

Bu Ajax içerdiğinden, Diyelim ki bir benzeri xhr işlemek için jQuery kullanabilirsiniz varsayalım.

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

Tamam, bazı JS ve HTML yüzden. Şimdi, yazı işlemek için ayrı bir PHP komut dosyası gerekir. Örneğin ile gitmek için, bu 'delete.php' adlı, aynı dizinde kaydedilmiş olacaktır.

<?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'));
  }

Burada jQuery kullanarak başka bir örnek:

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