By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

Undefined index variable errors

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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Form</title></head>
  3. <body>
  4. <h1>Web Developer Required</h1>
  5. <h4>Please fill the following form to apply for this job</h4>
  6. <form method= "post" action = "application.php">
  7. <table width "100%" border="1">
  8. <tr><td>Name: </td><td><input type="text" name= "name" /></td></tr>
  9. <tr><td>Phone: </td><td><input type="text" name= "phone" /></td></tr>
  10. <tr><td>Email: </td><td><input type="text" name= "email" /></td></tr>
  11. <tr><td>Address: </td><td><input type="text" name= "address" /></td></tr>
  12. <tr><td>Gender: </td><td><input type="text" name= "gender" type="radio" value= "male" />
  13. Female:<input name="gender" type= "radio" value="female" /></td></tr>
  14. <tr><td>City: </td><td><select name ="city">
  15. <option>Islamabad</option>
  16. <option>Karachi</option>
  17. <option>Lahore</option>
  18. <option>Quetta</option>
  19. </select></td></tr><tr>
  20. <tr><td>Age: </td><td><input ="text" name="age" /></td></tr>
  21. <tr><td=width="40%" valign="top"> Skills </td><td>
  22. PHP: <input type= "checkbox"name="skills[]" value "PHP"><br>
  23. HTML: <input type= "checkbox"name="skills[]" value "HTML>"<br>
  24. Photoshop: <input type= "checkbox"name="skills[]" value "Photoshop>"<br>
  25. javascript: <input type= "checkbox"name="skills[]" value "Javascript />"<br/>
  26. Ajax: <input type= "checkbox"name="skills[]" value "Ajax"/> <br/>
  27. MySQL: <input type= "checkbox"name="skills[]" value "MySQL"  />
  28. </td></tr><tr><td valigen="top">&nbsp;</td>
  29. <td><input type="submit" name= "submit" value= "Submit" />
  30. <input type="reset" name= "submit2" value= "Reset" /></td>
  31. </tr></table></bpdy></html>
  32. <?php
  33. function cleanStr($str) {
  34. $cStr = trim($str);
  35. $cStr = htmlspecialchars($str);
  36. $cStr = addslashes($str);
  37. return $cStr;
  38. }
  39.  
  40. function emptyFields($ar){
  41. if(!is_array($ar)) {
  42. print "The argument must be an array";
  43. return false;
  44. }
  45. foreach($ar as $key => $value) {
  46. $value = celanStr($value);
  47. if(empty($value)) {
  48. print "<b>" . ucwords($key) . "</b>
  49.  
  50. must not be empty. Please Click browser
  51. back button and correct it.";
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57.  
  58. if(!emptyFields($_POST)) {
  59. exit();
  60.  
  61. }
  62.  
  63. //check required skills and age
  64.  
  65. function isEligible($skills, $age){
  66. $age = intval($age);
  67.  
  68. if(count($skills) == 0){
  69. print ("You dont have any skills");
  70. return false;
  71.  
  72. }
  73.  
  74.  
  75. if($age < 23 || $age > 35) {
  76. print("Your age must be between 23 and 35");
  77. return false;
  78. }
  79. return true;
  80. }
  81. // assigning fields value to variable
  82.  
  83. $name= cleanStr($_POST['name']);
  84. $phone= cleanStr($_POST['phone']);
  85. $email= cleanStr($_POST['email']);
  86. $address= cleanStr($_POST['address']);
  87. $gender= $_POST['gender'];
  88.  
  89. $city = $_POST['city'];
  90. $age =   (int)cleanStr($_POST['age']);
  91. $skills = $_POST['skills'];
  92.  
  93. //call func candidate is eligible
  94.  
  95. if (!isEligible($skills,$age)) {
  96. exit();
  97. }
  98. // if every thing is ok
  99.  
  100. print "<h4>Thanks your ". $name.
  101. "for your interest in this job. We will contact you soon. </h4>";
  102. print "We received the following information about you. <br>";
  103.  
  104. //printing user submitted information
  105.  
  106. print "Name: " . $name . "<br>";
  107. print "Phone: " . $phone . "<br>";
  108. print "Email: " . $email . "<br>";
  109. print "Address: " . $address . "<br>";
  110. print "Gender: " . $gender . "<br>";
  111. print "City: " . $city . "<br>";
  112. print "Age: " . $age . "<br>";
  113. print "Your Skills :" ;
  114. foreach($skills as $value)
  115.  {
  116. print $value . ",";
  117. }
  118. ?>

Regards,

Mumtaz Ali
Mar 22 '12 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,365
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.
Mar 22 '12 #2

P: 2
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Form</title></head>
  3. <body>
  4. <h1>Web Developer Required</h1>
  5. <h4>Please fill the following form to apply for this job</h4>
  6. <form method= "post" action = "application.php">
  7. <table width "100%" border="1">
  8. <tr><td>Name: </td><td><input type="text" name= "name" /></td></tr>
  9. <tr><td>Phone: </td><td><input type="text" name= "phone" /></td></tr>
  10. <tr><td>Email: </td><td><input type="text" name= "email" /></td></tr>
  11. <tr><td>Address: </td><td><input type="text" name= "address" /></td></tr>
  12. <tr><td>Gender: </td><td><input type="text" name= "gender" type="radio" value= "male" />
  13. Female:<input name="gender" type= "radio" value="female" /></td></tr>
  14. <tr><td>City: </td><td><select name ="city">
  15. <option>Islamabad</option>
  16. <option>Karachi</option>
  17. <option>Lahore</option>
  18. <option>Quetta</option>
  19. </select></td></tr><tr>
  20. <tr><td>Age: </td><td><input ="text" name="age" /></td></tr>
  21. <tr><td=width="40%" valign="top"> Skills </td><td>
  22. PHP: <input type= "checkbox"name="skills[]" value "PHP"><br>
  23. HTML: <input type= "checkbox"name="skills[]" value "HTML>"<br>
  24. Photoshop: <input type= "checkbox"name="skills[]" value "Photoshop>"<br>
  25. javascript: <input type= "checkbox"name="skills[]" value "Javascript />"<br/>
  26. Ajax: <input type= "checkbox"name="skills[]" value "Ajax"/> <br/>
  27. MySQL: <input type= "checkbox"name="skills[]" value "MySQL"  />
  28. </td></tr><tr><td valigen="top">&nbsp;</td>
  29. <td><input type="submit" name= "submit" value= "Submit" />
  30. <input type="reset" name= "submit2" value= "Reset" /></td>
  31. </tr></table></bpdy></html>
  32. <?php
  33. function cleanStr($str) {
  34. $cStr = trim($str);
  35. $cStr = htmlspecialchars($cStr);
  36. $cStr = addslashes($cStr);
  37. return $cStr;
  38. }
  39.  
  40. function emptyFields($ar){
  41. if(!is_array($ar)) {
  42. print "The argument must be an array";
  43. return false;
  44. }
  45. foreach($ar as $key => $value) {
  46. $value = cleanStr($value);
  47. if(empty($value)) {
  48. print "<b>" . ucwords($key) . "</b>
  49.  
  50. must not be empty. Please Click browser
  51. back button and correct it.";
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57.  
  58. if(!emptyFields($_POST)) {
  59. exit();
  60.  
  61. }
  62.  
  63. //check required skills and age
  64.  
  65. function isEligible($skills, $age){
  66. $age = intval($age);
  67.  
  68. if(count($skills) == 0){
  69. print ("You dont have any skills");
  70. return false;
  71.  
  72. }
  73.  
  74.  
  75. if($age < 23 || $age > 35) {
  76. print("Your age must be between 23 and 35");
  77. return false;
  78. }
  79. return true;
  80. }
  81. // assigning fields value to variable
  82. if (isset($_POST[''])==true) {
  83. $name= cleanStr($_POST['name']);
  84. $phone= cleanStr($_POST['phone']);
  85. $email= cleanStr($_POST['email']);
  86. $address= cleanStr($_POST['address']);
  87. $gender= $_POST['gender'];
  88.  
  89. $city = $_POST['city'];
  90. $age =   (int)cleanStr($_POST['age']);
  91. $skills = $_POST['skills'];
  92. }
  93. //call func candidate is eligible
  94. if (isset($_POST[''])==true) {
  95. if (!isEligible($skills,$age)) {
  96. exit();
  97. }
  98. }
  99. // if every thing is ok
  100. if (isset($_POST[''])==true) {
  101. print "<h4>Thanks your ". $name.
  102. "for your interest in this job. We will contact you soon. </h4>";
  103. print "We received the following information about you. <br>";
  104. }
  105. //printing user submitted information
  106. if (isset($_POST[''])==true) {
  107. print "Name: " . $name . "<br>";
  108. print "Phone: " . $phone . "<br>";
  109. print "Email: " . $email . "<br>";
  110. print "Address: " . $address . "<br>";
  111. print "Gender: " . $gender . "<br>";
  112. print "City: " . $city . "<br>";
  113. print "Age: " . $age . "<br>";
  114. print "Your Skills :" ;
  115.  
  116. foreach($skills as $value)
  117.  {
  118. print $value . ",";
  119. }
  120. }
  121. ?>
  122.  
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.php on line 34
Skills must not be empty. Please Click browser back button and correct i
Mar 23 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
Warning: trim() expects parameter 1 to be string, array given in C:\xampp\htdocs\application.php 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.
Mar 23 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.