Undefined index in every part? [duplicate] - php

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;
}

Related

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" />

How can i fetching,viewing and updating checkbox values stored in db

i have a html form that has a more then 30 checkboxes. checkboxes are used to select for different service.
I want to store those all value in 1 Database field seperated by , those could be numeric or string. Code for inserting checkbox values is as follows:
<form method="post" action="posted.php">
Option 1: <input type="checkbox" name="type" value="1" /><br />
Option 2: <input type="checkbox" name="type" value="2" /><br />
Option 3: <input type="checkbox" name="type" value="3" /><br />
Option 4: <input type="checkbox" name="type" value="4" /><br />
Option 5: <input type="checkbox" name="type" value="5" /><br />
Option 6: <input type="checkbox" name="type" value="6" /><br />
Option 7: <input type="checkbox" name="type" value="7" /><br />
Option 8: <input type="checkbox" name="type" value="8" /><br />
Option 9: <input type="checkbox" name="type" value="9"/><br />
<input type="submit" value="Submit" />
</form>
After successful submission off course i need to fetch those data in the corespondent check box while update.
Please response me as soon as possible with complete PHP code.
You can use JSON. From this:
Option 1: <input type="checkbox" name="myval[option_name1]" /><br />
Option N: <input type="checkbox" name="myval[option_nameN]" /><br />
Store:
$model->field = json_encode($_POST['myval']);
Or comma-seporated (if you really want):
$model->field = implode(",", $_POST['myval']);

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

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.

How to handle multiple checkboxes in a PHP form?

I have multiple checkboxes on my form:
<input type="checkbox" name="animal" value="Cat" />
<input type="checkbox" name="animal" value="Dog" />
<input type="checkbox" name="animal" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['submit']) {
echo $_POST['animal'];
}
I get "Bear", i.e. the last chosen checkbox value even though I picked all three. How to get all 3?
See the changes I have made in the name:
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
you have to set it up as array.
print_r($_POST['animal']);
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo $animal;
}
}
use square brackets following the field name
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
On the PHP side, you can treat it like any other array.

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