I can get the $_POST variables in the next function once, but when repeating the function or going to the next function the variables aren't accessible. I am using PHP class and multiple functions. I have searched and haven't found the solution. Maybe I am missing something simple. I am new to using classes in PHP, and also $_POST variables. I am unclear why they aren't accessible within some functions. Thanks in advance.
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$MainPage = new MainPage();
$MainPage->showMainPage();
class MainPage
{
function __construct()
{
}
public function showMainPage()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "<meta charset=\"utf-8\">";
echo " </head>";
echo "<body>";
$showmainoptions = 0;
if (isset($_GET['showmainoptions']))
{
$showmainoptions = $_GET['showmainoptions'];
}
$showFinOptions = 0;
if (isset($_GET['showfinoptions']))
{
$showFinOptions = $_GET['showfinoptions'];
}
$showInspectionList = 0;
if (isset($_GET['showinspectionlist']))
{
$showInspectionList = $_GET['showinspectionlist'];
}
if($showmainoptions)
{
$this->showMainOptions();
}
if($showFinOptions)
{
$this->showFinOptions();
}
if($showInspectionList)
{
$this->showInspectionList();
}
$this->showFooterHTML();
return;
}
public function showMainOptions()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
echo "Select Main Options<br>";
echo "<form name=\"submitMainOptions\" id=\"submitMainOptions\" method=\"post\" action=\"mainpage.php?showmainoptions=0&showfinoptions=1&showinspectionlist=0&addnewinspection=0\">";
echo "<label><font size=4px>Wharehouse</font></label> ";
echo "<select id=\"house\" name=\"house\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"CYP\">Cypress</option>";
echo "<option value=\"FAY\">Fayetville</option>";
echo "<option value=\"FUR\">Furnace</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Fin Type</font></label> ";
echo "<select id=\"fintype\" name=\"fintype\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"Copper\">Copper</option>";
echo "<option value=\"Aluminum\">Aluminum</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Shift</font></label> ";
echo "<select id=\"shift\" name=\"shift\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"1\">Shift 1</option>";
echo "<option value=\"2\">Shift 2</option>";
echo "<option value=\"3\">Shift 3</option>";
echo "</select>";
echo "<br>";
echo "<button type=\"submit\" class=\"btn btn-primary btn-block btn-flat\">Continue</button></a>";
echo "</form>";
}
public function showFinOptions()
{
$house = $_POST['house'];
echo $house."<br>";
echo "Select Fin Options<br>";
echo "<form name=\"submitFinSelection\" id=\"submitFinSelection\" method=\"post\" action=\"mainpage.php?showmainoptions=0&showfinoptions=0&showinspectionlist=1&addnewinspection=0\">";
echo "<label><font size=4px>Part No</font></label> ";
echo "<select id=\"partno\" name=\"partno\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"partno1\">partno 1</option>";
echo "<option value=\"partno2\">partno 2</option>";
echo "<option value=\"partno3\">partno 3</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Fin Size</font></label> ";
echo "<select id=\"finsize\" name=\"finsize\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"5mm\">5mm</option>";
echo "<option value=\"7mm\">7mm</option>";
echo "</select>";
echo "<br>";
echo "<label><font size=4px>Press No</font></label> ";
echo "<select id=\"pressno\" name=\"pressno\" required>";
echo "<option value=\"\" selected></option>";
echo "<option value=\"CF016\">CF016</option>";
echo "<option value=\"CF018\">CF018</option>";
echo "<option value=\"CF020\">CF020</option>";
echo "</select>";
echo "<br>";
echo "<button type=\"submit\" class=\"btn btn-primary btn-block btn-flat\">Continue</button></a>";
echo "</form>";
}
public function showInspectionList()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$house = $_POST['house'];
echo $house."<br>";
$pressno = $_POST['pressno'];
echo $pressno."<br>";
}
public function showFooterHTML()
{
echo " </body>";
echo "</html>";
return;
}
}
?>
Related
How would I be able to give the different things different names (as I am creating them in a loop) so that I can use the data? Could I modify name=\"type\" to maybe include $x within the name so that it is different each time?
For($x=0; $x<=$noQuestions-1; $x++){
echo "<table>";
echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
echo "<form method=\"post\">";
echo "<Select class=\"form-control\" type=\"text\" name=\"type\" required>";
echo "<option value=\"1\">".$optionData[$x][0]."</option>";
echo "<option value=\"2\">".$optionData[$x][1]."</option>";
echo "<option value=\"3\">".$optionData[$x][2]."</option>";
echo "<option value=\"4\">".$optionData[$x][3]."</option>";
echo "</select>";
echo "</form>";
echo "</table>";
}
You can simply concatenate variable $x in name like this:
echo "<Select class=\"form-control\" type=\"text\" name=\"type".$x."\" required>";
EDIT:
$type = "dropdown";
for($x=0; $x<=$noQuestions-1; $x++)
{
echo "<table>";
echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
echo "<form method=\"post\">";
echo "<Select class=\"form-control\" type=\"text\" name=\"".$type.$x."\" required>";
echo "<option value=\"1\">".$optionData[$x][0]."</option>";
echo "<option value=\"2\">".$optionData[$x][1]."</option>";
echo "<option value=\"3\">".$optionData[$x][2]."</option>";
echo "<option value=\"4\">".$optionData[$x][3]."</option>";
echo "</select>"; echo "</form>";
echo "</table>";
}
One simple solution would be to add $x in the name like this for example:
echo "< select class=\"form-control\" type=\"text\" name=\"type-$x\" required>";
<?php
$status=&$_POST['status'];
$from=&$_POST['date_from'];
$to=&$_POST['date_to'];
$conn=mysqli_connect('localhost','root','','punbus') or die("Database not connected".mysqli_error());
if(isset($_POST['sub'])){
$ins="insert into driver_status(driver_name,status,date_from,date_to)
select Driver_name,'$status','$from','$to' from driver_master";
if(mysqli_query($conn,$ins)){
echo "added";
}
else{
echo "NOT".mysqli_error($conn);
}
}
$sel='select Driver_name from driver_master';
$query=mysqli_query($conn,$sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['Driver_name']."</td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
?>
<td>
<select name="status">
<?php
while($row1=mysqli_fetch_assoc($query1)){
$st=$row1['d_status'];
echo "<option value='$st'>$st</option>";
}
?>
</select>
</td>
<?php
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
That is my code. I want to save option selected from 4 drop down list to mysql. When I submit the form, the value selected from last box are getting saved in all rows of mysql table. Now, please tell me what should I do?
I am getting drop down box values from database table properly so what is the problem?
this is your code.
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from = $_POST['date_from'];
$to = $_POST['date_to'];
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$s','$from','$to')";
if (mysqli_query($conn, $ins)) {
echo "added";
} else {
echo "NOT" . mysqli_error($conn);
}
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>";
}
echo "</select></td></tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
this is similar to other textbox
$op="select * from client where active=0";
$op1=mysql_query($op);
echo '<select name="c" id="c" style="width:160px;" required>';
while ($row = mysql_fetch_array($op1))
{
echo $s=$row["c_name"];
if($company!=$s)
{
echo '<option value="'.$row["c_name"].'">'.$row["c_name"].'</option>';
}
}
$c=$_REQUEST['c'];
$sql="insert into project c_name) values('$c')";
$sql1=mysql_query($sql);
I am required to create a drop down box which gathers the records from a database to populate the drop down. When one of the values is selected, a form is to be displayed which contains the data relating to the value selected.
The form is to have the function of showing the selected data, but also to update the record in the database when filled out and Submit.
I have created a php file to try and accomplish this, but Im getting errors such as undefined index and unknown column.
I am only new to PHP so this is a big task for me. Could someone have a look at my code and inform me if there are any errors.
Ive been trying to piece code together here and there from the net but its been tricky trying to get it all to work.
I am not getting any errors now after some tweaking, but the record wont update. I get a 'record updated successfully' message but the record isn't updated.
I am pretty sure the lack of a record update is coming down to the ID not getting collected properly via the $q=$row["BearId"] but if I use $q=$_GET["q"] I get nothing but errors. I am not completely positive this is the problem though, that is why Im asking the question here.
I would appreciate any help that you can give. Ive gotten so far with this thing and yet I cant get it to update the record.
EDIT: I have pinpointed the problem down to the id in
$sql = "UPDATE //snip WHERE BearId = '$q'";
$q=$row["BearId"];
If I manually change BearId to equal '1' then the record is updated.
updatebears.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" />
<title>Update Bears</title>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("result").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getvalues.php?q="+str,true);
xmlhttp.send();
}
</script>
<style>
// style elements
</style>
</head>
<body>
<h1>Update Bears</h1>
Select a Bear:
<br />
<select name="bears" onchange="showUser(this.value)">
<option value="">Select a BearId</option>
<?php
$query = "SELECT * FROM bears";
$mysqli = new mysqli('localhost','User','123','bears');
$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
echo '<option value="'.$row["BearId"].'">'.$row["BearId"].'</option>';
?>
</select>
<br />
<?php
$q=$row["BearId"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if(array_key_exists('_submit_check', $_POST))
{
$weight = $_POST['Weight'];
$sex = $_POST['Sex'];
$type = $_POST['Type'];
$colour = $_POST['Colour'];
$breed = $_POST['BreedId'];
$sql = "UPDATE bears SET Weight = '$weight', Sex = '$sex', Type = '$type', Colour = '$colour', Breed = '$breed' WHERE BearId = '$q'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
?>
<br />
<div id="result"></div>
<br />
Click here to Visit Task 2 (Insert Bears) | Click here to Visit Task 3 (Display Bears)
</body>
</html>
getvalues.php
<?php
$q=$_GET["q"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if($stmt = $mysqli->prepare($sql))
{
$stmt->execute();
$stmt->bind_result($BearId, $Weight, $Sex, $Type, $Colour, $Breed);
while ($stmt->fetch())
{
echo "<form method='post' name='form1' onsubmit='return validateForm()' action='updatebears.php'>";
echo "<p>";
echo "<label for='BreedId'>BreedId:</label>";
echo "<br />";
echo "<select id='BreedId' name='BreedId' />";
echo "<option value='".$Breed."'>".$Breed."</option>";
echo "<option value='1'>1. Polar</option>";
echo "<option value='2'>2. Brown</option>";
echo "<option value='3'>3. Panda</option>";
echo "</select>";
echo "</p>";
echo "<p>";
echo "<label for='Weight'>Weight(kg):</label>";
echo "<br />";
echo "<input type='text' id='Weight' name='Weight' value='".$Weight."' />";
echo "</label>";
echo "</p>";
echo "<p>";
echo "Sex: ";
echo "<br />";
echo "<label for='M'>Male</label><input type='radio' id='M' value='M' name='Sex'";
if($Sex=='M') echo "checked";
echo "/>";
echo "<label for='F'>Female</label><input type='radio' id='F' value='F' name='Sex'";
if($Sex=='F') echo "checked";
echo "/>";
echo "</p>";
echo "<p>";
echo "<label for='Type'>Type:</label> ";
echo "<br />";
echo "<input type='text' id='Type' name='Type' maxlength='100' value='".$Type."' />";
echo "</p>";
echo "<p>";
echo "<label for='Colour'>Colour:</label>";
echo "<br />";
echo "<input type='text' id='Colour' name='Colour' maxlength='20' value='".$Colour."' />";
echo "</p>";
echo "<p>";
echo "<input type='submit' value='Submit' />";
echo "<input type='reset' value='Reset' />";
echo "<input type='hidden' name='_submit_check' value=1 />";
echo "</p>";
echo "</form>";
}
}
else
{
echo 'Unable to connect';
exit();
}
?>
Thanks for the help.
I believe what you need to do is create a hidden input type for the BearId within the getvalues.php file. That way when your form performs the post you can get the BearId from the post as opposed to trying to get it from the $row['BearId']. I'm fairly certain $row['BearId'] is not the same $row['BearId'] that the user selected when he first goes to the getvalues.php form. Have you tried printing $row['BearId'] to html to verify it's a legitimate value?
if(array_key_exists('_submit_check', $_POST))
{
$id = $_POST['BearId']
$weight = $_POST['Weight'];
$sex = $_POST['Sex'];
$type = $_POST['Type'];
$colour = $_POST['Colour'];
$breed = $_POST['BreedId'];
$sql = "UPDATE bears SET Weight = '$weight', Sex = '$sex', Type = '$type', Colour = '$colour', Breed = '$breed' WHERE BearId = '$id'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
?>
<h1>getvalues.php</h1>
<?php
$q=$_GET["q"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if($stmt = $mysqli->prepare($sql))
{
$stmt->execute();
$stmt->bind_result($BearId, $Weight, $Sex, $Type, $Colour, $Breed);
while ($stmt->fetch())
{
echo "<form method='post' name='form1' onsubmit='return validateForm()' action='updatebears.php'>";
echo <input type="hidden" name="BearId" value='".$q."'>
echo "<p>";
echo "<label for='BreedId'>BreedId:</label>";
echo "<br />";
echo "<select id='BreedId' name='BreedId' />";
echo "<option value='".$Breed."'>".$Breed."</option>";
echo "<option value='1'>1. Polar</option>";
echo "<option value='2'>2. Brown</option>";
echo "<option value='3'>3. Panda</option>";
echo "</select>";
echo "</p>";
echo "<p>";
echo "<label for='Weight'>Weight(kg):</label>";
echo "<br />";
echo "<input type='text' id='Weight' name='Weight' value='".$Weight."' />";
echo "</label>";
echo "</p>";
echo "<p>";
echo "Sex: ";
echo "<br />";
echo "<label for='M'>Male</label><input type='radio' id='M' value='M' name='Sex'";
if($Sex=='M') echo "checked";
echo "/>";
echo "<label for='F'>Female</label><input type='radio' id='F' value='F' name='Sex'";
if($Sex=='F') echo "checked";
echo "/>";
echo "</p>";
echo "<p>";
echo "<label for='Type'>Type:</label> ";
echo "<br />";
echo "<input type='text' id='Type' name='Type' maxlength='100' value='".$Type."' />";
echo "</p>";
echo "<p>";
echo "<label for='Colour'>Colour:</label>";
echo "<br />";
echo "<input type='text' id='Colour' name='Colour' maxlength='20' value='".$Colour."' />";
echo "</p>";
echo "<p>";
echo "<input type='submit' value='Submit' />";
echo "<input type='reset' value='Reset' />";
echo "<input type='hidden' name='_submit_check' value=1 />";
echo "</p>";
echo "</form>";
}
}
else
{
echo 'Unable to connect';
exit();
}
I've coded a form for a hiring application on an online game, but am running some issues when I actually put the code on the website. Running this on my local apache web server runs fine, but doing this on a LiteSpeed web server online seems to not work. Is there anything invalid in the code?
I've set the code already to detect any errors (I've removed that from the below sample), but nothing turns up except for undefined variables (which is fine in this case).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Zone Application Manager</title>
</head>
<body>
<?php
require("includes.php");
function displayElements($category) {
echo "<div id=\"left\">\n";
$col1 = mysql_query("SELECT * FROM gp_applications WHERE category = '$category' ORDER BY id ASC");
echo "<h3>" . $category . "</h3>\n";
echo "<form action=\"#\" method=\"get\">\n";
echo "<select name=\"category\">\n";
if ($category=="Unread Apps") {
echo "<option value=\"Unread Apps\" selected=\"selected\">Unread Apps</option>\n";
echo "<option value=\"Deleted Apps\">Deleted Apps</option>\n";
echo "<option value=\"Good Apps\">Good Apps</option>\n";
}
if ($category=="Deleted Apps") {
echo "<option value=\"Unread Apps\">Unread Apps</option>\n";
echo "<option value=\"Deleted Apps\" selected=\"selected\">Deleted Apps</option>\n";
echo "<option value=\"Good Apps\">Good Apps</option>\n";
}
if ($category=="Good Apps") {
echo "<option value=\"Unread Apps\">Unread Apps</option>\n";
echo "<option value=\"Deleted Apps\">Deleted Apps</option>\n";
echo "<option value=\"Good Apps\" selected=\"selected\">Good Apps</option>\n";
}
echo "</select>\n";
echo "<input type=\"submit\" value=\"Choose\">\n";
echo "</form>\n";
echo "<br />\n";
echo "<form action=\"#\" method=\"get\">\n";
echo "<input type=\"hidden\" name=\"category\" value=\"" . $category . "\">\n";
echo "<select name=\"id\" size=\"25\">\n";
while($row1 = mysql_fetch_array($col1)) {
echo "<option value=\"" . $row1[id] . "\">" . $row1[q2] . "</option>\n";
}
echo "</select>\n";
echo "<br />\n";
echo "<br />\n";
echo "<input type=\"submit\" name=\"read\" value=\"Read Application\">\n";
echo "<br />\n";
echo "<input type=\"submit\" name=\"delete\" value=\"Delete Application\">\n";
echo "<br />\n";
echo "<input type=\"submit\" name=\"unread\" value=\"Mark Application Unread\">\n";
echo "<br />\n";
echo "<input type=\"submit\" name=\"good\" value=\"Good Application\">\n";
echo "</div>\n";
echo "</form>";
echo "<form action=\"#\" method=\"get\">\n";
echo "<div id=\"bottom\">\n";
echo "<select name=\"appMode\">";
$mode = file_get_contents('mode.txt');
if($mode=="yes") {
echo "<option value=\"appon\" selected=\"selected\">Turn Application On</option>";
echo "<option value=\"appoff\">Turn Application Off</option>";
} else {
echo "<option value=\"appon\">Turn Application On</option>";
echo "<option value=\"appoff\" selected=\"selected\">Turn Application Off</option>";
}
echo "</select>";
echo "<input type=\"submit\" name=\"appModeBtn\" value=\"Toggle App Mode\">";
echo "</div>";
echo "</form>";
}
function executeAppAction($id) {
if (isset($_GET['good'])) {
mysql_query("UPDATE gp_applications SET category='Good Apps' WHERE id='$id'")or die(mysql_error());
header("Location: admin.php?action=admin");
}
if (isset($_GET['unread'])) {
mysql_query("UPDATE gp_applications SET category='Unread Apps' WHERE id='$id'")or die(mysql_error());
header("Location: admin.php");
}
if (isset($_GET['delete'])) {
mysql_query("UPDATE gp_applications SET category='Deleted Apps' WHERE id='$id'")or die(mysql_error());
header("Location: admin.php");
}
if (isset($_GET['read'])) {
$result = mysql_query("SELECT * FROM gp_applications WHERE id='$id'");
if ($result) {
$row = mysql_fetch_assoc($result);
}
echo "<div id=\"formBody\">";
//application opener, not needed to view
echo "</div>\n";
}
}
if($_POST['pwd'] || isset($_COOKIE['zonegp_application_login'])) {
if($_POST['pwd']=="xxxx") {
$hour = time() + 3600;
$pwd = $_POST['pwd'];
setcookie('zonegp_application_login', $pwd, $hour);
}
if($_COOKIE['zonegp_application_login']=="xxxx" || $_POST['pwd']=="xxxx") {
echo "<h1>The Doctor's Application Manager</h1>\n";
echo "<p>Logout</p>\n";
if($_GET['category']) {
$appcategory = $_GET['category'];
}
if(empty($appcategory)) {
$appcategory = "Unread Apps";
displayElements($appcategory);
}
else {
displayElements($appcategory);
}
if($_GET['id']) {
$id = $_GET['id'];
executeAppAction($id);
}
if ($_GET['appModeBtn']) {
if($_GET['appMode']=="appon") {
$myFile = "mode.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "yes";
fwrite($fh, $stringData);
fclose($fh);
header("Location: admin.php");
}
if($_GET['appMode']=="appoff") {
$myFile = "mode.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "no";
fwrite($fh, $stringData);
fclose($fh);
header("Location: admin.php");
}
}
}
else {
echo "Incorrect Password";
}
}
else {
header("Location: login.html");
}
?>
</body>
</html>
Hosted Locally:
Hosted Online:
Yes, I am running PHP on the website version.
please add
var_dump(file_exists('includes.php'));
just before
require('includes.php');
require drops any further code if the file is not found
I'm trying to validate a form of a test. I get an error in answer.php Basically I want to validate that each question has been answered.
The form:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY q_nr";
$result1=mysql_query($sql1);
echo "<form method='post' name='form1' action='answer.php'>";
while($row1 = mysql_fetch_array($result1))
{
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "<P><strong>$q_nr $question</strong><BR>";
echo "<BR>";
echo "</p>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Submit' name='Submit'>";
echo "</form>";
answer.php
foreach($_POST['question'] as $key => $ans) {
if ($ans[] = '') {
echo "answer is empty";
}
}
I get the error: Warning: Invalid argument supplied for foreach() in ......
One thing is that you are assigning the answer rather than checking it, use ==
foreach($_POST as $key => $ans) {
if ($ans == '') {
echo "answer is empty";
}
}
and instead of using
name='question[$q_nr]'
I would use for the radio fields
name='question_{$q_nr}'
and for the checkboxes
name='question_{$q_nr}[]'
On answer.php you should be able to do a print_r($_POST) to check what you are getting.
This is probably because your $_POST['question'] is empty. This is what happens when you try to do this with an empty array.
Whereas your HTML says: name='question[$q_nr]'.
Print the values in the array to see what it contains, use print_r.
Edit: $_POST['question'] IS NOT an array! While $_POST IS an array...
Maybe you should try to do something like this:
foreach ($_POST as $key => $value)
Or do it however you want the result to be displayed.