2015-12-04 53 views
1

我想要有人點擊此鏈接首先在我的功能,我已經在JS和之後,遵循href函數存在於<a>標記。問題在於它首先遵循href,而我無法從js函數傳遞我想要的變量。HTML標籤<A>傳遞變量都與href和onclick

這裏是我的代碼:

PHP

echo '<a href="category.php?id=' . $_SESSION['id'] . '" onclick="myFunction();"> 
     <div class="icon-lock"> 
      <img src="/test/images/lock.png" alt="Locked Button" width="35" height="35" border="0"> 
     </div>'; 
echo '</a>'; 

JS

<script> 
function myFunction() { 
$.ajax({ 
     type: "POST", 
     data: {id: "1"}, 
     url: "category.php" 
     }); 
} 
</script> 

我如何href和myfunction的會以打印

工作很迷茫
$id=$_POST['id']; 
echo $id; 

在php頁面重新載入後...
有什麼想法?

+0

重新加載頁面的目的是什麼? – guest271314

+0

我想要重新加載頁面,然後根據用戶是否點擊鏈接打印一個不同的圖標,之前..當你看到我的代碼中有一個lock.png圖像,但如果鏈接被按下,那麼我有一個如果()(!(id == null)),然後打印lock.png其他打印unlock.png ..爲了這個目的,我首先必須從會話傳遞id作爲category.php?id = session [' ID'],但也要傳遞JavaScript的JavaScript,我想檢查在同一頁上打印另一個圖像。我希望你明白我想要做什麼。 – Johnny

回答

0

防止採用事件對象的myfunction方法中的默認操作。然後在成功回覆你的ajax請求時,做你想做的事情。

<a href="category.php?id=' . $_SESSION['id'] . '" onclick="myFunction(e);">

function myFunction(e) { 
    e.preventDefault(); 
    $.ajax({ 
     type: "POST", 
     data: {id: "1"}, 
     url: "category.php" 
    }).done(function() { 
     // What ever following the link does 
    }); 
} 
+0

我也試過你的方式,但它只是沒有遵循JavaScript ...它只是立即刷新,沒有發送到我寫的文件..要檢查哪一個得到第一我寫 ' $ id ='1'; ' and in js i wrote to go in another php page but it always goes to category.php ' function myFunction(id) { $.ajax({ type: "POST", data: {id: "1"}, url: "category.php" }) .done(function() { window.location.assign("index.php?id="+id); }); }' – Johnny

+0

@Johnny.. Can you check the edit. i forgot to prevent the default action. –

1

你可以通過「身份證」在你的函數中的參數,並使用JavaScript來改變窗口的位置後,阿賈克斯成功。

HTML

<div class="icon-lock"> 
    <img onclick="myFunction(<?=$_SESSION['id']?>)" src="/test/images/lock.png" alt="Locked Button" width="35" height="35" border="0"> 
</div> 

的Javascript

function myFunction(id) { 
    $.ajax({ 
     type: "POST", 
     data: {id: "1"}, 
     url: "category.php", 
     success: function() { 
      window.location.assign("category.php?id="+id); 
     } 
    }); 
} 
+0

First of all thanks for your answer but it doesnt work when i press in the image..the id that i want to be returned is not.Look at my code as i have it now: – Johnny

+0

PHP: 'echo '

'; \t \t \t \t \t \t \t \t \t \t \t \t \t echo '「;' 的javascript: '結myFunction的(ID){ $就({ 類型: 「POST」, 數據:{ID: 「1」}, URL:「的範疇。PHP的?」 }); 成功:函數(){ window.location.assign( 「category.php ID =」 + ID);} } ' – Johnny

+0

什麼我做錯了,我想通過這兩個變量? $ _SESSION ['id']和我有作爲數據發佈在JavaScript..so實際上我想要2個變量傳遞時,頁面刷新,我想打印'身份證'變量我在JavaScript後有通過使用'$ ID = $ PHP代碼_ POST [ 'ID']; 回聲$ ID;' – Johnny

2

你只是這個消息傳給你的函數

你的PHP代碼將看起來像這樣

echo '<a href="category.php?id=' . $_SESSION['id'] . '" onclick="myFunction(this);">/*pass this keyword to function */ 
      <div class="icon-lock"> 
       <img src="/test/images/lock.png" alt="Locked Button" width="35" height="35" border="0"> 
      </div>'; 
    echo '</a>'; 

function myFunction(str) { 
    $.ajax({ 
     type: "POST", 
     url: str.getAttribute("href") 
    }).done(function() { 
     // What ever following the link does 
      window.location.href=str.getAttribute("href") 

    }); 
} 
+0

我取代我的JS與 '函數myFunction的(STR){ $就({ 類型: 「POST」, url:str.getAttribute(「href」) })。done(function(){ window.location.replace(「index.php?id =」+ str); }); }' – Johnny

+0

和我的php與 '回聲' \t \t \t \t \t \t \t \t \t \t \t

'; \t \t \t \t \t \t \t \t \t \t \t \t \t echo ''; \t \t \t \t \t \t \t \t \t \t \t \t \t $ ID = $ _ POST [ 'ID']; \t \t \t \t \t \t \t \t \t \t \t \t \t回聲的$ id;' – Johnny

+0

但它始終沒有重定向的passind變種id來index.php來category.php(指數只是爲了檢查它goes..actually我希望它被刷新併發送category.php?id = $ _ SESSION ['id'] + id ='1') – Johnny