Stop PHP executing on page load

Discussion in 'PHP' started by gilgil2, Jan 4, 2012.

  1. #1
    Hi

    I have a problem with several of my PHP scripts, they all execute when the page loads.

    An example is one searches a database but because i have it set to Where table LIKE '%searchterm%' as it execute when it loads it brings up everything in the table, which isn't great.

    Could anyone tell me how to prevent this?

    Thanks
     
    Solved! View solution.
    gilgil2, Jan 4, 2012 IP
  2. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #2
    Can you post the part of your PHP code which executes on pageload?
     
    dujmovicv, Jan 4, 2012 IP
  3. gilgil2

    gilgil2 Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Hi here is one of them. It gives all the band names as results.

    
    <form method='post'>Band Name: <input type="text" name="BandName" /><input type="submit" value="search" /></form>
    <?php 
    $result = mysql_query("SELECT * FROM bandsWHERE BandName LIKE '%$BandName%'");
    while($row = mysql_fetch_array($result))  {  echo $row['BandName'] . " " . $row['Genre'];  echo "<br />";  }
    
    ?>
    
    HTML:
    Thanks
     
    gilgil2, Jan 5, 2012 IP
  4. #4
    
    <form method="post">Band Name: <input type="text" name="BandName" /><input name="search_now" type="submit" value="search" /></form>
    <?php
    if (isset($_POST['search_now'])) {
        $result = mysql_query("SELECT * FROM bands WHERE BandName LIKE '%$BandName%'");
        while($row = mysql_fetch_array($result))  {  echo $row['BandName'] . " " . $row['Genre'];  echo "<br />";  }
    }
    ?>
    
    PHP:
     
    dujmovicv, Jan 5, 2012 IP
    gilgil2 likes this.
  5. gilgil2

    gilgil2 Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Works perfectly, thank you! How do I mark as solved?
     
    gilgil2, Jan 5, 2012 IP
  6. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #6
    You're welcome! Not sure.... I haven't started a thread yet...
     
    dujmovicv, Jan 5, 2012 IP