2015-04-29 59 views
1

我有一個URL,我可以調用一個用戶ID,它會更新一個SQL表。 (例如domain.com/postback.php?userid=userid)。手動工作正常,但不是在嘗試讓JavaScript調用它時。我剛剛開始學習3天前的JavaScript,所以請原諒我是否容易 - 但我看不到它。通過JavaScript調用PHP腳本與PHP變量

我這裏所說的JS問題:

<input type='button' id='countdw' value='Wait 30s' class='btn btngo disabled'> 
<script> 
    var secsLeft = 30; 
    setInterval(function(){ 
     secsLeft--; 
     if(secsLeft > 0){ 
      $('#countdw').val('Wait ' + secsLeft + 's'); 
     } else if(secsLeft == 0){ 
      $('#countdw').removeClass('disabled'); 
      $('#countdw').val('Go'); 
      $('#countdw').attr("onclick","doSomething2()"); 
     } 
    }, 1000); 

這裏是我的JS。我嘗試了很多不同的方式,這只是其中之一。

<script type="text/javascript"> 
$(document).ready(function(){ 
    function doSomething2(){ 
     $.ajax 
      { url: 'update.php', 
       data: { userid : userid }, 
       type : 'GET', 
       dataType: 'json', 
       success : function (jsXHR) { 
       }, 
       failed : function(status,data){ } 
       } 
     ); 
    } 

}); 
</script> 

<?php 
$subid = $_GET['userid']; 
?> 

這裏是update.php腳本(我知道這不是MySQL的 - 是的,我需要一本新書。)我只是無法得到上面的JS調用它。

$uped = 1; 
$subid = $_REQUEST['userid']; 
mysql_query("UPDATE ".MYSQLTABLE." SET view=view+".$uped." WHERE userid='".$subid."'") or die(mysql_error()); 

mysql_close(); 

?> 
+1

你在哪裏設置在JS'userid'? – Barmar

+0

先看$ subid = $ _GET ['userid'];而不是$ subid = $ _REQUEST ['userid']; –

+1

你的圓括號和大括號都在'$ .ajax'周圍搞砸了。 – Barmar

回答

0

這似乎是爲我工作。

function count() { 
 
     window.secsLeft--; 
 
     if (window.secsLeft > 0) { 
 
      $('#countdw').val('Wait ' + window.secsLeft + 's'); 
 
     } else if (window.secsLeft === 0) { 
 
      $('#countdw').removeClass('disabled'); 
 
      $('#countdw').val('Go'); 
 
      $('#countdw').attr("onclick", "doSomething2()"); 
 
     } 
 
    } 
 

 

 
    function doSomething2() { 
 
      $.ajax({ 
 
       url: 'update.php', 
 
       data: { 
 
        userid: <?php echo $userid; ?> 
 
       }, 
 
       type: 'GET', 
 
       dataType: 'json', 
 
       success: function (data) { 
 
        console.log(data); 
 
       }, 
 
       error: function (data) { 
 
        console.log(data); 
 
       } 
 
      }); 
 
     } 
 
    
 
    (function() { 
 
     "use strict"; 
 
     window.secsLeft = 30; 
 

 
     setInterval('count()', 1000); 
 
    }());
<input type="button" id="countdw" value='Wait 30s' class="btn btngo disabled">