PHP POST method using javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tombik
Forum Newbie
Posts: 2
Joined: Tue Sep 22, 2009 1:18 pm

PHP POST method using javascript

Post 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?
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: PHP POST method using javascript

Post 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?
Tombik
Forum Newbie
Posts: 2
Joined: Tue Sep 22, 2009 1:18 pm

Re: PHP POST method using javascript

Post 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! :)
Post Reply