2015-04-29 41 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">