Undefined index variable errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • machachar
    New Member
    • Mar 2012
    • 2

    Undefined index variable errors

    Dear
    I am beginner of PHP programming I tried my best to run below given code but failed. Please solve it.

    Code:
    <html>
    <head><title>Form</title></head>
    <body>
    <h1>Web Developer Required</h1>
    <h4>Please fill the following form to apply for this job</h4>
    <form method= "post" action = "application.php">
    <table width "100%" border="1">
    <tr><td>Name: </td><td><input type="text" name= "name" /></td></tr>
    <tr><td>Phone: </td><td><input type="text" name= "phone" /></td></tr>
    <tr><td>Email: </td><td><input type="text" name= "email" /></td></tr>
    <tr><td>Address: </td><td><input type="text" name= "address" /></td></tr>
    <tr><td>Gender: </td><td><input type="text" name= "gender" type="radio" value= "male" />
    Female:<input name="gender" type= "radio" value="female" /></td></tr>
    <tr><td>City: </td><td><select name ="city">
    <option>Islamabad</option>
    <option>Karachi</option>
    <option>Lahore</option>
    <option>Quetta</option>
    </select></td></tr><tr>
    <tr><td>Age: </td><td><input ="text" name="age" /></td></tr>
    <tr><td=width="40%" valign="top"> Skills </td><td>
    PHP: <input type= "checkbox"name="skills[]" value "PHP"><br>
    HTML: <input type= "checkbox"name="skills[]" value "HTML>"<br>
    Photoshop: <input type= "checkbox"name="skills[]" value "Photoshop>"<br>
    Javascript: <input type= "checkbox"name="skills[]" value "Javascript />"<br/>
    Ajax: <input type= "checkbox"name="skills[]" value "Ajax"/> <br/>
    MySQL: <input type= "checkbox"name="skills[]" value "MySQL"  />
    </td></tr><tr><td valigen="top">&nbsp;</td>
    <td><input type="submit" name= "submit" value= "Submit" />
    <input type="reset" name= "submit2" value= "Reset" /></td>
    </tr></table></bpdy></html>
    <?php
    function cleanStr($str) {
    $cStr = trim($str);
    $cStr = htmlspecialchars($str);
    $cStr = addslashes($str);
    return $cStr;
    }
    
    function emptyFields($ar){
    if(!is_array($ar)) {
    print "The argument must be an array";
    return false;
    }
    foreach($ar as $key => $value) {
    $value = celanStr($value);
    if(empty($value)) {
    print "<b>" . ucwords($key) . "</b>
    
    must not be empty. Please Click browser
    back button and correct it.";
    return false;
    }
    }
    return true;
    }
    
    if(!emptyFields($_POST)) {
    exit();
    
    }
    
    //check required skills and age
    
    function isEligible($skills, $age){
    $age = intval($age);
    
    if(count($skills) == 0){
    print ("You dont have any skills");
    return false;
    
    }
    
    
    if($age < 23 || $age > 35) {
    print("Your age must be between 23 and 35");
    return false;
    }
    return true;
    }
    // assigning fields value to variable
    
    $name= cleanStr($_POST['name']);
    $phone= cleanStr($_POST['phone']);
    $email= cleanStr($_POST['email']);
    $address= cleanStr($_POST['address']);
    $gender= $_POST['gender'];
    
    $city = $_POST['city'];
    $age =   (int)cleanStr($_POST['age']);
    $skills = $_POST['skills'];
    
    //call func candidate is eligible
    
    if (!isEligible($skills,$age)) {
    exit();
    }
    // if every thing is ok
    
    print "<h4>Thanks your ". $name.
    "for your interest in this job. We will contact you soon. </h4>";
    print "We received the following information about you. <br>";
    
    //printing user submitted information
    
    print "Name: " . $name . "<br>";
    print "Phone: " . $phone . "<br>";
    print "Email: " . $email . "<br>";
    print "Address: " . $address . "<br>";
    print "Gender: " . $gender . "<br>";
    print "City: " . $city . "<br>";
    print "Age: " . $age . "<br>";
    print "Your Skills :" ;
    foreach($skills as $value)
     {
    print $value . ",";
    }
    ?>

    Regards,

    Mumtaz Ali
    Last edited by Rabbit; Mar 22 '12, 03:28 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    You need to give us the full error text along with which line of code the error is on.

    Comment

    • machachar
      New Member
      • Mar 2012
      • 2

      #3
      Code:
      <html>
      <head><title>Form</title></head>
      <body>
      <h1>Web Developer Required</h1>
      <h4>Please fill the following form to apply for this job</h4>
      <form method= "post" action = "application.php">
      <table width "100%" border="1">
      <tr><td>Name: </td><td><input type="text" name= "name" /></td></tr>
      <tr><td>Phone: </td><td><input type="text" name= "phone" /></td></tr>
      <tr><td>Email: </td><td><input type="text" name= "email" /></td></tr>
      <tr><td>Address: </td><td><input type="text" name= "address" /></td></tr>
      <tr><td>Gender: </td><td><input type="text" name= "gender" type="radio" value= "male" />
      Female:<input name="gender" type= "radio" value="female" /></td></tr>
      <tr><td>City: </td><td><select name ="city">
      <option>Islamabad</option>
      <option>Karachi</option>
      <option>Lahore</option>
      <option>Quetta</option>
      </select></td></tr><tr>
      <tr><td>Age: </td><td><input ="text" name="age" /></td></tr>
      <tr><td=width="40%" valign="top"> Skills </td><td>
      PHP: <input type= "checkbox"name="skills[]" value "PHP"><br>
      HTML: <input type= "checkbox"name="skills[]" value "HTML>"<br>
      Photoshop: <input type= "checkbox"name="skills[]" value "Photoshop>"<br>
      Javascript: <input type= "checkbox"name="skills[]" value "Javascript />"<br/>
      Ajax: <input type= "checkbox"name="skills[]" value "Ajax"/> <br/>
      MySQL: <input type= "checkbox"name="skills[]" value "MySQL"  />
      </td></tr><tr><td valigen="top">&nbsp;</td>
      <td><input type="submit" name= "submit" value= "Submit" />
      <input type="reset" name= "submit2" value= "Reset" /></td>
      </tr></table></bpdy></html>
      <?php
      function cleanStr($str) {
      $cStr = trim($str);
      $cStr = htmlspecialchars($cStr);
      $cStr = addslashes($cStr);
      return $cStr;
      }
      
      function emptyFields($ar){
      if(!is_array($ar)) {
      print "The argument must be an array";
      return false;
      }
      foreach($ar as $key => $value) {
      $value = cleanStr($value);
      if(empty($value)) {
      print "<b>" . ucwords($key) . "</b>
      
      must not be empty. Please Click browser
      back button and correct it.";
      return false;
      }
      }
      return true;
      }
      
      if(!emptyFields($_POST)) {
      exit();
      
      }
      
      //check required skills and age
      
      function isEligible($skills, $age){
      $age = intval($age);
      
      if(count($skills) == 0){
      print ("You dont have any skills");
      return false;
      
      }
      
      
      if($age < 23 || $age > 35) {
      print("Your age must be between 23 and 35");
      return false;
      }
      return true;
      }
      // assigning fields value to variable
      if (isset($_POST[''])==true) {
      $name= cleanStr($_POST['name']);
      $phone= cleanStr($_POST['phone']);
      $email= cleanStr($_POST['email']);
      $address= cleanStr($_POST['address']);
      $gender= $_POST['gender'];
      
      $city = $_POST['city'];
      $age =   (int)cleanStr($_POST['age']);
      $skills = $_POST['skills'];
      }
      //call func candidate is eligible
      if (isset($_POST[''])==true) {
      if (!isEligible($skills,$age)) {
      exit();
      }
      }
      // if every thing is ok
      if (isset($_POST[''])==true) {
      print "<h4>Thanks your ". $name.
      "for your interest in this job. We will contact you soon. </h4>";
      print "We received the following information about you. <br>";
      }
      //printing user submitted information
      if (isset($_POST[''])==true) {
      print "Name: " . $name . "<br>";
      print "Phone: " . $phone . "<br>";
      print "Email: " . $email . "<br>";
      print "Address: " . $address . "<br>";
      print "Gender: " . $gender . "<br>";
      print "City: " . $city . "<br>";
      print "Age: " . $age . "<br>";
      print "Your Skills :" ;
      
      foreach($skills as $value)
       {
      print $value . ",";
      }
      }
      ?>
      I tried and removed errors of undefined errors but two errors i couldn't remove it.
      Warning: trim() expects parameter 1 to be string, array given in C:\xampp\htdocs \application.ph p on line 34
      Skills must not be empty. Please Click browser back button and correct i

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Warning: trim() expects parameter 1 to be string, array given in C:\xampp\htdocs \application.ph p on line 34
        not sure what is ambigous about this warning. it tells you that you pass an array where you should pass a string.

        reason being, you declare Skills to be an input array, so the foreach() loop's $value will be an array (and not a string like the others) for the skills key.

        the second error is caused by the first error. just fix that one and it should go away.

        Comment

        Working...