php form validation problem

Discussion in 'PHP' started by azeem12, Feb 24, 2010.

  1. #1
    i m stucked in form validation
    i am trying to make content management system so which i have 2 subjects products services without form validation when i add new subject my code work i want to validate the form like what if user send long string as an menu name or what if user didn't enter the name but the send the position of new subject so i want to validate these things so i wrote these code it compiles correct and doesn't give me error when i add some subject it doesn't respond anything it doesn't add anything i write data on menu name select position number then click on Yes radio button to make it visible and hit submit button it doesn't do anything not add any thing or doesn't redirect back to content.php it doesnt work :(help me out please
    
    include("includes/connection.php");
    include("includes/functions.php");
    // form validation
    
    $errors=array();
    $required_feilds=array('menu_name','postion','visible');
    foreach($required_feilds as $feildname){
       if(!isset($_POST[$feildname])|| empty($_POST[$feildname])){
       $errors[]=$feildname;
       }
    }
    $feild_with_lengths=array('menu_name'=>30);
    foreach($feild_with_lenghts as $feildname=>$maxlenght){
       if(strlen(trim(mysql_prep($_POST[$feildname])))>$maxlenght){
          $errors[]=$feildname;}
    }
    if(!empty($errors)){
    redirect("new_subject.php");
    }
    //$_POST = array_map('mysql_real_escape_string', $_POST);
    $menu_name= mysql_prep($_POST['menu_name']);
    $position = mysql_prep($_POST['position']);
    $visible = mysql_prep($_POST['visible']);
    
    $query = "INSERT INTO subjects (
    menu_name, position, visible)
    Values ( '{$menu_name}',{$position},{$visible})";
       if(mysql_query($query,$connection)){
          
          header("Location:content.php");
          exit;
                   
       }else {
          echo "<p> Subject creation is failed .</p>";
          echo "<p>".mysql_error()."</p>";
       }
    mysql_close($connection);
    
    PHP:

     
    azeem12, Feb 24, 2010 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    where's your form?
     
    javaongsan, Feb 24, 2010 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    post your errors surely there will have some notices from mysql..
    and your form
     
    bartolay13, Feb 25, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    If you have your fields within an array to validate each element you could use array_filter()
     
    danx10, Feb 25, 2010 IP
  5. azeem12

    azeem12 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks for all the suggestion the problem is solved
    error was in the array
    $required_feilds=array('menu_name','postion','visible');
    i wrote wrong spelling postion
    it should position nw it works fine thanks alot for every one
     
    azeem12, Feb 25, 2010 IP