Onclick Alert popup in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrsjrs
    New Member
    • Sep 2006
    • 24

    Onclick Alert popup in php

    The "filetest.t xt" contents is here
    ............... ...
    one/yes
    two/no
    three/maybe
    four/fun
    five/ok
    ............... .

    The main program is here
    ............... ...........
    <?php
    srand((double) microtime() * 1000000);
    $readfile = file("filetest. txt");
    $k=rand(0, sizeof($readfil e)-2);
    list($quest,$an sw) = split("/",$readfile[$k]);
    print("$quest<B R>$answ<br>");
    ?>
    <button onClick="alert( 'Question is : <?=$quest?>')"> Question1</button>
    <button onclick="alert( <?php echo "'Question is : " . $quest ."'"; ?>)">Question2 </button><BR>
    <button onClick="alert( 'Answer is : <?=$answ?>')">A nswer1</button>
    <button onclick="alert( <?php echo "'Answer is : " . $answ ."'"; ?>)">Answer2</button>
    <script>
    <?php echo "var V='" . $answ ."';"; ?>
    </script>
    <button onclick="alert( V)">click</button>
    ............... ............... ..............

    All the above "alert" popups work with the "Question" ($quest), or first item after reading the text file.
    But none of them work with the "Answer" ($answ), or the second item in text file. Why?
  • raji20
    New Member
    • Aug 2006
    • 28

    #2
    Originally posted by jrsjrs
    The "filetest.t xt" contents is here
    ............... ...
    one/yes
    two/no
    three/maybe
    four/fun
    five/ok
    ............... .

    The main program is here
    ............... ...........
    <?php
    srand((double) microtime() * 1000000);
    $readfile = file("filetest. txt");
    $k=rand(0, sizeof($readfil e)-2);
    list($quest,$an sw) = split("/",$readfile[$k]);
    print("$quest<B R>$answ<br>");
    ?>
    <button onClick="alert( 'Question is : <?=$quest?>')"> Question1</button>
    <button onclick="alert( <?php echo "'Question is : " . $quest ."'"; ?>)">Question2 </button><BR>
    <button onClick="alert( 'Answer is : <?=$answ?>')">A nswer1</button>
    <button onclick="alert( <?php echo "'Answer is : " . $answ ."'"; ?>)">Answer2</button>
    <script>
    <?php echo "var V='" . $answ ."';"; ?>
    </script>
    <button onclick="alert( V)">click</button>
    ............... ............... ..............

    All the above "alert" popups work with the "Question" ($quest), or first item after reading the text file.
    But none of them work with the "Answer" ($answ), or the second item in text file. Why?




    TRy this code
    The problem is that there is the new line after the answer, so i have used trim function to remove the white spaces, and its working fine.

    <?php
    srand((double) microtime() * 1000000);
    $readfile = file("filetest. txt");
    $k=rand(0, sizeof($readfil e)-2);
    list($quest,$an sw) = split("/",$readfile[$k]);
    print("$quest<B R>$answ<br>");
    $answ = trim($answ);
    ?>
    <button onClick="alert( 'Question is : <?=$quest?>')"> Question1</button>
    <button onclick="alert( <?php echo "'Question is : " . $quest ."'"; ?>)">Question2 </button><BR>
    <button onClick="alert( 'Answer is : <?=$answ?>')">A nswer1</button>
    <button onclick="alert( <?php echo "'Answer is : " . $answ ."'"; ?>)">Answer2</button>
    <script>
    <?php echo "var V='" . $answ ."';"; ?>
    </script>
    <button onclick="alert( V)">click</button>

    Comment

    • jrsjrs
      New Member
      • Sep 2006
      • 24

      #3
      Thank you very much raji20. It works perfectly now!
      I was not aware that a carriage return needed a "trim".

      Would you or anyone else know how to change this script
      so that the alert popup is triggered from clicking on
      an image or a word (like a link) instead of on a button?

      Comment

      Working...