如何将Javascript变量传递给PHP

如何将Javascript变量传递给PHP,php,javascript,html,Php,Javascript,Html,可能重复: 我想将用户点击的点击次数与数据库中保存的数据进行比较。 我不知道如何继续传递HTML“clicks”的值以与PHP中的“counts”进行比较 var计数=0; 函数countClicks() { 计数=计数+1; document.getElementById(“clicks”).innerHTML=count; } 警报(“计数为空!!”; 您以错误的方式使用PHP和Javascript。PHP是一种服务器端语言。这意味着它在页面加载到浏览器之前就已经

可能重复:

我想将用户点击的点击次数与数据库中保存的数据进行比较。 我不知道如何继续传递HTML“clicks”的值以与PHP中的“counts”进行比较


var计数=0;
函数countClicks()
{          
计数=计数+1;
document.getElementById(“clicks”).innerHTML=count;
}  
警报(“计数为空!!”;

您以错误的方式使用PHP和Javascript。PHP是一种服务器端语言。这意味着它在页面加载到浏览器之前就已经运行了

您必须创建一个javascript点击计数器,并将其值放入一个隐藏的formfield中。然后使用submit按钮将信息发送到服务器(PHP)。然后让PHP从数据库中进行检查和选择,并返回答案


另一个解决方案是使用javascript-AJAX,但我建议首先尝试上述方法。

最好的方法是调用(AJAX)PHP是一种服务器端语言,在构建HTML(因此,在Javascript之前)并向浏览器显示之前执行

因此,Javascript与PHP交换变量和数据的唯一方法是进行AJAX调用(您总是可以使用表单submit或and重新加载页面,但如果操作过于频繁,这不是最好的方法)

在AJAX中,您可以创建另一个PHP页面,该页面将检查这两个值并返回您想要的任何内容

我建议您阅读更多关于AJAX的内容,并了解它


编辑:在阅读了你的评论之后,我决定在这里放一个简单的例子

Javascript(在HTML页面中)

<?php
if(!isset($_GET['username']) || !isset($_GET['clicks']))
    die("Error");

$username = $_GET['username'];
$jsClicks = $_GET['clicks'];
$phpClicks = null;

#I am using the mysqli class to execute the query since mysql is deprecated in PHP5.
$data = mysqli_query("SELECT clicks FROM customerdetails WHERE customer_username='$username'"); 

while($row = mysqli_fetch_array($data))
{
    $phpClicks = $row['clicks'];
}

#If your "IF" only contains one line, you don't need brackets.
#Otherwise they are needed.

if($phpClicks == null)
    die("Could not get the number of clicks of the desired username");

#This is the string PHP will send to Javascript.
$response = "Same number of clicks!";

#If phpClicks is different and has the same type as jsClicks...
if($phpClicks !== $jsClicks)
{
    $response = "Number of clicks changed!";
    if($jsClicks > $phpClicks)
    {
        #Updates the number of clicks the user has done.
        $mysqli_result = mysqli_query("UPDATE customerdetails SET clicks=$jsClicks WHERE customer_username='$username';"); 
    }
}

echo json_encode(array('response'=>$response));
PHP(这里是名为getUserClicks.PHP的文件)

<?php
if(!isset($_GET['username']) || !isset($_GET['clicks']))
    die("Error");

$username = $_GET['username'];
$jsClicks = $_GET['clicks'];
$phpClicks = null;

#I am using the mysqli class to execute the query since mysql is deprecated in PHP5.
$data = mysqli_query("SELECT clicks FROM customerdetails WHERE customer_username='$username'"); 

while($row = mysqli_fetch_array($data))
{
    $phpClicks = $row['clicks'];
}

#If your "IF" only contains one line, you don't need brackets.
#Otherwise they are needed.

if($phpClicks == null)
    die("Could not get the number of clicks of the desired username");

#This is the string PHP will send to Javascript.
$response = "Same number of clicks!";

#If phpClicks is different and has the same type as jsClicks...
if($phpClicks !== $jsClicks)
{
    $response = "Number of clicks changed!";
    if($jsClicks > $phpClicks)
    {
        #Updates the number of clicks the user has done.
        $mysqli_result = mysqli_query("UPDATE customerdetails SET clicks=$jsClicks WHERE customer_username='$username';"); 
    }
}

echo json_encode(array('response'=>$response));

请不要使用
mysql.*
函数来编写新代码。它们不再被维护,社区已经开始。请看?相反,您应该了解并使用或。如果您不能决定使用哪一个,将对您有所帮助。如果您选择PDO,.Hi,ghilled。感谢您的建议。我已经在w3schoo中阅读了AJAX教程ls.你能更详细地解释一下我如何用AJAX解决我的问题吗?很抱歉,我在编程方面很弱。但是,我正在努力学习。通过使用AJAX,我应该将javascript和php文件分开?然后,运行php文件,用AJAX调用javascript中的函数?比如:xmlhttp.open(“GET”,“click.html”,true);?那么如何仅检索“clicks”"在javascript中并与数据库中的数据进行比较?我正在为您制作一个示例。我会在完成后立即编辑我的文章。非常感谢您~我也在尽我最大的努力..但我不知道应该在哪里划分这些部分。例如.php或.html~混淆的函数请查看我的编辑。我希望这会有所帮助我建议你经常使用W3Schools,谷歌总是很有帮助;-)。我可以知道mysql和mysqli有什么不同吗?噢~刚刚看到你的//评论~谢谢你的评论~
<?php
if(!isset($_GET['username']) || !isset($_GET['clicks']))
    die("Error");

$username = $_GET['username'];
$jsClicks = $_GET['clicks'];
$phpClicks = null;

#I am using the mysqli class to execute the query since mysql is deprecated in PHP5.
$data = mysqli_query("SELECT clicks FROM customerdetails WHERE customer_username='$username'"); 

while($row = mysqli_fetch_array($data))
{
    $phpClicks = $row['clicks'];
}

#If your "IF" only contains one line, you don't need brackets.
#Otherwise they are needed.

if($phpClicks == null)
    die("Could not get the number of clicks of the desired username");

#This is the string PHP will send to Javascript.
$response = "Same number of clicks!";

#If phpClicks is different and has the same type as jsClicks...
if($phpClicks !== $jsClicks)
{
    $response = "Number of clicks changed!";
    if($jsClicks > $phpClicks)
    {
        #Updates the number of clicks the user has done.
        $mysqli_result = mysqli_query("UPDATE customerdetails SET clicks=$jsClicks WHERE customer_username='$username';"); 
    }
}

echo json_encode(array('response'=>$response));