2015-08-14 64 views
1

我不知道這是否可能,但任何幫助將不勝感激。提交<a> href到PHP

我通常使用javascript將我的html表單提交給外部php腳本。 共振是一個很好的方式顯示錯誤通知的JavaScript句柄。

下面是一個例子;

<script> 
    $("input#submit").click(function() { 
     $('#loader').show(); 
     // Client side validation 
     if ($("#category").val() == "") { 
      $('#loader').hide(); 
      detError(); 
     } else { 
      $.post($("#cat-form").attr("action"), 
       $("#cat-form :input").serializeArray(), 

       function(response) { 
        switch (response) { 
         case "Valid": 
          $('#loader').hide(); 
          catSuccess(); 
          $('#cat-form').trigger("reset"); 
          setTimeout(function() { 
           window.location.href = "blog_admin_categories.php"; 
          }, 4000); 
          break; 
         case "not Valid": 
          $('#loader').hide(); 
          catError(); 
          break; 
        } 
       }); 
     } 
     $("#cat-form").submit(function() { 
      return false; 
     }); 
    }); 
</script> 

正如你所看到的,根據我從PHP腳本(有效,無效等),我運行另一個函數,也許刷新頁面回波接收響應。

現在我想用鏈接<a>來做到這一點。

<a class='link' id='delete-link' href=/blog/php/blog_category_delete.php?delete='$cat_id'>Delete</a> 

我該怎麼做?

在下面的答案幫助下,我現在有這個工作... 下面的代碼;

<script> 
       $("a.delete").click(function(event) { 
        event.preventDefault(); // this is needed to stop the browser from changing the site 
        var href = $(this).attr('href'); 

        $.post($(this).attr('href'), 
          $('href').serializeArray(), 

          function(response) { 
         switch(response) { 
          case "Valid": 
           $('#loader').hide(); 
           catDeleted(); 
           $('#cat-form').trigger("reset"); 
           setTimeout(function(){window.location.href="blog_admin_categories.php";},3000); 
           break;} 
        }); 
       }); 
      </script> 
+0

你是說你想你刪除做類似的帖子,並得到類似的迴應? – gfrobenius

回答

0

您的href打破了報價。

<a class='jslink' href='blog/php/blog_category_delete.php?delete=$cat_id'>Delete</a> 
<script> 
    $("a.jslink").click(function(event) { 
     event.preventDefault(); // this is needed to stop the browser from changing the site 
     var href = $(this).attr('href'); 

     // your code 
    }); 
</script> 
+0

感謝。 如何創建一個類似的表單應用功能傳遞給PHP腳本? 你可以給我一個這個函數的樣子嗎? 我只想捕捉從PHP腳本返回的回顯 正如你可以從我的代碼看到的,我可以這樣做形式。 –

+0

感謝您的幫助。星! –

+0

如果您不需要向腳本發佈信息,可以使用簡單的[ajax get-request](https://api.jquery.com/jquery.get/): '$ .get(href, '',函數(數據){/ *你想對數據做什麼/ *});' – WhoIsJohnDoe

1

你可以通過AJAX來完成。

$("#delete-link").click(function(){ 

    //you can write your ajax here 

    return false; 
}); 
+0

這個點擊處理器解決方案比我的onclick屬性解決方案更優雅。 – JRulle

0

而不是使用<a>href屬性只創建和onclick屬性,可以調用你的JavaScript函數:

<a class='link' id='delete-link' href="" onclick="callphp('$cat_id')">Delete</a> 

<script> 
    function callphp(value) { 
    //call you submission function and do your validation here... 
    } 
</script> 
+0

你能給我一個關於這個函數看起來如何的想法嗎? 我只想捕捉從PHP腳本返回的回聲。從您的代碼中可以看到,我可以爲表單執行此操作。 –

+0

你應該能夠使用你原來的提交功能......點擊處理函數中的所有東西。 – JRulle

+0

基於我提交表單的函數, '$ .post($(「#cat-form」)。attr(「action」), $(「#cat-form:input」)。serializeArray() ' 我將如何取代這兩個? 一種是通過這崗位行動,另一種是聚集形式的數據。 –

0

jquery是最簡單的方法。使用jquery這樣裝上點擊監聽器的鏈接:

$("#mylink").click(fucnction(){  }); 

和花括號內可以.post的$或者$不用彷徨發送AJAX請求到PHP腳本