我有一个文本文件,里面有一个数字。文本文件中的数字应该是+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在单击提交按钮后更新值。这个函数应该只通过调用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);
}
})
答案 0 :(得分:1)
您正在使用数字替换输入,而不仅仅是更新其值。试试这个......
$(document).ready(function(){
$(".reloadpg").click(function(){
$.get("http://localhost/einvoice/inum.php", function(data) {
$(".on input").val(data);
});
});
});
使用jQuery的get()
方法进行ajax调用,将响应作为参数传递给回调函数。然后,您可以在该函数内随意执行任何操作。
答案 1 :(得分:0)
你的jquery部分不好。试试这个:
$(document).ready(function(){
$(".reloadpg").click(function(){
$.get( "/einvoice/inum.php", function( data ) {
$("#omlat").val(data);
});
});
});