Frustrating MySQL/PHP issue i cant seem to comprehend [closed] - php

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Please someone help me, why is my code not speaking to mySQL table? I really have no clue why it doesn't work and i've spent days looking for solutions, and help would be greatly appreciated......
my html...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="Locus.js"></script> <!-- LINKS TO THE EXTERNAL JAVASCRIPT -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Locus of control test</title>
<link href="Locus.css" rel="stylesheet" type="text/css" /> <!-- LINKS TO THE EXTERNAL CS SHEET -->
</head>
<body>
<form action="Locus.php" id="Locus" method="post" name="Locus" onsubmit="validateForm()"> <!-- GIVES A LINK TO THE PHP FORM AND THE METHOD -->
<h4>Surname: <input type="text" name="surname"/> <br/>Prison: <input type="text" name="prison"/><br/>National Insurance: <input type="text" name="NI"/></h4>
<h3> Please carefully read all the instructions given on the left hand panel.</h3>
<h1 class="Header"> LOCUS OF CONTROL</h1>
<h2>Please click submit ONLY when all questions have been completed <br/> <input name="submit" id="submit" type="submit" value="Check & Submit" /></h2>
<p>1. I can anticipate difficulties and take action to avoid them. <br />
<input type="radio" name="Q1" value="4" /> Always Agree <input type="radio" name="Q1" value="3" /> Agree <input type="radio" name="Q1" value="2" /> Unsure <input type="radio" name="Q1" value="1" /> Disgree <input type="radio" name="Q1" value="0" /> Always Disagree
</p>
<p>2. A great deal of what happens to me is just a matter of chance. <br />
<input type="radio" name="Q2" value="0" /> Always Agree <input type="radio" name="Q2" value="1" /> Agree <input type="radio" name="Q2" value="2" /> Unsure <input type="radio" name="Q2" value="3" /> Disgree <input type="radio" name="Q2" value="4" /> Always Disagree
</p>
<p>3. Everyone knows that luck or chance determines the future. <br />
<input type="radio" name="Q3" value="0" /> Always Agree <input type="radio" name="Q3" value="1" /> Agree <input type="radio" name="Q3" value="2" /> Unsure <input type="radio" name="Q3" value="3" /> Disgree <input type="radio" name="Q3" value="4" /> Always Disagree
</p>
<p>4. I can control my problems only if I have outside support. <br />
<input type="radio" name="Q4" value="0" /> Always Agree <input type="radio" name="Q4" value="1" /> Agree <input type="radio" name="Q4" value="2" /> Unsure <input type="radio" name="Q4" value="3" /> Disgree <input type="radio" name="Q4" value="4" /> Always Disagree
</p>
<p>5. When I make plans I am almost certain I can make them work. <br />
<input type="radio" name="Q5" value="4" /> Always Agree <input type="radio" name="Q5" value="3" /> Agree <input type="radio" name="Q5" value="2" /> Unsure <input type="radio" name="Q5" value="1" /> Disgree <input type="radio" name="Q5" value="0" /> Always Disagree
</p>
<p>6. My problems will dominate all my life. <br />
<input type="radio" name="Q6" value="0" /> Always Agree <input type="radio" name="Q6" value="1" /> Agree <input type="radio" name="Q6" value="2" /> Unsure <input type="radio" name="Q6" value="3" /> Disgree <input type="radio" name="Q6" value="4" /> Always Disagree
</p>
<p>7. My mistakes and problems are my responsibility to deal with. <br />
<input type="radio" name="Q7" value="4" /> Always Agree <input type="radio" name="Q7" value="3" /> Agree <input type="radio" name="Q7" value="2" /> Unsure <input type="radio" name="Q7" value="1" /> Disgree <input type="radio" name="Q7" value="0" /> Always Disagree
</p>
<p>8. Becoming a success is a matter of hard work, luck has little or nothing to do with it. <br />
<input type="radio" name="Q8" value="4" /> Always Agree <input type="radio" name="Q8" value="3" /> Agree <input type="radio" name="Q8" value="2" /> Unsure <input type="radio" name="Q8" value="1" /> Disgree <input type="radio" name="Q8" value="0" /> Always Disagree
</p>
<p>9. My life is controlled by outside actions and events. <br />
<input type="radio" name="Q9" value="0" /> Always Agree <input type="radio" name="Q9" value="1" /> Agree <input type="radio" name="Q9" value="2" /> Unsure <input type="radio" name="Q9" value="3" /> Disgree <input type="radio" name="Q9" value="4" /> Always Disagree
</p>
<p>10. I believe people are victims of circumstances beyond their control. <br />
<input type="radio" name="Q10" value="0" /> Always Agree <input type="radio" name="Q10" value="1" /> Agree <input type="radio" name="Q10" value="2" /> Unsure <input type="radio" name="Q10" value="3" /> Disgree <input type="radio" name="Q10" value="4" /> Always Disagree
</p>
<p>11. To continually manage my problems I need professional help. <br />
<input type="radio" name="Q11" value="0" /> Always Agree <input type="radio" name="Q11" value="1" /> Agree <input type="radio" name="Q11" value="2" /> Unsure <input type="radio" name="Q11" value="3" /> Disgree <input type="radio" name="Q11" value="4" /> Always Disagree
</p>
<p>12. When I am under stress the tightness in my muscles is due to things outside my control.<br />
<input type="radio" name="Q12" value="0" /> Always Agree <input type="radio" name="Q12" value="1" /> Agree <input type="radio" name="Q12" value="2" /> Unsure <input type="radio" name="Q12" value="3" /> Disgree <input type="radio" name="Q12" value="4" /> Always Disagree
</p>
<p>13. 1 believe a person really can be master of his own fate.<br />
<input type="radio" name="Q13" value="4" /> Always Agree <input type="radio" name="Q13" value="3" /> Agree <input type="radio" name="Q13" value="2" /> Unsure <input type="radio" name="Q13" value="1" /> Disgree <input type="radio" name="Q13" value="0" /> Always Disagree
</p>
<p>14. It is impossible to control irregular fast breathing when I am having difficulties.<br />
<input type="radio" name="Q14" value="0" /> Always Agree <input type="radio" name="Q14" value="1" /> Agree <input type="radio" name="Q14" value="2" /> Unsure <input type="radio" name="Q14" value="3" /> Disgree <input type="radio" name="Q14" value="4" /> Always Disagree
</p>
<p>15. I understand why my problems vary so much from one occasion to another. <br />
<input type="radio" name="Q15" value="4" /> Always Agree <input type="radio" name="Q15" value="3" /> Agree <input type="radio" name="Q15" value="2" /> Unsure <input type="radio" name="Q15" value="1" /> Disgree <input type="radio" name="Q15" value="0" /> Always Disagree
</p>
<p>16. I am confident of being able to deal successfully with future problems.<br />
<input type="radio" name="Q16" value="4" /> Always Agree <input type="radio" name="Q16" value="3" /> Agree <input type="radio" name="Q16" value="2" /> Unsure <input type="radio" name="Q16" value="1" /> Disgree <input type="radio" name="Q16" value="0" /> Always Disagree
</p>
<p>17. In my case maintaining control over my problems is mainly due to luck. <br />
<input type="radio" name="Q17" value="0" /> Always Agree <input type="radio" name="Q17" value="1" /> Agree <input type="radio" name="Q17" value="2" /> Unsure <input type="radio" name="Q17" value="3" /> Disgree <input type="radio" name="Q17" value="4" /> Always Disagree
</p>
<p>18. I have often been blamed for events beyond my control. <br />
<input type="radio" name="Q18" value="0" /> Always Agree <input type="radio" name="Q18" value="1" /> Agree <input type="radio" name="Q18" value="2" /> Unsure <input type="radio" name="Q18" value="3" /> Disgree <input type="radio" name="Q18" value="4" /> Always Disagree
</p>
<h5>Please click 'submit' at the start of this test</h5>
</form>
</body>
</html>
here is my php....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="Locus.css" rel="stylesheet" type="text/css" /> <!-- LINKED ATTACHED STYLE SHEET-->
<title>Locus Test Complete</title>
</head>
<h4>THANK YOU FOR SUBMITTING THIS TEST, PLEASE CHOOSE ANOTHER FROM THE MENU BAR.</h4>
<body>
<?php
/*DECLARING THE VARIABLES FOR ACCESS*/
$username="root";
$password="";
$database_server="localhost";
/*CONNECTING TO THE SERVER*/
$database="locus";
mysql_connect($database_server,$username,$password) or die("cannot connect");
/*CONNECTING TO THE DATABASE*/
#mysql_select_db($database) or die( "Unable to select database");
/*CHECK THE STATUS AND DECLARE THE VARIABLES*/
$_POST = array( );
if( isset( $_POST['surname'])) {
$Surname = $_POST['surname'];
} else {
$Surname = "";
}
if( isset( $_POST['prison'])) {
$Prison = $_POST['prison'];
} else {
$Prison = "";
}
/*SHORTHAND WAY TO CHECK THE STATUS AND DECLARE THE VARIABLES*/
$NI = isset( $_POST['NI']) ? $_POST['NI'] : "";
$Q1 = isset( $_POST['Q1']) ? $_POST['Q1'] : "";
$Q2 = isset( $_POST['Q2']) ? $_POST['Q2'] : "";
$Q3 = isset( $_POST['Q3']) ? $_POST['Q3'] : "";
$Q4 = isset( $_POST['Q4']) ? $_POST['Q4'] : "";
$Q5 = isset( $_POST['Q5']) ? $_POST['Q5'] : "";
$Q6 = isset( $_POST['Q6']) ? $_POST['Q6'] : "";
$Q7 = isset( $_POST['Q7']) ? $_POST['Q7'] : "";
$Q8 = isset( $_POST['Q8']) ? $_POST['Q8'] : "";
$Q9 = isset( $_POST['Q9']) ? $_POST['Q9'] : "";
$Q10 = isset( $_POST['Q10']) ? $_POST['Q10'] : "";
$Q11 = isset( $_POST['Q11']) ? $_POST['Q11'] : "";
$Q12 = isset( $_POST['Q12']) ? $_POST['Q12'] : "";
$Q13 = isset( $_POST['Q13']) ? $_POST['Q13'] : "";
$Q14 = isset( $_POST['Q14']) ? $_POST['Q14'] : "";
$Q15 = isset( $_POST['Q15']) ? $_POST['Q15'] : "";
$Q16 = isset( $_POST['Q16']) ? $_POST['Q16'] : "";
$Q17 = isset( $_POST['Q17']) ? $_POST['Q17'] : "";
$Q18 = isset( $_POST['Q18']) ? $_POST['Q18'] : "";
/*PLAVE THE VALUES OF THE VARIABLES WITHIN THE DATABASE TABLE*/
$query = "INSERT INTO locus.locusofcontrolscores VALUES
('','$Surname','$Prison','$NI','$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10','$Q11','$Q12','$Q13','$Q14','$Q15','$Q16','$Q17','$Q18')";
/*CREATE A QUERY FROM THE VARIABLE*/
mysql_query($query);
/*CLOSE THE SERVER CONNECTION*/
mysql_close();
?>
</body>
</html>

$_POST = array( );
is wrong, remove this line.
$_POST is an array() you do not have to declare this.

according the screed shot in comment you are checking the structure ... click on the Browse tab than you will see the content
also try
mysql_query($query) or die(mysql_error());
to see the error in query
Warning : your code is vulnerable to SQL Injunction
also MySql_* function are deprecated use pdo or mysqli

Things that could solve your problem:
mysql_query($query) or die(mysql_error() . ' ' . $query);
don't clear out the post array
don't suppress the database connection or at least put the error in the die
Things you should probably fix, but probably isn't your problem:
$sql injection by putting the variables in the sql without escaping
them Using mysql instead of mysqli Validating the data, doing
controller stuff after the html started using radio buttons like
checkboxes not using an absolute path for your url
don't use root as the username to you db and have no password
no html should be in between the head and body.
Also I'm not sure you can have "" as the auto increment id. I would put Null there instead. Even better I'd explicitly put the columns you want to add to the table. You will have a of headaches later if you don't....say you have that same insert into a 1000 places...well you add a column and now you have to update a 1000 places.
Also I would echo out the sql statement you made and try and run it via command prompt or phpmyadmin.

Related

Saving Radio Button Inputs into Array

I am working on a custom survey that is dynamically populated with database information and needs to be saved as an array. I can make everything work as it should except I would like to use a star rating system for many of the answers and it seems radio buttons won't work like this:
<input type="radio" name="surveyRating[]" value="1" />
<input type="radio" name="surveyRating[]" value="2" />
<input type="radio" name="surveyRating[]" value="3" />
<input type="radio" name="surveyRating[]" value="4" />
<input type="radio" name="surveyRating[]" value="5" />
Because there are multiple instances of this same code. This will be saved into an array so surveyRating will be used over and over again. If I click a radio button in one group, it changes to a radio in another group.
I have read I can possibly do this with checkboxes instead. Is that the best alternative? Or is there another option I should be looking at? I want the final product to be star ratings 1-5. Not sure if I can do that with checkboxes.
EDIT
Ok so using name="surveyRating[1]" is helping as far as the radio buttons not conflicting on the client side. However, it is not saving correctly the way I have my php set up now. Here is how it is saved currently. What do I need to change to make the [1] work appropriately. Currently it is only saving the last iteration.
$new = array();
$ratings = $_POST['surveyRating'];
$count = count( $ratings );
for ( $i = 0; $i < $count; $i++ ) {
if ( $ratings[$i] != '' ) {
$new[$i]['surveyRating'] = stripslashes( strip_tags( $ratings[$i] ) );
}
}
EDIT 2
So to demonstrate how I accomplished this using the answer below, I had to add [0] as the first iteration in the loop. I was using $items = 0; $items++ to dynamically add a number to each loop.
To make it start at 0, I set $items = -1 that way the first iteration is 0 instead of 1. Hope that makes sense.
Yep, it does because you have the name with []. That's for array. Remove it and you will get it without array:
<input type="radio" name="surveyRating" value="1" />
<input type="radio" name="surveyRating" value="2" />
<input type="radio" name="surveyRating" value="3" />
<input type="radio" name="surveyRating" value="4" />
<input type="radio" name="surveyRating" value="5" />
For radio buttons with several groups, you have to do something like this:
<!-- Group 1 -->
<input type="radio" name="surveyRating[1]" value="1" />
<input type="radio" name="surveyRating[1]" value="2" />
<input type="radio" name="surveyRating[1]" value="3" />
<input type="radio" name="surveyRating[1]" value="4" />
<input type="radio" name="surveyRating[1]" value="5" />
<!-- Group 2 -->
<input type="radio" name="surveyRating[2]" value="1" />
<input type="radio" name="surveyRating[2]" value="2" />
<input type="radio" name="surveyRating[2]" value="3" />
<input type="radio" name="surveyRating[2]" value="4" />
<input type="radio" name="surveyRating[2]" value="5" />

Get variable on Wordpress to PHP

I'm trying to get a value of a form in Wordpress to PHP. The form is like this and it is displaying fine in the preview:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_4" value=0 />
<input type="checkbox" name="form_ques_4" value=1 />
<input type="checkbox" name="form_ques_4" value=2 />
<input type="submit" name="formSubmit" value="submit" />
</form>
If the user selected option 2, the value is 1 and this will later be used as the input in a MySQL database. As I have read in other posts, I should get value with the php line.
$a = $_GET["form_ques_4"];
I have tested some other simple outputs for the .php and there is no problem with the "form action" of the wordpress. I also tried using single and double quotes for the "GET" with no result.
Try to change the names of your checkboxes, if you want a user multiple choice:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_1" value="0" />
<input type="checkbox" name="form_ques_2" value="1" />
<input type="checkbox" name="form_ques_3" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
otherwise, if you want the user makes only one choice use type="radio"
<form action=".../name.php" method="get">
<input type="radio" name="form_ques_4" value="0" />
<input type="radio" name="form_ques_4" value="1" />
<input type="radio" name="form_ques_4" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
EDIT
yes, as AZinkey says, you can also use
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques[]" value="0" />
<input type="checkbox" name="form_ques[]" value="1" />
<input type="checkbox" name="form_ques[]" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
then get the results in php
$checked = $_GET['form_ques'];
for($i=0; $i < count($checked); $i++){
echo $checked[$i] . "<br/>";
}
Quote your value attribute like value="0" and update name to "form_ques_4[]"
<input type="checkbox" name="form_ques_4[]" value="0" />

PHP How to keep radio button state to the next page

I made a form with radio buttons. How can I preserve it's state after a user picked a choice? Then same form will show again in the next page and the radio button that the user picked is enabled.
//page1.html
<form method="post" action="page2.html">
<p>
<input type="radio" name="q1" value="A" />
A. <br />
<input type="radio" name="q1" value="B" />
B. <br />
<input type="radio" name="q1" value="C" />
C. <br />
<input type="radio" name="q1" value="D" />
D.
<p>
<input type="submit" name="action" value="Enter" />
</p>
</form>
To get the value of q1 on the next page, you would use $_POST['q1']. You can verify that the element has been posted, and the value matches the specific radio button by using if(isset($_POST['q1'])) && $_POST['q1'] == VALUE. So your form code would look like -
<input type="radio" name="q1" value="A" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'A')) echo 'checked="checked" ';?>/>
A. <br />
<input type="radio" name="q1" value="B" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'B')) echo 'checked="checked" ';?>/>
B. <br />
<input type="radio" name="q1" value="C" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'C')) echo 'checked="checked" ';?>/>
C. <br />
<input type="radio" name="q1" value="D" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'D')) echo 'checked="checked" ';?>/>

Undefined index in every part? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I have spent around three days on this problem and have no idea how to fix it i'm getting...
Notice: Undefined index: surname in C:\xampp\htdocs\Locus.php on line 25
Notice: Undefined index: prison in C:\xampp\htdocs\Locus.php on line 26
Notice: Undefined index: NI in C:\xampp\htdocs\Locus.php on line 27
Notice: Undefined index: Q1 in C:\xampp\htdocs\Locus.php on line 28
Notice: Undefined index: Q2 in C:\xampp\htdocs\Locus.php on line 29
Notice: Undefined index: Q3 in C:\xampp\htdocs\Locus.php on line 30
Notice: Undefined index: Q4 in C:\xampp\htdocs\Locus.php on line 31
Notice: Undefined index: Q5 in C:\xampp\htdocs\Locus.php on line 32
....etc
here is my HTML...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="Locus.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Locus of control test</title>
<link href="Locus.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="Locus.php" id="Locus" method="post" name="Locus" onsubmit="return validateForm()"
<h4>Surname: <input type="text" name="surname"/> <br/>Prison: <input type="text" name="prison"/><br/>National Insurance: <input type="text" name="NI"/></h4>
<h3> Please carefully read all the instructions given on the left hand panel.</h3>
<h1 class="Header"> LOCUS OF CONTROL</h1>
<h2>Please click submit ONLY when all questions have been completed <br/> <input name="submit" id="submit" type="submit" value="Check & Submit" /></h2>
<p>1. I can anticipate difficulties and take action to avoid them. <br />
<input type="radio" name="Q1" value="4" /> Always Agree <input type="radio" name="Q1" value="3" /> Agree <input type="radio" name="Q1" value="2" /> Unsure <input type="radio" name="Q1" value="1" /> Disgree <input type="radio" name="Q1" value="0" /> Always Disagree
</p>
<p>2. A great deal of what happens to me is just a matter of chance. <br />
<input type="radio" name="Q2" value="0" /> Always Agree <input type="radio" name="Q2" value="1" /> Agree <input type="radio" name="Q2" value="2" /> Unsure <input type="radio" name="Q2" value="3" /> Disgree <input type="radio" name="Q2" value="4" /> Always Disagree
</p>
<p>3. Everyone knows that luck or chance determines the future. <br />
<input type="radio" name="Q3" value="0" /> Always Agree <input type="radio" name="Q3" value="1" /> Agree <input type="radio" name="Q3" value="2" /> Unsure <input type="radio" name="Q3" value="3" /> Disgree <input type="radio" name="Q3" value="4" /> Always Disagree
</p>
<p>4. I can control my problems only if I have outside support. <br />
<input type="radio" name="Q4" value="0" /> Always Agree <input type="radio" name="Q4" value="1" /> Agree <input type="radio" name="Q4" value="2" /> Unsure <input type="radio" name="Q4" value="3" /> Disgree <input type="radio" name="Q4" value="4" /> Always Disagree
</p>
<p>5. When I make plans I am almost certain I can make them work. <br />
<input type="radio" name="Q5" value="4" /> Always Agree <input type="radio" name="Q5" value="3" /> Agree <input type="radio" name="Q5" value="2" /> Unsure <input type="radio" name="Q5" value="1" /> Disgree <input type="radio" name="Q5" value="0" /> Always Disagree
</p>
<p>6. My problems will dominate all my life. <br />
<input type="radio" name="Q6" value="0" /> Always Agree <input type="radio" name="Q6" value="1" /> Agree <input type="radio" name="Q6" value="2" /> Unsure <input type="radio" name="Q6" value="3" /> Disgree <input type="radio" name="Q6" value="4" /> Always Disagree
</p>
<p>7. My mistakes and problems are my responsibility to deal with. <br />
<input type="radio" name="Q7" value="4" /> Always Agree <input type="radio" name="Q7" value="3" /> Agree <input type="radio" name="Q7" value="2" /> Unsure <input type="radio" name="Q7" value="1" /> Disgree <input type="radio" name="Q7" value="0" /> Always Disagree
</p>
<p>8. Becoming a success is a matter of hard work, luck has little or nothing to do with it. <br />
<input type="radio" name="Q8" value="4" /> Always Agree <input type="radio" name="Q8" value="3" /> Agree <input type="radio" name="Q8" value="2" /> Unsure <input type="radio" name="Q8" value="1" /> Disgree <input type="radio" name="Q8" value="0" /> Always Disagree
</p>
<p>9. My life is controlled by outside actions and events. <br />
<input type="radio" name="Q9" value="0" /> Always Agree <input type="radio" name="Q9" value="1" /> Agree <input type="radio" name="Q9" value="2" /> Unsure <input type="radio" name="Q9" value="3" /> Disgree <input type="radio" name="Q9" value="4" /> Always Disagree
</p>
<p>10. I believe people are victims of circumstances beyond their control. <br />
<input type="radio" name="Q10" value="0" /> Always Agree <input type="radio" name="Q10" value="1" /> Agree <input type="radio" name="Q10" value="2" /> Unsure <input type="radio" name="Q10" value="3" /> Disgree <input type="radio" name="Q10" value="4" /> Always Disagree
</p>
<p>11. To continually manage my problems I need professional help. <br />
<input type="radio" name="Q11" value="0" /> Always Agree <input type="radio" name="Q11" value="1" /> Agree <input type="radio" name="Q11" value="2" /> Unsure <input type="radio" name="Q11" value="3" /> Disgree <input type="radio" name="Q11" value="4" /> Always Disagree
</p>
<p>12. When I am under stress the tightness in my muscles is due to things outside my control.<br />
<input type="radio" name="Q12" value="0" /> Always Agree <input type="radio" name="Q12" value="1" /> Agree <input type="radio" name="Q12" value="2" /> Unsure <input type="radio" name="Q12" value="3" /> Disgree <input type="radio" name="Q12" value="4" /> Always Disagree
</p>
<p>13. 1 believe a person really can be master of his own fate.<br />
<input type="radio" name="Q13" value="4" /> Always Agree <input type="radio" name="Q13" value="3" /> Agree <input type="radio" name="Q13" value="2" /> Unsure <input type="radio" name="Q13" value="1" /> Disgree <input type="radio" name="Q13" value="0" /> Always Disagree
</p>
<p>14. It is impossible to control irregular fast breathing when I am having difficulties.<br />
<input type="radio" name="Q14" value="0" /> Always Agree <input type="radio" name="Q14" value="1" /> Agree <input type="radio" name="Q14" value="2" /> Unsure <input type="radio" name="Q14" value="3" /> Disgree <input type="radio" name="Q14" value="4" /> Always Disagree
</p>
<p>15. I understand why my problems vary so much from one occasion to another. <br />
<input type="radio" name="Q15" value="4" /> Always Agree <input type="radio" name="Q15" value="3" /> Agree <input type="radio" name="Q15" value="2" /> Unsure <input type="radio" name="Q15" value="1" /> Disgree <input type="radio" name="Q15" value="0" /> Always Disagree
</p>
<p>16. I am confident of being able to deal successfully with future problems.<br />
<input type="radio" name="Q16" value="4" /> Always Agree <input type="radio" name="Q16" value="3" /> Agree <input type="radio" name="Q16" value="2" /> Unsure <input type="radio" name="Q16" value="1" /> Disgree <input type="radio" name="Q16" value="0" /> Always Disagree
</p>
<p>17. In my case maintaining control over my problems is mainly due to luck. <br />
<input type="radio" name="Q17" value="0" /> Always Agree <input type="radio" name="Q17" value="1" /> Agree <input type="radio" name="Q17" value="2" /> Unsure <input type="radio" name="Q17" value="3" /> Disgree <input type="radio" name="Q17" value="4" /> Always Disagree
</p>
<p>18. I have often been blamed for events beyond my control. <br />
<input type="radio" name="Q18" value="0" /> Always Agree <input type="radio" name="Q18" value="1" /> Agree <input type="radio" name="Q18" value="2" /> Unsure <input type="radio" name="Q18" value="3" /> Disgree <input type="radio" name="Q18" value="4" /> Always Disagree
</p>
<h5>Please click 'submit' at the start of this test</h5>
</form>
</body>
</html>
and here is my PHP ....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="Locus.css" rel="stylesheet" type="text/css" />
<title>Locus Test Complete</title>
</head>
<body>
<?php
/*variable declaration*/
$username="root";
$password="";
$database_server="localhost";
/*server connection*/
$database="Locus";
mysql_connect($database_server,$username,$password) or die("cannot connect");
/*database connection*/
#mysql_select_db($database) or die( "Unable to select database");
/*create values from Locus to PHP variables*/
$Surname = $_POST['surname'];
$Prison = $_POST['prison'];
$NI = $_POST['NI'];
$Q1 = $_POST['Q1'];
$Q2 = $_POST['Q2'];
$Q3 = $_POST['Q3'];
$Q4 = $_POST['Q4'];
$Q5 = $_POST['Q5'];
$Q6 = $_POST['Q6'];
$Q7 = $_POST['Q7'];
$Q8 = $_POST['Q8'];
$Q9 = $_POST['Q9'];
$Q10 = $_POST['Q10'];
$Q11 = $_POST['Q11'];
$Q12 = $_POST['Q12'];
$Q13 = $_POST['Q13'];
$Q14 = $_POST['Q14'];
$Q15 = $_POST['Q15'];
$Q16 = $_POST['Q16'];
$Q17 = $_POST['Q17'];
$Q18 = $_POST['Q18'];
/*assign the values to the locus of control scores table*/
$query = "INSERT INTO locus of control scores VALUES ('','$Surname','$Prison','$NI','$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10','$Q11','$Q12','$Q13','$Q14','$Q15','$Q16','$Q17','$Q18')";
mysql_query($query);
/*create query*/
mysql_query($query);
/*close server connection*/
mysql_close();
?>
</body>
</html>
Can anyone offer any insight as to why i'm getting these notices...?
any help would be greatly appreciated.
You can't reference keys from an array that doesn't have them, and that's what you're trying to do from the $_POST array, since the request didn't have those values posted.
To get rid of the notices, change all of your declarations where you pull values from $_POST directly from:
$Surname = $_POST['surname'];
To:
$Surname = isset( $_POST['surname']) ? $_POST['surname'] : ""; // Some default value
Also, in your code you're inserting the same query twice it looks like. And, VDP brings up a good point, you don't want to insert empty rows, so to fix that, you can wrap the whole logic for inserting with an if statement, check if the $_POST is empty, or check if every variable has a valid value before inserting.
Edit: OK, a more thorough explanation.
When you try to access keys from an array that the array does not have, you get this notice. So, your $_POST array is likely empty, because when you accessed the page, you didn't submit a POST request to it, or if you did, you didn't send the variables it's expecting.
So $_POST looks like:
$_POST = array( );
And you're trying to do:
$Surname = $_POST['surname']; // DOESN'T EXIST!
Clearly, you can't do that. So, instead of directly grabbing things from $_POST, you check if that key actually exists in the $_POST array:
$Surname = isset( $_POST['surname']) ? $_POST['surname'] : "";
However, this is just shorthand for:
if( isset( $_POST['surname'])) {
$Surname = $_POST['surname'];
} else {
$Surname = "";
}
It's called the ternary operator, and you can look it up on the PHP documentation to read more about it.
Instead of
$Surname = $_POST['surname'];
Do
if(isset($_POST['surname']))
{
$Surname = $_POST['surname'];
}
Or
$Surname = (isset($_POST['surname'])) ? $_POST['surname'] : $some_string; /* or something */
You could also just use extract() for localising all those post variables.
If you are COMPLETELY SURE that those notices won't screw your application and want a quick fix use
error_reporting(0);
or use #$variable;
I repeat, this is not really a solution but a temporal quick fix.
For really fixing it you should always check for the existence of the variables.
echo $myvariable; // will output notice
$myvariable = null;
echo $myvariable; // will output nothing but nor the notice
A good way is to use :
if(isset($_POST['myvariable'])){
$myvariable = $_POST['myvariable'];
} else {
$myvariable = null;
}

how to load a form from php file dynamically based on option

here is the code:
<ul>
<li><a id="various3" href="index2.html">Opera</a></li>
<li><a id="various3" href="index2.html">Chrome</a></li>
</ul>
it is available in index2.html
<form id="browsers" name="form100" action="browser.php" method="post">
<li> Do you like look and feel of your browser?<br/>
<input type="radio" name="q1" value="1"/> Yes
<input type="radio" name="q1" value="2"/> No
<li> Does your system gets slow when your browser is running?<br/>
<input type="radio" name="q2" value="1"/> Yes
<input type="radio" name="q2" value="2"/> No
<input type="radio" name="q2" value="3"/> Maybe
<li> Does the browser load fast when you click on the icon?<br/>
<input type="radio" name="q3" value="1"/> Yes
<input type="radio" name="q3" value="2"/> No
<input type="radio" name="q3" value="3"/> Maybe
<li> Does the images render fast in your browser?<br/>
<input type="radio" name="q4" value="1"/> Yes
<input type="radio" name="q4" value="2"/> No
<li> Does video and image streaming fast in your computer?<br/>
<input type="radio" name="q5" value="1"/> Yes
<input type="radio" name="q5" value="2"/> No
<li> Does every website load faster and properly in your browser?<br/>
<input type="radio" name="q6" value="1"/> Yes
<input type="radio" name="q6" value="2"/> No
<li> Does your browser get slow when you open more than 3 tabs?<br/>
<input type="radio" name="q7" value="1"/> Yes
<input type="radio" name="q7" value="2"/> No
<li> Does it needs inbuilt mp3 player?<br/>
<input type="radio" name="q8" value="1"/> Yes
<input type="radio" name="q8" value="2"/> No
<li> Does it needs inbuilt flash player?<br/>
<input type="radio" name="q9" value="1"/> Yes
<input type="radio" name="q9" value="2"/> No
<li> Does it needs inbuilt media player?<br/>
<input type="radio" name="q10" value="1" /> Yes
<input type="radio" name="q10" value="2" /> No
<li> Whether your browser should be visually customizable? <br/>
<input type="radio" name="q11" value="1" /> Yes
<input type="radio" name="q11" value="2" /> No</li>
<li> Should your browser have twitter and Facebook integration?<br/>
<input type="radio" name="q12" value="1" /> Yes
<input type="radio" name="q12" value="2" /> No</li>
<li> Should your browser have chat application?<br/>
<input type="radio" name="q13" value="1" /> Yes
<input type="radio" name="q13" value="2" /> No
</li>
<li> Will you recommend this to a friend?<br/>
<input type="radio" name="q14" value="1" /> Yes
<input type="radio" name="q14" value="2" /> No
</li>
<li id="com">Type in your comments<br/>
<textarea name="comments" value="" cols=40 rows=6></textarea><br/>
<input type="submit" name="submit" value="I've verfied the above. Submit It!"><br/>
</ul>
</form>
when i run index.html file. it has two link Opera and chrome. on clicking both it goes to index2.html. that is fine..
now i need to dynamically load form. the index2.html contains 14 question. when opera in clicked that 1,3,4,5,6,7 questions to be loaded and when chrome is clicked the remaining quesitons should be loaded. instead of making two html file to do this. is there any way to load the form based on selected option using php????
change your links to
<li><a id="various3" href="index2.html?browser=opera">Opera</a></li>
<li><a id="various3" href="index2.html?browser=chrome">Chrome</a></li>
then in index2 you can see the browser selected in the $_GET global
$_GET['browser']
then you can go
if ($_GET['browser']=='opera'){
//whatever
}

Categories