Web Analytics Made Easy -
StatCounter onclick Event - CodingForum

Announcement

Collapse
No announcement yet.

onclick Event

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • onclick Event

    Hi,

    From a button_onclick, can I call a php script, if so please advise me, how I can do this.
    <input type=button value="Search" onclick=SearchClick()>

    function SearchClick()
    {
    $sql = "select * from table where Name like '%hello%'";
    $result = mysql_query($sql);
    if($row = mysql_num_rows($result)){
    echo 'Number of results found '.$row.'.';
    }else{
    echo 'No results';
    }
    }

  • #2
    no that wont work...the php code will run before the page loads (before the button shows) so ..the php code and button will have to be on seperate pages
    ~Designer's Toolz~

    Comment


    • #3
      Originally posted by DsgnrsTLZAdmin
      no that wont work...the php code will run before the page loads (before the button shows) so ..the php code and button will have to be on seperate pages
      Yes the PHP code runs before the page but no you don't have to have the code in a seperate page.
      Spookster
      CodingForum Supreme Overlord
      All Hail Spookster

      Comment


      • #4
        Do I have to post the page to itself then!

        Comment


        • #5
          Re: onclick Event

          Originally posted by j-iN-uK
          Hi,

          From a button_onclick, can I call a php script, if so please advise me, how I can do this.
          <input type=button value="Search" onclick=SearchClick()>

          function SearchClick()
          {
          $sql = "select * from table where Name like '%hello%'";
          $result = mysql_query($sql);
          if($row = mysql_num_rows($result)){
          echo 'Number of results found '.$row.'.';
          }else{
          echo 'No results';
          }
          }
          You can not directly call a function in PHP via either a hyperlink or a form button. A similar question to this was just asked the other day and I've explained it here:

          http://www.codingforum.net/showthrea...threadid=33244

          Yours would be done just slightly different when using a form button. You could use the $_POST array instead or still use the $_GET array whereas a hyperlink could only use the $_GET array. In your case you could check something like this:

          PHP Code:

          <?php
          function SearchClick(){
              
          //your search function code here
          }

          if(isset(
          $_POST['search'])){
              
          SearchClick();
          }
          ?>

          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
          <input type="submit" name="search" value="Search">
          </form>
          Spookster
          CodingForum Supreme Overlord
          All Hail Spookster

          Comment


          • #6
            thanku, that worked fine......

            Comment

            Working...
            X