2

I have the following function on a button:

$(function() {
    $("#submit").click(function(){
        $.ajaxSetup({ cache: false });
        $.ajax({
        url:'crfile.php',
        type:'GET',
    });
};

It calls the following PHP file:

if (session_status() == PHP_SESSION_ACTIVE){
    session_destroy();
}
session_start();

$extension = ".txt";
$_SESSION['fname'] = substr(md5(rand()), 0, 7).$extension;
fopen("temp/" . $_SESSION['fname'], 'w');
$fname = $_SESSION['fname'];

It creates a file with a random name in the temp/ folder. Both are working properly except when I click the #submit button a second time without reloading the page. It reruns the function, however it doesn't generate a new filename in the PHP script.

I thought it was cache or sessions, however neither is the case. I am using PHP 5.5.

Can someone please advise?

1 Answer 1

1

At first, you need to check your ajax function. trying Use the method 'post', or use on('click',function(){ .....}) for your code.

Secondly, you need to make sure your ajax function really works. use 'print' and 'exit' for debugging.

if (session_status() == PHP_SESSION_ACTIVE){
    session_destroy();
}
session_start();

$extension = ".txt";
$_SESSION['fname'] = substr(md5(rand()), 0, 7).$extension;
//debugging command
print  $_SESSION['fname'];
print  'see this?';
exit;


fopen("temp/" . $_SESSION['fname'], 'w');
$fname = $_SESSION['fname'];

this way you will know whether your ajax work or not. You can use debugging command at the end of the code above.

Try those codes and let me know if you need more help.

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

Comments

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.