shadramon
shadramon

Reputation: 59

jQuery loading value from php file into html input on button click - not working

i have a text file with a number that is inside it. The number should be +1 inside the text file and the new value should be updated inside index.php all this should happen after a button inside index.php is clicked, but thats not happening.. i did a lot of googling and i tried many things from what i sow still it's not working, keeping in mind I'm new to jQuery. below is all the involved code explained. any help will be appreciated!

The php script inside index.php to retrieve the value from num.txt and place it inside the text input once index.php is loaded, this works perfectly:

<?php
$filename = "num.txt";
$file = fopen("num.txt","r+");
$number = fread($file, filesize($filename));
fclose($file);
?>

The text input code, as you can see will take the $number value from the above script and this works fine. keep in mind i used the id of the input and the class of it then i ended up adding a div and using its class, i didn't know what to do so i tested them all, same thing nothing worked:

<div class="on"><input type="text" id ="omlat" class="inv-number" value="<?php echo $number;?>"></input></div>

jQuery to update the value after clicking on the submit button. this function should only refresh the value of the input value by calling inum.php and taking the value inside inum.php after the code there is excited:

$(document).ready(function(){
$(".reloadpg").click(function(){
$(".on").load("http://localhost/einvoice/inum.php");  
});
});

Code inside inum.php, this code works fine i tested it (this code takes the number inside num.txt and +1 the value as you can see):

<?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);
?>

-- Update --

The code bellow worked for the above part it worked perfectly but now I'm facing another problem. Another function that listens to the same button that was working before stopped working! so what i did was that i toke some of the code that the guys bellow provided and pot it inside the older function that was listening to the button click the whole code is as follows(please read the comments to understand the code):

$('.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);
    }
})

Upvotes: 0

Views: 1485

Answers (2)

ArGh
ArGh

Reputation: 1168

You jquery part is not good. Try this :

$(document).ready(function(){
    $(".reloadpg").click(function(){
        $.get( "/einvoice/inum.php", function( data ) {
          $("#omlat").val(data);
        });
    });
}); 

Upvotes: 0

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

You're replacing the input with the number, rather than just updating its value. Try this instead...

$(document).ready(function(){
    $(".reloadpg").click(function(){
        $.get("http://localhost/einvoice/inum.php", function(data) {
            $(".on input").val(data);
        });  
    });
});

That uses jQuery's get() method to make an ajax call that passes the response into a callback function as a parameter. You can then do whatever you need with it inside that function.

Upvotes: 1

Related Questions