Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2007
    Posts
    24

    Unanswered: How to pass parameter in Java script

    I am using php and mysql; in my code there is one block of javascript.
    and it works fine when it is single line as given bellow.

    <a href="#" onclick="return Redirect('http://localhost/readfile.php?file=12.pdf');"> 12.pdf </a>

    Here I want to pass parameter in place of : 12.pdf
    And the complete line in for loop.
    Can anybody know the solution…

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Just use PHP variable substitution as you would normally in html...

    Code:
    <a href="#" onclick="return Redirect('http://localhost/readfile.php?file=<?=$filename?>');"><?=$filename?></a>

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Of course if you're writing this out in a loop there is a good chance your loop will be inside the PHP code, thus :

    Code:
    <?php
    
    foreach($files as $filename) {
      echo "<a href=\"#\" onclick=\"return Redirect('http://localhost/readfile.php?file={$filename}');\">{$filename}</a>";
    }
    
    ?>
    As in the above example don't forget to escape your quotation marks...

  4. #4
    Join Date
    Apr 2007
    Posts
    24
    Your code is not working:

    <?php
    global $filename;
    $filename = "12.pdf";
    ?>

    <a href="#" onclick="return Redirect('http://localhost/readfile.php?file=<?=$filename?>');"><?=$filename? ></a>

    when I put <? $filename ?> it always pass blank value. i.e. no any file name.

  5. #5
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Which PHP version are you using?

    don't use global (sometimes invalid depending on PHP setup)

    Try this
    Code:
     <?
    $filename = "12.pdf";
    ?>
    
    <a href="#" 
    onclick="return Redirect('http://localhost/readfile.php?file=<?php echo $filename; ?>');">
    <?php echo $filename; ?>
    </a>
    You might need to use the long syntax as you're not getting an error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •