Page 1 of 1

PHP POST method using javascript

Posted: Tue Sep 22, 2009 1:34 pm
by Tombik
This is my situation:
I have a directory listing script that lists through directories with backups. Each file (backup) has link labeled "restore" which trigger a javascript function submitPostLink() and that function trigger another script which restore selected backup.
The important part of a code is:

Code: Select all

 
        <script language=javascript type=text/javascript>
        function submitPostLink(file) {
              if(confirm("Do you really want to restore backup "+ file +"?")) {
                    document.postlink['backup'].value = file;
                    document.postlink.submit();
              }
        }
        </script>
        <form action="index.php" name=postlink method="post">
                <input type="hidden" name="backup" value="">
        <a href=# onclick="submitPostLink('<?php echo $files[$i] ?>');">restore</a>
 
$file[$i] is a variable with a current name of a file. I need that to POST it not to GET it and it has to be a hyperlink not a button or something like that. That's why I'm using a javascript, to submit that form.

When I'm listing a directory with only one backup and I click on restore hyperlink, it works well, but when there are more than one backup, the javascript function is called with a right variable (I know that because 'confirm("Do you really want to restore backup "+ file +"?"))' has a right variable file) but will not submit that form.

Could you help me sort it out, please?

Re: PHP POST method using javascript

Posted: Wed Sep 23, 2009 12:36 am
by Robert07
It appears that you are sharing the part of the code that works. You mentioned that it works with only one backup, but doesn't with more than one. Can you add the loop which displays more than one backup? Where is your </form> tag?

Re: PHP POST method using javascript

Posted: Wed Sep 23, 2009 4:04 am
by Tombik
Ok my problem has been SOLVED.
I defined that javascript function in for loop so in generated HTML code was that javascript function defined several times. Same for that form. Now, there are off the loop and everything works just fine :)

BIG THANKS! :)