423,319 Members | 2,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,319 IT Pros & Developers. It's quick & easy.

undefined index error

P: 2
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. //connecting to the database 
  3. define('DB_HOST', 'localhost'); 
  4. define('DB_NAME', 'survey'); 
  5. define('DB_USER','root'); 
  6. define('DB_PASSWORD',''); 
  7. $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); 
  8. $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
  9. ?>
  10.  
  11. <html> 
  12. <head> 
  13. <title>page1</title> 
  14. <link rel="stylesheet" type="text/css" href="style.css"> 
  15. </head>
  16. <div id="one"> 
  17. <form method="post">
  18.  
  19.  
  20.   <h4>Answer all the questions on the scale (1)Strongly agree to (5)Strongly disagree </h4>
  21.   <table width="1000" border="1" cellpadding="10">
  22.     <tbody>
  23.       <tr>
  24.         <th style="width:50% ; text-align:center" scope="col">Questions</th>
  25.         <th style="width:10% ; text-align:center" scope="col">Strongly agree</th>
  26.         <th style="width:10% ; text-align:center" scope="col">Agree</th>
  27.         <th style="width:10% ; text-align:center" scope="col">Neutral</th>
  28.         <th style="width:10% ; text-align:center" scope="col">Disagree</th>
  29.         <th style="width:10% ; text-align:center" scope="col">Strongly disagree</th>
  30.       </tr>
  31.       <tr>
  32.         <td>1.People are clear about their role and performance levels expected of them.</td>
  33.         <td><input type="radio" name="q1" value="q1a">
  34.           1</td>
  35.         <td><input type="radio" name="q1" value="q1b">
  36.           2</td>
  37.         <td><input type="radio" name="q1" value="q1c">
  38.           3</td>
  39.         <td><input type="radio" name="q1" value="q1d">
  40.           4</td>
  41.         <td><input type="radio" name="q1" value="q1e">
  42.           5</td>
  43.       </tr>
  44.       <tr>
  45.         <td>2.People understand how their role fits with the responsibilities of others.</td>
  46.         <td><input type="radio" name="q2" value="q2a">
  47.           1</td>
  48.         <td><input type="radio" name="q2" value="q2b">
  49.           2</td>
  50.         <td><input type="radio" name="q2" value="q2c">
  51.           3</td>
  52.         <td><input type="radio" name="q2" value="q2d">
  53.           4</td>
  54.         <td><input type="radio" name="q2" value="q2e">
  55.           5</td>
  56.       </tr>
  57.       <tr>
  58.         <td>3.People understand the contribution they are making to organisational objectives.</td>
  59.         <td><input type="radio" name="q3" value="q3a">
  60.           1</td>
  61.         <td><input type="radio" name="q3" value="q3b">
  62.           2</td>
  63.         <td><input type="radio" name="q3" value="q3c">
  64.           3</td>
  65.         <td><input type="radio" name="q3" value="q3d">
  66.           4</td>
  67.         <td><input type="radio" name="q3" value="q3e">
  68.           5</td>
  69.       </tr>
  70.       <tr>
  71.         <td>4.People are clear about the behaviours expected of them in achieving their objectives.</td>
  72.         <td><input type="radio" name="q4" value="q4a">
  73.           1</td>
  74.         <td><input type="radio" name="q4" value="q4b">
  75.           2</td>
  76.         <td><input type="radio" name="q4" value="q4c">
  77.           3</td>
  78.         <td><input type="radio" name="q4" value="q4d">
  79.           4</td>
  80.         <td><input type="radio" name="q4" value="q4e">
  81.           5</td>
  82.       </tr>
  83.       <tr>
  84.         <td>5.Values and attributes are aligned with each other and support critical behaviour such as customer service or innovation.</td>
  85.         <td><input type="radio" name="q5" value="q5a">
  86.           1</td>
  87.         <td><input type="radio" name="q5" value="q5b">
  88.           2</td>
  89.         <td><input type="radio" name="q5" value="q5c">
  90.           3</td>
  91.         <td><input type="radio" name="q5" value="q5d">
  92.           4</td>
  93.         <td><input type="radio" name="q5" value="q5e">
  94.           5</td>
  95.       </tr>
  96.       <tr>
  97.         <td>6.People feel the organisational systems are 'in sync' with their objectives.</td>
  98.         <td><input type="radio" name="q6" value="q6a">
  99.           1</td>
  100.         <td><input type="radio" name="q6" value="q6b">
  101.           2</td>
  102.         <td><input type="radio" name="q6" value="q6c">
  103.           3</td>
  104.         <td><input type="radio" name="q6" value="q6d">
  105.           4</td>
  106.         <td><input type="radio" name="q6" value="q6e">
  107.           5</td>
  108.       </tr>
  109.       <tr>
  110.         <td>7.People believe they are given space to try new ideas or ways of doing things.</td>
  111.         <td><input type="radio" name="q7" value="q7a">
  112.           1</td>
  113.         <td><input type="radio" name="q7" value="q7b">
  114.           2</td>
  115.         <td><input type="radio" name="q7" value="q7c">
  116.           3</td>
  117.         <td><input type="radio" name="q7" value="q7d">
  118.           4</td>
  119.         <td><input type="radio" name="q7" value="q7e">
  120.           5</td>
  121.       </tr>
  122.       <tr>
  123.         <td>8.Knowledge-sharing enables people to learn from one another.</td>
  124.         <td><input type="radio" name="q8" value="q8a">
  125.           1</td>
  126.         <td><input type="radio" name="q8" value="q8b">
  127.           2</td>
  128.         <td><input type="radio" name="q8" value="q8c">
  129.           3</td>
  130.         <td><input type="radio" name="q8" value="q8d">
  131.           4</td>
  132.         <td><input type="radio" name="q8" value="q8e">
  133.           5</td>
  134.       </tr>
  135.       <tr>
  136.         <td>9.Cross-functional working enables people to learn from others' experiences.</td>
  137.         <td><input type="radio" name="q9" value="q9a">
  138.           1</td>
  139.         <td><input type="radio" name="q9" value="q9b">
  140.           2</td>
  141.         <td><input type="radio" name="q9" value="q9c">
  142.           3</td>
  143.         <td><input type="radio" name="q9" value="q9d">
  144.           4</td>
  145.         <td><input type="radio" name="q9" value="q9e">
  146.           5</td>
  147.       </tr>
  148.       <tr>
  149.         <td>10.People watch for changes within the external world that might impact on the organisation.</td>
  150.         <td><input type="radio" name="q10" value="q10a">
  151.           1</td>
  152.         <td><input type="radio" name="q10" value="q10b">
  153.           2</td>
  154.         <td><input type="radio" name="q10" value="q10c">
  155.           3</td>
  156.         <td><input type="radio" name="q10" value="q10d">
  157.           4</td>
  158.         <td><input type="radio" name="q10" value="q10e">
  159.           5</td>
  160.       </tr>
  161.     </tbody>
  162.   </table>
  163.   <INPUT TYPE="submit" name="submit" VALUE="submit">
  164. </form>
  165. </div>
  166. </body>
  167. </html> 
  168.  
  169.  <?php
  170. //Get values and store in variables
  171. $q1 = $_POST['q1'];
  172. $q2 = $_POST['q2'];
  173. $q3 = $_POST['q3'];
  174. $q4 = $_POST['q4'];
  175. $q5 = $_POST['q5'];
  176. $q6 = $_POST['q6'];
  177. $q7 = $_POST['q7'];
  178. $q8 = $_POST['q8'];
  179. $q9 = $_POST['q9'];
  180. $q10 = $_POST['q10'];
  181. ?>
  182.  
  183. <?php
  184. //Inserting values into the record
  185. $query = "INSERT INTO trial(q1,q2,q3,q4,q5,q6,q7,q8,q9,q10)VALUES('$q1','$q2','$q3','$q4','$q5','$q6','$q7','$q8','$q9','$q10')"; 
  186. $result = mysql_query($query);   
  187.  
  188. //posting result
  189. if($result)
  190. echo "survey over";
  191. }
  192.  
  193. else 
  194. die('Error: '.mysql_error($con)); 
  195. mysql_close($con)
  196. ?>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  197.  
Notice: Undefined index: q1 in C:\xampp\htdocs\surveypg\test.php on line 166
Similar error for all the 10 post variables.
I get the output as survey over even if i dont select any option and dont press the submit key
Is there a problem in the sequence of the code or any other problem ?
May 17 '16 #1
Share this Question
Share on Google+
2 Replies


Expert Mod 100+
P: 588
The line number in the error message doesn't correlate with the code you posted. Are you sure this is the version of the script that generated the error? I'll assume that the error is related to lines 172-181.

Are you receiving that error on the initial loading of the form page prior to submitting the form?

Since that section of code is not within a conditional block which checks if it's the initial page load or the form submission, you will receive that error on the initial page load prior to submitting the form.
May 18 '16 #2

P: 57
Can you at least show us the line? But there are 2 probabilities I can think of.

1. You forgot the "method" attribute in the "form" tag

2. When the page loads, the input boxes are empty by default. So, PHP doesn't get any value. To check this, enter a value to the input. If the error is gone, then put the code in an if conditional.

Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST['submit']) {
  2.     # code...
  3. }
.
Jul 6 '16 #3

Post your reply

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