我有一个文本文件,里面有一个数字。在文本文件中,该数字应为+1,并且应在index.php中更新新值。所有这些都应在单击index.php中的按钮后发生,但那没有发生..我做了很多谷歌搜索,我尝试了我播种的许多内容仍然无法正常工作,请记住我是jQuery新手。以下是所有涉及的代码说明。任何帮助将不胜感激!
index.php中的php脚本可从num.txt中检索值,并将其放入输入的文本中,一旦index.php加载完毕,即可完美运行:
<?php
$filename = "num.txt";
$file = fopen("num.txt","r+");
$number = fread($file, filesize($filename));
fclose($file);
?>
如您所见,文本输入代码将从上面的脚本中获取$ number值,并且可以正常工作。请记住,我使用了输入的id和它的类,然后我最终添加了一个div并使用了它的类,我不知道该怎么做,所以我对它们全部进行了测试,同样的东西没有用:
<div class="on"><input type="text" id ="omlat" class="inv-number" value="<?php echo $number;?>"></input></div>
jQuery在单击Submit按钮后更新值。此函数只应在调用代码后通过调用inum.php并将inum.php内的值刷新为输入值的值:
$(document).ready(function(){
$(".reloadpg").click(function(){
$(".on").load("http://localhost/einvoice/inum.php");
});
});
inum.php中的代码,此代码在我对其进行了测试时工作正常(此代码采用num.txt中的数字,并对值+1即可看到):
<?php
header("Cache-Control: no-cache");
$filename = "num.txt";
$file = fopen("num.txt","r+");
$number = fread($file, filesize($filename));
$number = $number + 1; //the new number to proceed
file_put_contents('num.txt', $number);
echo $number;
fclose($file);
?>
-更新-
波纹管在上面的部分工作正常,但效果很好,但是现在我面临另一个问题。另一个功能,可在停止工作之前监听正在使用的相同按钮!因此,我所做的是,我摘录了那些波纹管提供的代码,并将其放入监听按钮单击的旧函数中,整个代码如下(请阅读注释以了解代码):
$('.create-invoice').on('click',function()
{
//Below is the code i added from the first problem above its not working here.. when it was alone outside this function as the accepted answer it will work but it will stop this function from working!
$.get( "/einvoice/inum.php", function(data) {
$('.inv-number').val(data);
});
//Above is the code i added from the first problem..
//below is the original code for the function, keep in mind only the below code is bing excited the above is not.. its not refreshing the part of the page it should its calling the php script successfully tho, but i will have to refresh the page my self to see the number updated
grab_invoice_data();
// Declare a variable
var jsonObj = invoice_data;
// Lets convert our JSON object
var postData = JSON.stringify(jsonObj);
// Lets put our stringified json into a variable for posting
var postArray = {json:postData};
$.download("php/json.php", postArray, 'post');
//if cookie exists
var i_n = $('.inv-number').val();
$.cookie('lid', ++i_n, { expires: 365 } );
//invoices created
if( $.cookie('ic') ){
var ck_inv_created = ($.cookie('ic'));
$.cookie('ic', (++ck_inv_created));
} else {
$.cookie('ic', ++inv_created);
}
})
最佳答案
您将用数字替换输入,而不仅仅是更新其值。试试这个...
$(document).ready(function(){
$(".reloadpg").click(function(){
$.get("http://localhost/einvoice/inum.php", function(data) {
$(".on input").val(data);
});
});
});
它使用jQuery的
get()
方法进行ajax调用,该响应将响应作为参数传递给回调函数。然后,您可以在该函数中使用它进行所需的任何操作。