首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按一下jQuery从php文件加载值到html输入-不起作用

按一下jQuery从php文件加载值到html输入-不起作用
EN

Stack Overflow用户
提问于 2014-05-06 22:01:39
回答 2查看 1.5K关注 0票数 0

我有一个文本文件,里面有一个号码。文本文件中的数字应该是+1,而新的值应该在index.php中更新,所有这些都应该在单击index.php内的按钮之后发生,但这是不可能的。我做了很多谷歌搜索,我尝试了很多东西,从我播下的仍然是不起作用的,记住我是jQuery的新手。下面是所有涉及的代码解释。任何帮助都将不胜感激!

index.php中的php脚本可以从num.txt检索值,并在加载index.php后将其放置在文本输入中,这样做非常完美:

代码语言:javascript
复制
<?php
$filename = "num.txt";
$file = fopen("num.txt","r+");
$number = fread($file, filesize($filename));
fclose($file);
?>

如您所见,文本输入代码将从上面的脚本中获取$number值,这很好。请记住,我使用了输入的id和它的类,然后我添加了一个div并使用它的类,我不知道该做什么,所以我测试了它们,同样的事情没有什么用:

代码语言:javascript
复制
<div class="on"><input type="text" id ="omlat" class="inv-number" value="<?php echo $number;?>"></input></div>

jQuery在单击submit按钮后更新该值。此函数只应通过调用inum.php并在代码兴奋后在inum.php中获取值来刷新输入值的值:

代码语言:javascript
复制
$(document).ready(function(){
$(".reloadpg").click(function(){
$(".on").load("http://localhost/einvoice/inum.php");  
});
});

inum.php中的代码,这段代码运行良好--我测试了它(这段代码接受了num.txt中的数字,并得到了您可以看到的值+1 ):

代码语言:javascript
复制
<?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);
?>

-更新

下面的代码适用于上面的部分,它工作得很好,但现在我面临另一个问题。另一个功能,听相同的按钮,是工作之前停止工作!所以我所做的是,我拿出了一些那些家伙提供的代码,并将其放入正在监听按钮的旧函数中,单击整个代码如下(请阅读注释以理解代码):

代码语言:javascript
复制
$('.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);
    }
})
EN

Stack Overflow用户

发布于 2014-05-06 22:10:19

jquery部分不好。试试这个:

代码语言:javascript
复制
$(document).ready(function(){
    $(".reloadpg").click(function(){
        $.get( "/einvoice/inum.php", function( data ) {
          $("#omlat").val(data);
        });
    });
}); 
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23505276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档