2016-01-07 26 views
-2

我有一个php代码有问题。这是一个简单的网页,我的电脑上有两个按钮和一个文本框。当我点击+或 - 按钮时,文本框中的数字会增加或减少。除非我想使用php代码将textBox.value写入文本文件,否则一切都按原样运行。当它应该是“value = 0”时,结果就像“value = textBox.value”。谢谢。 的代码如下:如何用php编写文本文件中的变量值

<!DOCTYPE html> 
<html> 
<body> 

<input id="increaseNumber" type="button" value="+" style="font-size:24pt; width: 50px; height: 50px;"><br> 
<input id="textBox" type="text" value="0" style="font-size:24pt; width: 40px; height: 40px;"> 
<label style="font-size:24pt;">&#8451;</label><br> 
<input id="decreaseNumber" type="button" value="-" style="font-size:24pt; width: 50px; height: 50px;"><br> 

<script> 

decreaseNumber.onclick = function() { 
    textBox.value = parseInt(textBox.value) -1 
} 
increaseNumber.onclick = function() { 
    textBox.value = parseInt(textBox.value) + 1 
} 

</script> 

<?php 
$myfile = fopen("/home/pi/test/test.txt", "w") or die("Unable to open file!"); 

$txt = "value = "; 
fwrite($myfile, $txt); 
$v = textBox.value; 
fwrite($myfile, $v); 
fclose($myfile); 
?> 

</body> 
</html> 
+0

错误,如果有的话?它正在写入该文件吗?检查你的控制台也 –

+0

'$ v = textBox.value;'看起来不像PHP。错误报告也会告诉你。 – Qirel

+0

^真实和错误报告会抛出未定义的常量声明。 http://php.net/manual/en/function.error-reporting.php –

回答

0

简要说明:

您正在尝试仿佛“按需”脚本语言两者都是客户端,以两种不同的语言结合起来。

Javascript是客户端,因此将基于事件侦听器或任何其他定义的参数运行。 PHP是服务器端,因此,将在文件解析时运行。这意味着,当Javascript甚至进入图片时,您的PHP已经被处理并且不在图片之中。

什么是你的脚本发生目前:

这个文件被解析,PHP是确定的,并且运行第一。运行后立即写入文件。 PHP默认为“textBox.value”的文本值,因为它不是一个实际变量(如果是的话,它会以$开头)。诚实地说,它应该返回一个错误,即“textBox.value”意味着什么,本质上(PHP不严格,所以它做了很多假设)。然而,这并不是因为它假设你引用了一个常量。处理完PHP后,DOM将被处理并发送到浏览器。 PHP在这个时候甚至不存在,并且已经被剥离了DOM(理所当然 - 您永远不希望PHP对客户端可见)。

怎么办:

你可以选择你想要增加/用PHP是同一个文件内减少数值,每次不运行这个PHP代码段(至少在没有表单提交,或某事以“抓住”请求 - 我的回应将基于这样一个事实,即单击时只需要它写入,而不是每次都提交表单)。你必须改变你的代码。我建议的一件事就是把PHP放在它自己的文件中。然后,在增加/减少该值时,您可以使用AJAX实际命中该文件(从而触发文件写入)。

实施例:

的index.html

<!DOCTYPE html> 
<html> 
<head> 
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
</head> 
<body> 

<input id="increaseNumber" type="button" value="+" style="font-size:24pt; width: 50px; height: 50px;"><br> 
<input id="textBox" type="text" value="0" style="font-size:24pt; width: 40px; height: 40px;"> 
<label style="font-size:24pt;">&#8451;</label><br> 
<input id="decreaseNumber" type="button" value="-" style="font-size:24pt; width: 50px; height: 50px;"><br> 

<script> 

$("#decreaseNumber").on("click", function() { 
    $("#textBox").val(parseInt($("#textBox").val()) -1); 
    writeToFile($("#textBox").val()); 
}) 
$("#increaseNumber").on("click", function() { 
    $("#textBox").val(parseInt($("#textBox").val()) +1); 
    writeToFile($("#textBox").val()); 
}) 

function writeToFile(value){ 

    $.ajax({ 
     url: 'write.php?v=' + value, 
     type: 'GET', 
     dataType: 'json', 
     success: function(ret){ 
      alert('Success!'); 
     }, 
     error: function(ret){ 
      alert('Error!'); 
     } 
    }); 

} 


</script> 

</body> 
</html> 

write.php

<?php 
$myfile = fopen("/home/pi/test/test.txt", "w") or die("Unable to open file!"); 

$txt = "value = " . $_GET['v']; 
fwrite($myfile, $txt); 
fclose($myfile); 
?> 

注变迁:

我在我的例子中使用JQuery。如果你想严格使用本地Javascript,我很抱歉。

另请注意,我更改了PHP。您有两次写入,这是不必要的,需要更多时间。文件输入/输出成本很高,应尽量少用。实际上,在这个例子中,我个人只写了一个数据库。然而,你并没有要求单独的解决方案,所以我只是提供了一个类似于你所写的例子。

随想

可以使用的,而不是你在做什么,以及递减/递增。像这样:

<script> 

decreaseNumber.onclick = function() { 
    textBox.value--; 
    alert(textBox.value); 
} 
increaseNumber.onclick = function() { 
    textBox.value++; 
    alert(textBox.value); 
} 

</script> 
+0

您的示例最适合我需要的内容,但每次执行代码时,它都会给我“错误”并且永远不会“成功”,即使一切都按原样运作。所以我删除了Ajax警报,现在它很完美。非常感谢你。 – Mugur

+0

完全没问题!您也可以更深入地了解这一点,并在AJAX中有一些错误回应。例如,如果它无法打开文件,您将收到一个响应,而不是刚刚死亡(如果您没有看到开发人员工具中的“网络”选项卡,则使用AJAX有时难以调试)。如果你想看到它,我很乐意编辑这篇文章来展示如何实现。 –

0

要让php做到这一点,您需要提交表单。你可以通过ajax或普通的提交后完成。

我已经修改了您的代码以进行基本的发布提交。 请阅读有关发布方法的信息。

我不得不在你的HTML以及php做出微小的改变。 我已经评论他们的简单。

你不能将两种不同的语言结合在一起工作,至少在这种情况下不能。您可以将值从PHP分配给JAVASCRIPT变量,但不能以其他方式分配。

只要提交输入字段,PHP就能够读取输入字段的值。 如果您想为PHP在每次数量增加或减少时写入文件,你将不得不做一个Ajax请求对这些特定事件得到这个工作的PHP脚本。(的onClick)

<!DOCTYPE html> 
<?php 
if($_SERVER['REQUEST_METHOD']=='POST') 
{ 
    $myfile = fopen("/home/pi/test/test.txt", "w") or die("Unable to open file!"); 

    //You dont need to write twice, you can use both values in one single variable and write once 
    //added whitespce \r\n for line break 
    $v = $_POST['textbox']; 
    $txt = "value = $v"; 
    fwrite($myfile, $txt); 
    fclose($myfile); 
    echo "written to file"; 
} 
?> 
<html> 
<body> 
<!--Made this a form so it can be submit to php--> 
<!--Read about POST method--> 
<form method="POST" action=""> 
<input id="increaseNumber" type="button" value="+" style="font-size:24pt; width: 50px; height: 50px;"><br> 
<!-- Add the name attribute so php can read the value --> 
<input name ='textbox' id="textBox" type="text" value="0" style="font-size:24pt; width: 40px; height: 40px;"> 
<label style="font-size:24pt;">&#8451;</label><br> 
<input id="decreaseNumber" type="button" value="-" style="font-size:24pt; width: 50px; height: 50px;"><br> 
<input type='submit' value='Submit'/> 
</form> 
<script> 

decreaseNumber.onclick = function() { 
    textBox.value = parseInt(textBox.value) -1 
} 
increaseNumber.onclick = function() { 
    textBox.value = parseInt(textBox.value) + 1 
} 

</script> 
</body> 
</html> 

链接,这将是对你有用

$ _ POST - http://php.net/manual/en/reserved.variables.post.php

PHP表单处理 - http://www.w3schools.com/php/php_forms.asp

AJ AX请求 - http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

0

在这里,你很容易在javascript中获得价值,如果你想获得值,那么你必须发布表单或通过URL中的参数发送。这里有一些方法来传递数据在PHP中:
- 使用$ _POST传递PHP变量
- 在与$ _GET的链接中传递PHP变量
- 传递不带POST或GET的PHP变量| $ _SESSION
- 传递PHP变量数组从一个网页到另一个
- 传递PHP Cookie变量

<!DOCTYPE html> 
<html> 
<body> 
<form action="" method="post"> 
<input id="increaseNumber" type="button" value="+" style="font-size:24pt; width: 50px; height: 50px;"><br> 
<input id="textBox" type="text" value="0" name="textBox" style="font-size:24pt; width: 40px; height: 40px;"> 
<label style="font-size:24pt;">&#8451;</label><br> 
<input id="decreaseNumber" type="button" value="-" style="font-size:24pt; width: 50px; height: 50px;"><br> 
<input type="submit" name="submit" value="Write To File"> 
</form> 
<script> 

decreaseNumber.onclick = function() { 
    textBox.value = parseInt(textBox.value) -1 
} 
increaseNumber.onclick = function() { 
    textBox.value = parseInt(textBox.value) + 1 
} 

</script> 

<?php 

if(isset($_POST['submit'])){ 
    $myfile = fopen("/home/pi/test/test.txt", "w") or die("Unable to open file!"); 

    $txt = "value = "; 
    fwrite($myfile, $txt); 
    $v = $_POST['textBox']; 
    fwrite($myfile, $v); 
    fclose($myfile); 
} 
?> 

</body> 
</html> 
0

你混的事情了,它不会像这样工作。

首先,奉上<form>指向PHP文件,将处理增加/减少功能,并将其写入到test.txt文件中的输入框:

<form id="handler" method="post" action="handler.php"> 
    <!-- Your input elements here --> 
</form> 

而且,你会需要将属性name添加到textBox元素,您可以在示例中将其称为nvalue

其次,您的点击事件侦听器无效。设置它们正确的是:

document.getElementById("decreaseNumber").addEventListener("click", function() { 
    document.getElementById("textBox.value").value--; 
    document.getElementById("handler").submit(); 
}); 

同样为increaseeNumber

第三,把你的PHP在新handler.php文件。用$_POST['nvalue']获取增加/减少值。将此值写入文件。

注意:您也可以为此使用AJAX,但它更高级一点,您也可能需要JS库(如jQuery)。

注2:您可以通过使用PHP header()功能更新test.txt文件后,从handler.php到表单页面重定向回来。

注3:你可以让你通过<input id="textBox">上面加载它,呼应value = "<?php echo $nvalue; ?>",当然,$ n值是使用fread()或类似得到显示文件test.txt当前值。

相关问题