Object Expected...how do i fix it?

Discussion in 'PHP' started by lost, Nov 17, 2005.

  1. #1
    i'm trying to retrieve the contents of a textfield, parse it into 2 fields and then insert into the database...again syntax errors.
    this is what i have:

    
    
    <?php
         if($rows = @mysql_fetch_array($result))
         {
            echo "<BR><BR><BR><FONT SIZE=2>Found the following results for <B>'";
            echo $_POST['searchfield'];
            echo "'</B> in <B>'$stype'</B> field:<BR><BR>";
    
    	    echo '<TABLE BORDER="1" CELLPADDING="4" CELLSPACING="2">';
    	    echo '<TR><TD><B><FONT SIZE=2>Last Name</B></TD><TD><B><FONT SIZE=2>First Name</B></TD><TD><B><FONT SIZE=2>User Id</B></TD><TD><B><FONT SIZE=2>Privileges</B></TD></TR>';
    
    	    extract($rows);
            echo "<TR><TD><FONT SIZE=2>$lastname</TD><TD><FONT SIZE=2>$firstname</TD><TD><FONT SIZE=2>$userid</TD><TD><FONT SIZE=2>$privileges</TD></TR>";
    
        	while($rows = @mysql_fetch_array($result))
            {
        	   extract($rows);
               echo "<TR><TD><FONT SIZE=2>$lastname</TD><TD><FONT SIZE=2>$firstname</TD><TD><FONT SIZE=2>$userid</TD><TD><FONT SIZE=2>$privileges</TD></TR>";
        	}
        	echo '</TABLE>'; 
            mysql_free_result($result);           
         }
         else
         {
            echo "<BR><BR><FONT SIZE=2>No entries found for <B>'";
    	    echo $_POST['searchfield'];
            echo "'</B> in <B>'$stype'</B> field.<BR>";
            echo "<FONT SIZE=2>Try using '*' for wildcard searching.<BR><BR>";
            echo "<B><FONT SIZE=2>OR<BR><BR></B>";
            echo "<FONT SIZE=2>Add New User  ";
            echo '<INPUT TYPE="text" ID="newuser" NAME="newname" VALUE="" SIZE="30" STYLE="display:none;"/>';
            echo '<BR><BR><INPUT TYPE=button NAME="submitnewuser" VALUE=" Add " ONCLICK="addNewUser();">';
         }
    ?>
    <SCRIPT LANGUAGE="Javascript" TYPE="text/javascript">
       document.getElementById('newuser').style.display='';
       document.getElementById('newuser').focus();    
    </SCRIPT>
    
    <?php     
         include('./searchgoback.inc'); 
         mysql_close();
      }
      else
      {
         include('./searchusers.inc');
      }
    
    ?>
    <SCRIPT LANGUAGE="Javascript" TYPE="text/javascript">
      function addNewUser()
      {    
          var $firstname;
          var $lastname;
          $newUser = document.getElementById('newuser').value;
          <?php $str = sscanf($newUser, "%s %s", $firstname, $lastname); ?>
          <?php $query = "INSERT INTO awacsuser(firstname, lastname) VALUES ('$firstname', '$lastname')"; ?>
      }
    
    </SCRIPT>
    
    
    
    Code (markup):
    i'm getting
    Undefined variable: newUser in this line:
    $newUser = document.getElementById('newuser').value;


    and object expected on the addNewUser() call.
     
    lost, Nov 17, 2005 IP
  2. luvkycool

    luvkycool Guest

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $newUser = document.getElementById('newuser').value;

    you doing some javascript and php.
    while its fine for me

    getelementbyid is used for an id=
    you need document.myformname.newuser.value

    but
    this normally(use javascript) is done before the form is processed to see if the values are filled for better user experience.

    but use php to process and also validate it (again)
    $username=$_POST['newuser'];

    goodluck

    you n
     
    luvkycool, Nov 17, 2005 IP
  3. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can't mix javascript with PHP.........
     
    mnemtsas, Nov 17, 2005 IP