Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    !is_numeric($value) isn't working properly

    PHP Code:
    if ($_SESSION['check'] == 1) {
        
    $validate = array($_POST[$Category] , $_POST[$Subcategory] , $_POST[$ProductNumber] , $_POST[$ProductName] , $_POST[$Manufacturers] , $_POST[$Descriptions] , $_POST[Instock] , $_POST[$Quantity] , $_POST[$OurCost] , $_POST[$SellPrice] , $_POST[$TaxPrice] , $_POST[$Dementions] , $_POST[$Weight] , $_POST[$WeightCheck]); 
        
    $radio = array($_POST[$Instock] , $_POST[$WeightCheck]);
        
    $numbers = array($_POST[$ProductNumber] , $_POST[$Quanity] , $_POST[$OurCost] , $_POST[$SellPrice] , $_POST[$TaxPrice] , $_POST[$Dementions] , $_POST[$Weight]);
        
        
    $errors='';
        
        foreach(
    $_POST as $var=>$value){
            echo 
    strlen($value).'<br>';
               if (
    strlen($value)<0){ 
                  if ((
    in_array($var$numbers)) && (!is_numeric($value))){ 
                      
    $errors .= 'Field ' $var ' is required to be filled out in numberic form.<br>';   
                   } else { 
                      
    $errors .= 'Field ' $var .' is required to be filled out.<br>';  
                   } 
            }
        } 


    The problem i think is with this if ((in_array($var, $numbers)) && (!is_numeric($value))){ I'm typing in a nonnumeric into the field that requires to be numeric but it just prints out Field ..... is required to be filled out. This doesn't make any sence because I tried some error trapping and debugging and printed out the string lenght and the array values and they match up perfectly so I'm not too sure why this is not working... Any help will be greatly appreciated :-)

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    without seeing the rest of your code its a guess but

    =array( $_POST[$Category] , $_POST[$Subcategory] , etc

    should most likely be

    =array( $_POST['Category'] , $_POST['Subcategory'] , etc

    etc.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    without seeing the rest of your code its a guess but

    =array( $_POST[$Category] , $_POST[$Subcategory] , etc

    should most likely be

    =array( $_POST['Category'] , $_POST['Subcategory'] , etc

    etc.
    PHP Code:
    <?PHP session_start(); ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Add product to Database</title>
    <?php



    if ($_SESSION['check'] == 1) {
        
    $validate = array($_POST['Category'] , $_POST['Subcategory'] , $_POST['ProductNumber'] , $_POST['ProductName'] , $_POST['Manufacturers'] , $_POST['Descriptions'] , $_POST['Instock'] , $_POST['Quantity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight'] , $_POST['WeightCheck']); 
        
    $radio = array($_POST['Instock'] , $_POST['WeightCheck']);
        
    $numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']);
        
        
    $errors='';
        
        foreach(
    $_POST as $var=>$value){
            echo 
    strlen($value).'<br>';
            
               if (
    strlen($value)==0){
                  if ((
    in_array($var$numbers)) && (!is_numeric($value))){ 
                      
    $errors .= 'Field ' $var ' is required to be filled out in numberic form.<br>';   
                   } else { 
                      
    $errors .= 'Field ' $var .' is required to be filled out.<br>';  
                   } 
            }
        } 

    }
        
    include 
    'dbconnect-include.php';

    $TableName maxforcepc;

    $link mysql_connect ($Host$User$Password);  
    mysql_select_db($DBName$link);

    $add "INSERT INTO products (Cat_ID, Sub_Cat_ID, Product_Number, Product, Man_ID, Descriptions, In_Stock, Show_Quanity, Quanity, Cost, Sell_Price, Taxable, Tax_Price, Dementions, Weight) VALUES (" $_POST['Category'] . ", " $_POST['Subcategory'] . ", " $_POST['Subcategory'] . ", " $_POST['ProductNumber'] . ", " $_POST['ProductName'] . ", " $_POST['Manufacturers'] . ", " $_POST['Descriptions'] . ", " $_POST['InStockCheck'] . ", " $_POST['ShowQCheck'] . ", " $_POST['Quanity'] . ", " $_POST['OurCost'] . ", " $_POST['SellPrice'] . ", " $_POST['TaxableCheck'] . ", " $_POST['TaxPrice'] . ", " $_POST['Dementions'] . ", " $_POST['Weight'] . ")";
    $add_to_db mysql_query ($add);

    function 
    run_query($col$table) {
        
    $query "SELECT $col FROM $table"
        
        
    $Result mysql_query ($query) or die(mysql_error());
        
        if (
    $Result) {
            echo 
    "<select name='$col' id='$col'><option></option>";
            while (
    $Row mysql_fetch_array($Result)) {    echo "<option>".$Row[$col]."</option>"; }
        } else {
            echo 
    "The query could not be executed!";
        }
        
        
    mysql_close ($link);
        print (
    "</select>");
    }
    ?> 
    <style type="text/css">
    <!--
    .Titles {
        margin:0 0 0 5px;
        padding:0 0 0 5px;
    }
    .txtfieldmrgbtm {
        margin-bottom:2px;
        padding-bottom:2px;
        border-bottom:1px solid black;
    }
    -->
    </style></head>

    <body>
    <form name="form1" method="post" action="<? $_SESSION['check'] = 1$_SERVER['PHP_SELF']; ?>">
      <table width="427" border="0" align="center" cellpadding="0" cellspacing="0" style="border:2px solid black;">
        <tr>
          <td colspan="3" class="Titles txtfieldmrgbtm">
              <b>Titles in <font color="#FF0000">RED</font> must be filled out completely!<br>
              </b>
              <? 
                
    echo $errors;
            
    ?>
          </td>
        </tr>
        <tr bgcolor="#FFFFCC">
          <td colspan="3" style="border-bottom:1px dashed black"><div align="center"><b>Information</b></div></td>
        </tr>
        <tr>
          <td colspan="3" bgcolor="#F0F9FF"><table width="100%" border="0" cellspacing="0" cellpadding="0" class="txtfieldmrgdtm">
            <tr>
              <td class="Titles txtfieldmrgbtm" id="1"><b><font color="#FF0000">Category:</font></b></td>
              <td width="298" class="txtfieldmrgbtm"><? run_query(Categorycategories); ?></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Subcategory:</font></b></td>
              <td class="txtfieldmrgbtm"><? run_query(Subcategorysubcategories); ?></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Product Number:</font></b></td>
              <td class="txtfieldmrgbtm"><input name="ProductNumber" type="text" id="ProductNumber"></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Product Name:</font></b></td>
              <td class="txtfieldmrgbtm"><input name="ProductName" type="text" id="ProductName"></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><p><b><font color="#FF0000">Manufacturers:</font></b></p>
                </td>
              <td class="txtfieldmrgbtm"><? run_query(Manufacturermanufacturers); ?></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Descriptions:</font></b></td>
              <td class="txtfieldmrgbtm"><textarea name="Description" cols="30" rows="3" id="Description"></textarea></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Instock:</font></b></td>
              <td class="txtfieldmrgbtm"><input name="Instock" type="radio" value="1">
                <strong>Yes</strong>
                <input name="Instock" type="radio" value="0">            <strong>No</strong></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><b><font color="#FF0000">Quanity:</font></b></td>
              <td class="txtfieldmrgbtm"><input name="Quanity" type="text" id="Quanity"></td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td colspan="3" bgcolor="#FFFFCC" style="border-bottom:1px dashed black;"><div align="center"><b>Cost Settings</b></div></td>
        </tr>
        <tr>
          <td colspan="3" bgcolor="#F0F9FF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Our Cost:</b></font></td>
              <td width="298" class="txtfieldmrgbtm"><input name="OurCost" type="text" id="OurCost"></td>
            </tr>
            <tr>
              <td class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Sell Price:</b></font></td>
              <td class="txtfieldmrgbtm"><input name="SellPrice" type="text" id="SellPrice"></td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td colspan="3" bgcolor="#FFFFCC" style="border-bottom:1px dashed black;"><div align="center"><b>Shipping Settings</b></div></td>
        </tr>
        <tr>
          <td colspan="3" bgcolor="#F0F9FF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Dementions:</b></font></td>
              <td width="299" class="txtfieldmrgbtm"><input name="Dementions1" type="text" id="Dementions1" size="5"> 
                X 
                <input name="Dementions2" type="text" id="Dementions2" size="5"> 
                X 
                <input name="Dementions3" type="text" id="Dementions3" size="5"></td>
            </tr>
            <tr>
              <td width="124" class="Titles txtfieldmrgbtm"><font color="#FF0000"><b>Weight:</b></font></td>
              <td class="txtfieldmrgbtm"><input name="Weight" type="text" id="Weight" size="10">
                <input name="weightcheck" type="radio" value="g">
                <strong>g 
                <strong><strong><strong>
                <strong><strong><strong><strong>
                <input name="WeightCheck" type="radio" value="oz">
                </strong></strong></strong></strong> </strong></strong> </strong>oz 
    <input name="WeightCheck" type="radio" value="lb"> 
    lb
    </strong></td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td width="50%" bgcolor="#FFFFCC">&nbsp;</td>
          <td width="50%" bgcolor="#FFFFCC">&nbsp;</td>
          <td bgcolor="#FFFFCC"><div align="right">
            <input type="submit" name="Submit" value="Add">
          </div></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    There is the rest of my code, all the php codeing is in the php color code so it will be fairly easy to pick out the php coding from the regular html. The coding makes sence to me just doesn't make sence on why it's not working the way I need it to. I did some print outs of the string lenght and what not, just that It's not working correctly... Any help would be greatly appreciated :-)
    Last edited by DiarYofaMadmaN; 05-28-2004 at 02:06 AM.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This first chack
    PHP Code:
     $numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']); 
        ...
    if ((
    in_array($var$numbers)) && (!is_numeric($value))){ 
    will never evauate to True. Your array should contain the variablenames like in your form. so
    $numbers = array('ProductNumber' , 'Quanity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight');
    But they need the formfields need to be exactly called like that.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    This first chack
    PHP Code:
     $numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']); 
        ...
    if ((
    in_array($var$numbers)) && (!is_numeric($value))){ 
    will never evauate to True. Your array should contain the variablenames like in your form. so
    $numbers = array('ProductNumber' , 'Quanity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight');
    But they need the formfields need to be exactly called like that.
    Hrmmm, I had that when I first started to code all this but it was doing the same thing, maybe I just gotten confused from changing the coding around trying different things... I'll try again and see what happends...

    The problem that I'm getting is that it's not finding if it's not numeric because I can type in a non numeric character and it will not pick it up and echo me the correct error message.
    Last edited by DiarYofaMadmaN; 05-28-2004 at 02:44 AM.

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is also a logic error in your code, because if there is a value filled in, the
    if (strlen($value)==0)
    will return False, so the checks will never be made.

    Maybe use the code i posted in one of your other threads
    having problems with quotes...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down

    Quote Originally Posted by raf
    There is also a logic error in your code, because if there is a value filled in, the
    if (strlen($value)==0)
    will return False, so the checks will never be made.

    Maybe use the code i posted in one of your other threads
    having problems with quotes...
    Sorry about responding late, I went to PA with my family for a family outing kinda thing :: hehe. Anyways, I really appreciate your help. PHP is all new to me right now. I am picking up sorta quickly I just need to remember lots things. However, I've been picking up what the coding does quickly by just looking at it. Just something doesn't make much sence. I need to check the form even if it is blank... So, I've changed this: if (strlen($value)>=1) to if ((strlen($value)>=1) || (strlen($value)==0))

    PHP Code:
    if ($_SESSION['check'] == 1) {
        
    $validate = array($_POST['Category'] , $_POST['Subcategory'] , $_POST['ProductNumber'] , $_POST['ProductName'] , $_POST['Manufacturers'] , $_POST['Descriptions'] , $_POST['Instock'] , $_POST['Quantity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight'] , $_POST['WeightCheck']); 
        
    $radio = array($_POST['Instock'] , $_POST['WeightCheck']);
        
    $numbers = array($_POST['ProductNumber'] , $_POST['Quanity'] , $_POST['OurCost'] , $_POST['SellPrice'] , $_POST['TaxPrice'] , $_POST['Dementions'] , $_POST['Weight']);
        
        
    $errors='';
        
        foreach(
    $_POST as $var=>$value){
            echo 
    strlen($value).'<br>';
            
               if ((
    strlen($value)>=1) || (strlen($value)==0)){ 
                  if ((
    in_array($var$numbers)) && (!is_numeric($value))){ 
                      
    $errors .= 'Field ' $var ' is required to be filled out in numberic form.<br>';
                   } else { 
                      
    $errors .= 'Field ' $var .' is required to be filled out.<br>';  
                   } 
            }
        } 


    The problem I have is I keep getting this executed: $errors .= 'Field ' . $var .' is required to be illed out.<br>'; and I should be getting $errors .= 'Field ' . $var . ' is required to be filled out in numberic form.<br>'; executed on the numeric fields... There are many sections of this project for myself that I'm doing that I've coded and finished by myself, just simple stuff like this cause me problems.
    Last edited by DiarYofaMadmaN; 06-02-2004 at 05:28 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •