0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

its not working =( the value is changing in the text file but it is not updated when the button is clicked i have to refresh the page ..
The code I've given you will work, so there must be something else causing a problem. Do you get any errors in the console? Also, put console.log(data); as the first line inside the get callback and check the console for that as well.
it worked perfectly! Thanks a lot. but now another function that listens to the same button stopped working =( ill post the code and update my question. I'm new to this..any help from you would be really appreciated.
Hi shadramon. I'm glad we got your initial problem fixed. Could you post the new issue as a separate question? As well as it being at the top of the page and therefore getting more exposure, it's also the correct thing to do for Stack Overflow users who search for problems. This is a different issue (although related), so should be posted as such.
Hello, i did as you said.. thanks again stackoverflow.com/questions/23529851/…
0

You jquery part is not good. Try this :

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

1 Comment

the value is bing changed inside the text file but its not bing updated in index.php once the button is clicked =( i need it to be updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.