2014-10-01 152 views
0

這裏是我的javascript代碼從JavaScript內部調用PHP函數?

<script> 
$(document).ready(function(){ 
$(".five").click(function(){ 
<?php echo updatepoints();?> 
}); 
}); 
</script> 

這裏是我的PHP代碼

<?php 
function updatepoints() { 
mysql_connect("localhost","user","password") or die (mysql_error()); 
mysql_select_db("database") or die ("Cannot connect to database"); 
$query = mysql_query("SELECT *from member WHERE username='" . $_SESSION["username"] . "'"); 
$row=mysql_fetch_array($query); 
$points = $row["points"]; 
$points = $points + 5; 
mysql_query("UPDATE member set points='" . $points . "' WHERE username='" . $_SESSION["username"] . "'"); 
} 
?> 

這兩個代碼是同一個頁面下方其他。這似乎沒有更新值。我應該如何繼續?

+3

如果你想要它在點擊生活加載該PHP需要做AJAX。這個實例只會在頁面加載完成時加載一次php。看看AJAX jQuery – Rasclatt 2014-10-01 04:26:36

+1

* ...加上,*我認爲你有'session_start();'加載某處? – 2014-10-01 04:28:04

+0

嘗試回顯更新查詢並執行它以查找任何錯誤。 – Jenz 2014-10-01 04:30:25

回答

1

創建一個名爲ajax_update.php的文件。在該文件中添加你的函數和用戶ajax來調用該函數。

ajax_update.php

<?php 
function updatepoints() { 
     mysql_connect("localhost","user","password") or die (mysql_error()); 
     mysql_select_db("database") or die ("Cannot connect to database"); 
     $query = mysql_query("SELECT *from member WHERE username='" . $_SESSION["username"] . "'"); 
     $row=mysql_fetch_array($query); 
     $points = $row["points"]; 
     $points = $points + 5; 
     mysql_query("UPDATE member set points='" . $points . "' WHERE username='" . $_SESSION["username"] . "'"); 
} 
if($_POST['action_type'] == 'update') { 
    updatepoints(); 
} 
?> 

改變你這樣的腳本

<script> 
$(document).ready(function(){ 
    $(".five").click(function(){ 
     $.ajax({ 
      type : "POST", 
      url : "ajax_update.php", 
      data : {action_type:"update"}, 
      success : function(html){ 
      } 
     }); 
    }); 
}); 
</script> 

你可以改變你的Ajax PHP文件作爲Ajax和類單獨的文件。在類中添加函數。讓它成爲工整清晰

+0

謝謝你,你的回答是最容易理解的,它的工作:) – 2014-10-01 05:25:47

2

這裏,試試這個:

旁註:content.php將是你的SQL文件,該文件被稱爲xmlhttp.open("GET","content.php",true);

這會工作,假設你的SQL已經很好。

<!doctype html> 

<head> 

<script> 
function loadXMLDoc() 
{ 
var xmlhttp; 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
    } 
} 
xmlhttp.open("GET","content.php",true); 
xmlhttp.send(); 
} 

</script> 

</head> 

</head> 
<body> 
<script> 
setInterval(loadXMLDoc,5000); 
</script> 
<h2>AJAX</h2> 
<button type="button" onclick="loadXMLDoc()">Request data</button> 

<div id="myDiv"></div> 

</body> 
</html> 
1

您可以使用jQuery,這讓事情變得更簡單:

<script> 
$(document).ready(function(){ 
    $.post("update_user_points.php", { username: 'someuser' }, function(response) { 
     console.log(response);//check your console 
    }); 
}); 
</script> 

我沒有看到你是如何開始的會議,但這個例子通過從客戶機到PHP的username值。如果你只是遞增5點,你可以更好地做到這一點檢查這個:

<?php 
if(isset($_POST['username']){ 

function updatepoints($username) { 

    mysql_connect("localhost","user","password") or die (mysql_error()); 
    mysql_select_db("database") or die ("Cannot connect to database"); 
    $query = sprintf("UPDATE member set points = points + 5 WHERE username='%s'", 
         mysql_real_escape_string($username)); 
    // Perform Query 
    $result = mysql_query($query); 

    // Check result 
    if (!$result) { 
     $message = 'Invalid query: ' . mysql_error() . "\n"; 
     $message .= 'Whole query: ' . $query; 
     die($message); 
    }else{ 
     echo 'update success'; 
    } 
} 
//call the function 
updatepoints($_POST['username']); 
} 
?>