473,413 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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.

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
3 2253
Rabbit
12,516 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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

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

Similar topics

1
by: lawrence | last post by:
I just switched error_reporting to ALL so I could debug my site. I got a huge page full of errors. One of the most common was that in my arrays I'm using undefined offsets and indexes. These still...
5
by: news.bigpond.com | last post by:
getting errors Notice: Undefined index: name in F:\uni\Software engineering\assignment4\guestbook.php on line 6 the variable $name is declared as $name = _POST; What could be causing this?...
4
by: John Oliver | last post by:
PHP Notice: Undefined index: name in /home/www/reformcagunlaws.com/new.php on line 6 PHP Notice: Undefined index: address in /home/www/reformcagunlaws.com/new.php on line 7 PHP Notice: ...
3
cassbiz
by: cassbiz | last post by:
Here are the errors that are coming up in my error_log Notice: Undefined index: andatum in /zipcode.php on line 11 Notice: Undefined index: andatum in /zipcode.php on line 12 Notice: Undefined...
3
by: number1yan | last post by:
Can anyone help me, i am creating a website and am using a php script that recomends the website to other people. I keep getting the same error and can not work out why. The error is: Notice:...
5
by: siyaverma | last post by:
Hi, I am new to php, i was doing some small chnages in a project developed by my collegue who left the job and i got the responsibility for that, After doing some changes when i run it on my...
5
by: movieking81 | last post by:
Another PHP newbie here, I trolled the boards here trying some of the different solutions but I keep getting the errors over an over. maybe my problem is specific. I keep getting the following when...
4
by: cyberlei | last post by:
hi all, I`m getting this error Notice: Undefined index: user in c:\inetpub\wwwroot\login.php on line 96 Notice: Undefined variable: message in c:\inetpub\wwwroot\login.php on line 101 Could...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
2
by: norwichchris | last post by:
hi, I am having serious trouble with my PHP Postcard script. The error message i get is: Notice: Undefined index: Notice: Undefined variable Basically it does not send out the scripts at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.