Undefined index: client_firstname in C:\Program Files\EasyPHP-5.3.3\www\client_search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jim king

    Undefined index: client_firstname in C:\Program Files\EasyPHP-5.3.3\www\client_search

    <form method=post action="client_ search.php">
    <input type="text" name="client_la stname" size="25">
    <input type="text" name="client_fi rstname" size="25">
    <input type="hidden" name="searchtyp e" value="1">


    The first and third variables work as expected the middle one returns the error.
  • Jim King

    #2
    Correction! The first variable works as expected. The following lines of code generate this output:
    Notice: Undefined index: client_firstnam e in C:\Program Files\EasyPHP-5.3.3\www\clien t_search.php on line 38
    king
    Notice: Undefined variable: searchtype in C:\Program Files\EasyPHP-5.3.3\www\clien t_search.php on line 44

    $search_type=$_ POST['searchtype'];
    $lastname=$_POS T['client_lastnam e'];
    $firstname=$_PO ST['client_firstna me'];
    @ $db = mysql_pconnect( 'localhost','ro ot','');
    mysql_select_db ('SalisOptical' );

    echo $lastname;
    echo $firstname;
    echo $searchtype;

    It seems simple but I can't figure it out.
    Jim

    Comment

    • Jordy

      #3
      Try closing your <input> fields -> </input>

      And close your form -> </form>

      It should work if im right.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        The problem is that you are trying to use POST values that don't seem to exist. Based on what you posted that shouldn't be happening, but you have only shown us bits and pieces of your code. Show us more of it (the entire for, for example) and we might be able to spot the problem.

        Also, it's always best to test whether POST variables exist before trying to use them. Like so:
        [code=php]
        <?php
        if(isset($_POST['first'], $_POST['second'], $_POST['etc..'])) {
        $first = $_POST['first'];
        $second = $_POST['second'];

        // etc...
        )
        else {
        // Values don't exist. React accordingly.
        }
        ?>[/code]

        Comment

        • Jim King

          #5
          Thanks for the help. The added </input> caused the second variable to work, now it's just the hidden variable.




          First added </input> and second variable worked. Hers is the code from the calling program.
          Then the called program.
          Then the results.

          CALLING PROGRAM ---------------------------

          Code:
          <?-- // Client Name Search--------------------- ;-->
          <tr>
          <td width="19%" align="center"><font color="#0000ff">
          <b> Name Last: First:</b></font></td>
          <td width="38%" align="center">
          <form method=post action="client_search.php">
          <input type="text" name="client_lastname" size="25">
          <input type="text" name="client_firstname" size="25">
          <input type="hidden" name="searchtype" value="1">
          </td>
          <td width="42%">
          <input type="submit" value="Search the Client File" name="B1">
          </td>
          </form>
          
          </input> //----------------------------------------------- JUST ADDED THIS<<<<<<<<<<<<<<<<<
          
          </tr>
          <tr>
          <td width="19%" align="center">
          </td>
          <td width="80%" colspan="2" align="center">
          </td>
          </tr>

          CALLED PROGRAM -------------------------

          Code:
          <?php
          
          $search_type=$_POST['searchtype'];
          $lastname=$_POST['client_lastname'];
          $firstname=$_POST['client_firstname'];
          @ $db = mysql_pconnect('localhost','root','');
          mysql_select_db('SalisOptical');
          
          echo "$lastname <br>";
          echo "$firstname <br>";
          echo $searchtype;
          
          $query = sprintf("SELECT First_Name, Last_Name, Address_1, City, State, Zip FROM customer WHERE First_Name='%s' AND Last_Name='%s'",
              mysql_real_escape_string($firstname),
              mysql_real_escape_string($lastname));
          
          // Perform Query
          $result = mysql_query($query);
          
          if (!$result) {
              $message  = 'Invalid query: ' . mysql_error() . "\n";
              $message .= 'Whole query: ' . $query;
              die($message);
          }
          
          while ($row = mysql_fetch_assoc($result)) {
              echo $row['First_Name'].'  	';
              echo $row['Last_Name'].'<br>';
              echo $row['Address_1'].'<br>';
              echo $row['City'].', ';
              echo $row['State'].' ';
              echo $row['Zip'].'<br>';
          }
          mysql_free_result($result);
          ?>
          RESULT ---------------------------------------

          King
          James



          Notice: Undefined variable: searchtype in C:\Program Files\EasyPHP-5.3.3\www\clien t_search.php on line 44
          JAMES KING
          5151 AIRPORT ROAD
          SALISBURY, MD 21804
          Last edited by Atli; Oct 18 '10, 04:32 PM. Reason: Please use [code] tags when posting code.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Line #16 of the calling code you just posted, Jim, is not good. It's completely out of place. Not sure why it would fix anything. Perhaps triggered some weird error fix in the browser you are using.

            Your HTML is a mess. You need to make sure the tags match up; that you aren't opening and closing them at random (which you seem to do a lot of).

            For example, this is wrong:
            [code=html]<td width="38%" align="center">
            <form method=post action="client_ search.php">
            <input type="text" name="client_la stname" size="25">
            <input type="text" name="client_fi rstname" size="25">
            <input type="hidden" name="searchtyp e" value="1">
            </td>
            <td width="42%">
            <input type="submit" value="Search the Client File" name="B1">
            </td>
            </form>[/code]
            You can see that the </form> closing tag doesn't fit. If you open the <form> inside a <td> it should be close within that same <td>.

            The fact that you use a table to position your input elements complicates this further, because you can not place a form element between the table, tr and td elements without messing up the markup for the table. It would have to surround the whole table. -- This is one reason why using tables for layout is discouraged!

            NOTE that the browsers do their best to fix mistakes like those, so it may *appear* to work fine in one browser, but the markup is still broken. (IE and Opera are particularly "bad" when it comes to this. Firefox is closest to reality, most of the time.) This sort of stuff leads to all sorts of nasty browser incompatibiliti es and future complications. - Correct markup is REQUIRED for proper web development. (The W3C HTML Validator can help with that.)

            Anyways, the only proper fix for the above code, while keeping the ancient table layout method, is to do something like:
            [code=html]<form method="post" action="client_ search.php">
            <table>
            <tr>
            <td width="38%" align="center">
            <input type="text" name="client_la stname" size="25">
            <input type="text" name="client_fi rstname" size="25">
            <input type="hidden" name="searchtyp e" value="1">
            </td>
            <td width="42%">
            <input type="submit" value="Search the Client File" name="B1">
            </td>
            </tr>
            </table>
            </form>[/code]
            Where the <form> and <tr> elements are the once already in your code, not the once I put in there.
            DO NOT just copy/paste that in place of your existing code, or it will get very very messed up.

            I also strongly recommend trying for a CSS based layout instead of your tables. Makes things like this SO much easier.

            Jordy: Try closing your <input> fields -> </input>
            This is only required if you are using XHTML, and given that most sites identify as text/html even though they give the XHTML doctype, this should not affect the functionality of the site.

            In normal HTML, omitting the closing tag of <input> is perfectly fine. In true XHTML sites omitting it would give you a parse error, not a broken form.

            Comment

            • Jim King

              #7
              Alti:
              Thank you so much for the detailed information. I'm a reasonable html programmer and prefer css coding BUT I'm not experienced in php so I grabbed a sample program from a "How to" web site (perhaps "How NOT to" is a better term) and just changed what I needed different. Your help is greatly appreciated.
              Jim

              Comment

              • Jim King

                #8
                OK, I've cleaned it up and used css. The following code creates the 4 variables. The first 2 work and the last 2 don't.

                Code:
                <div id="content">
                
                <h1>Salisbury Optical</h1>
                <h3>Name Last First</h3>
                
                <form method=post action="client_search.php">
                  <input type="text" name="client_lastname" size="35" />
                  <input type="text" name="client_firstname" size="35" />
                  <input type="hidden" name="searchtype" value="1" />
                  <input type="submit" value="Name Search" />
                </form>
                
                </div>
                The following is the code that creates the error messages. The <div> is closed later. The query works and finds clients based on first and last name.

                Code:
                <div id="content">
                
                <?php
                $search_type=$_POST['searchtype'];
                $lastname=$_POST['client_lastname'];
                $firstname=$_POST['client_firstname'];
                @ $db = mysql_pconnect('localhost','root','');
                mysql_select_db('SalisOptical');
                
                echo "$lastname <br />";
                echo "$firstname <br />";
                echo "$searchtype <br />";
                echo "$submit <br />";
                
                
                $query = sprintf("SELECT First_Name, Last_Name, Address_1, City, State, Zip FROM customer WHERE First_Name='%s' AND Last_Name='%s'",
                    mysql_real_escape_string($firstname),
                    mysql_real_escape_string($lastname));
                
                // Perform Query
                $result = mysql_query($query);
                
                // more code below not involved in problem (YET)
                The output of the echo lines is as follows:


                jones
                mary

                Notice: Undefined variable: searchtype in C:\Program Files\EasyPHP-5.3.3\www\clien t_search.php on line 21

                Notice: Undefined variable: submit in C:\Program Files\EasyPHP-5.3.3\www\clien t_search.php on line 22

                ----------
                I can't find an error with the two assignments.

                Thanks, for the help and the kick in the pants to clean up the code. I'm was trying a "quick and dirty" and I know that gets too dirty.
                Jim

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  OK nice. That looks a lot better!

                  Those two notices would be because you misspell $searchtype on line #12. You define $search_type on line #4, which I assume you were planing to use. - And the $submit variable on line #13 isn't defined. At least not in that part of the code. - If you are trying to capture the submit button, that's probably not a great idea. There are situations where submit buttons are not submitted with the form, like if you press the enter button instead of pressing the submit button on certain browsers.

                  Otherwise I see no problems in there. Unless you count that you don't test for the post variables before using them, but then again I'm probably only seeing a part of the code.

                  Comment

                  • Jim King

                    #10
                    Atli:
                    Thank you so much. And your right, I did not test and I will. I think age is catching up here. Even being a PHP beginner, I'm not a beginning programmer, so I should pay attention. Again, thanks.
                    Jim

                    Comment

                    Working...