Hi, very simple one I think (and yet I can`t get it)!

I have a search query which works well, but on initially opening the page the search term is of course "", i.e. blank. This means my whole database is being pulled through onto the page the first time it is referenced.

How do I check that if $Search =="" then do nothing,

or, better still, if $Search <>"" do the search?

Many thanks.

:rolleyes:

    you can use javascript with the html, or php page, i.e. when the submit button is clicked, it will check the field whether it is empty or not, if it is then it will prompt an error, the code is omething like this:

    <html>
    <head><title>Checking</title>
    <SCRIPT LANGUAGE="JavaScript">

    <!-- Begin
    function noEntry() {
    mt1=document.thisForm.txt1.value;
    if (mt1.length<1) {
    alert("Name Field is Required!");
    return false;

    document.thisForm.empty.focus();
    }

    return true;
    }
    // End -->
    </SCRIPT>
    </head>
    <body>
    <form name=thisForm method=post action=search.php>
    Search: : <input type=text name=txt1><p>
    <input type=submit value=Invite onClick="return noEntry()">
    </form>
    </font>
    </body>
    </html>

    that is the php page will only be called only if there is some value in the field.

      Hi. Thanks for the rely.

      The submission part of the form works fine. It`s just when the page is first loaded, obviously there is nothing in the serach term, so $Search = ""

      I have tried:

      if ($Search <> "")
      {
      exectute code
      }

      Clearly if this is the case I do not want to execute my MySQL query, as otherwise I pull in over 3200 entries! But this is giving me parse errors. I am therefore guessing I need to use either NUL, isEmpty or empty. Or something else I am not aware about.

        Don`t worry about it - found the problem. Common one - left off a ;

        Thanks anyway

          instead of if($string=='') you can use if (!$string)

            Originally posted by AlGale
            Hi, very simple one I think (and yet I can`t get it)!

            I have a search query which works well, but on initially opening the page the search term is of course "", i.e. blank. This means my whole database is being pulled through onto the page the first time it is referenced.

            How do I check that if $Search =="" then do nothing,

            or, better still, if $Search <>"" do the search?

            Many thanks.

            :rolleyes:

            you can use:

            if(isset($String)){
             //execute search queries here
            }
            

            OR
            if the form is submitted and you want to make sure something was entered:

            if(strlen(trim($String)) > 0){
             //execute search queries here
            }
            

            this ensures that if someone can't just hit the space bar in the input field and also saves you from executing a needless query.

            • keith
              Write a Reply...