1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP POST method using javascript

Discussion in 'Programming' started by Tombik, Sep 22, 2009.

  1. #1
    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:
     
            <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>
     
    Code (text):
    $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?
     
    Tombik, Sep 22, 2009 IP
  2. ohteddy

    ohteddy Member

    Messages:
    128
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Is postlink part of the Javascript api or are you using a library like jquery? Anyway my suspicion is that you need to call postlink on the form not the document.

    $('form_id').postlink
     
    ohteddy, Sep 23, 2009 IP
  3. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How are you coding the loop? Maybe there is a problem with the "a tag" in the loop. But if you can "alert" the right file name then... With a button does it work?
     
    pneulameiro, Sep 23, 2009 IP
  4. Tombik

    Tombik Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I forgot to write that this problem is already SOLVED. Sorry for this, I wrote it at three forums. It was the problem with the loop. :)
     
    Tombik, Sep 24, 2009 IP