MYSQL post query - not working need advise - php

Can someone take a look at this and maybe see why this is not updating my database?
It is a form that pulls data from my database and when I click the update button it SHOULD update the database, but it dosn't.
Is there a way to show if it is returning a error?
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Beerlist Admin</title>
</head>
<body>
<?php include '../beerlist/config.php'; ?>
<table border="0" width="95%" align="center" cellspacing="0" cellpadding="5">
<tr>
<?php
#///////////////////////////////////
#//////////// ALL BEERS/////////////
#///////////////////////////////////
$sql = "SELECT * FROM bottles ORDER BY name";
$mydata = mysql_query($sql,$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE bottles SET new='$_POST[new]', name='$_POST[name], style='$_POST[style], location='$_POST[location], size='$_POST[size], abv='$_POST[abv], number='$_POST[number], price='$_POST[price]' WHERE name='$_POST[hidden]'";
mysql_query ($UpdateQuery, $con);
};
while($record = mysql_fetch_array($mydata)){
echo "<form action=beerlist_admin.php method=post>";
echo "<td><input size=7 type=text name=new value=\"" . $record['new'] . "\" > ";
echo "<input type=text size=50 name=name value=\"" . $record['name'] . "\" >";
echo "<input type=text size=20 name=style value=\"" . $record['style'] . "\" >";
echo "<input type=text size=20 name=location value=\"" . $record['location'] . "\" >";
echo "<input type=text size=7 name=size value=\"" . $record['size'] . "\" >";
echo "<input type=text size=5 name=abv value=\"" . $record['abv'] . "\" >";
echo "<input type=text size=5 name=number value=\"" . $record['number'] . "\" >";
echo "<input type=text size=7 name=price value=\"" .$record['price'] . " \" >";
echo "<input type=hidden name=hidden value=" . $record['number'] . "\"> <input type=submit name=update value=update></td></tr>";
echo "</form>";
}
echo "</table>"
?>
</body>
</html>

First and foremost, mysql_* has been deprecated and you should consider migrating to MySQLi or PDO and be prepared for when mysql_* gets removed so your site does not stop working.
Your code is wide open to SQL Injection you should use mysql_real_escape_string($variable) to prevent that.
With a combination of sprintf you can further define what kind of variable you are expecting, for example:
%s - is meant to be used for string data.
%d - is meant to be used for numbers, and presented as a (signed) decimal number.
On your the below example I've used only %s as I am unsure of the data you're trying to insert and your database table field types.
Your UPDATE query is poorly written and may cause issues in different cases depending on the data you provide it with so you should always be using the mysql_error at least during the development process so you can debug this sort of errors and once you move to a live site make those errors get logged silently to file instead.
You don't need to always use echo to print every little piece of HTML, see how I've changed your WHILE code.
Also the way you're using the form within the table while it may work its not the correct way.
Since you have not posted the contents of config.php I can't tell if there is any issues in there as well.
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Beerlist Admin</title>
</head>
<body>
<?php
$con = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$con)
{
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('beerlist', $con);
if (!$db_selected)
{
die ('Can\'t use foo : ' . mysql_error());
}
?>
<table border="0" width="95%" align="center" cellspacing="0" cellpadding="5">
<?php
if (isset($_POST['update']))
{
$UpdateQuery = sprintf("UPDATE bottles
SET new='%s',
name='%s',
style='%s',
location='%s',
size='%s',
abv='%s',
number='%s',
price='%s'
WHERE name='%s'",
mysql_real_escape_string($_POST['new']),
mysql_real_escape_string($_POST['name']),
mysql_real_escape_string($_POST['style']),
mysql_real_escape_string($_POST['location']),
mysql_real_escape_string($_POST['size']),
mysql_real_escape_string($_POST['abv']),
mysql_real_escape_string($_POST['number']),
mysql_real_escape_string($_POST['price']),
mysql_real_escape_string($_POST['hidden']));
if (!mysql_query($UpdateQuery))
{
die('Invalid query: ' . mysql_error());
}
}
$sql = "SELECT *
FROM bottles
ORDER BY name";
$mydata = mysql_query($sql);
if (!$mydata)
{
die('Invalid query: ' . mysql_error());
}
while($record = mysql_fetch_array($mydata))
{
?>
<form action="beerlist_admin.php" method="post">
<tr><td>
<input size="7" type="text" name="new" value="<?php echo $record['new']; ?>">
<input type="text" size="50" name="name" value="<?php echo $record['name']; ?>">
<input type="text" size="20" name="style" value="<?php echo $record['style']; ?>">
<input type="text" size="20" name="location" value="<?php echo $record['location']; ?>">
<input type="text" size="7" name="size" value="<?php echo $record['size']; ?>">
<input type="text" size="5" name="abv" value="<?php echo $record['abv']; ?>">
<input type="text" size="5" name="number" value="<?php echo $record['number']; ?>">
<input type="text" size="7" name="price" value="<?php echo $record['price']; ?>">
<input type="hidden" name="hidden" value="<?php echo $record['number']; ?>">
<input type="submit" name="update" value="update">
</td></tr>
</form>
<?php
}
?>
</table>
</body>
</html>

Related

simple attendance system with radio button

I am making a simple attendance class system
I have this php code with radio buttons here.. now i put it in a table
I can only choose one radio button on the entire table instead of one radio button per registered account
<?php
session_start();
if(!isset($_SESSION["in"]))
{
header("Location: log.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>Attendance</title>
</head>
<body>
<center>
<br />
<?php
echo "Today is " . date("m/d/Y") . "<br><br><br><br>";
?>
<?php
$conn= new mysqli("localhost", "root", "", "dbform");
$sql = "SELECT * FROM tblusers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table width='1300' border='6'>
<tr>
<th width='100'>ID</th>
<th width='100'>Lastname</th>
<th width='100'>Firstname</th>
<th width='100'>Sex</th>
<th width='100'>Attendance</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr> ";
echo "<td align='center'>2016" . $row['id'] . "</td>";
echo "<td align='center'>" . $row['lastname'] . "</td>";
echo "<td align='center'>" . $row['firstname'] . "</td>";
echo "<td align='center'>" . $row['sex'] . "</td>";
?>
<form method="post" action="Succes_Submit_Attendace.php" name="submit_attendance">
attendance =
<td align='center'> <label><input type="radio" name="attendance" value="present">Present</label>
 <label><input type="radio" name="attendance" value="absent">Absent</label><br /><br />
<?php
$row['attendance'] ;
?> </td>
<?php
echo " </tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
$conn->close();
?>
<br>
<br>
<input type="reset"> <input type="submit" value="Submit Attendance"></h4>
</form>
<div align="right">
<h3>Back</h2>
</div>
All the radio buttons are of same name so you need to group them by row id. Change the input type to below :
<label><input type="radio" name="attendance[<?php echo $row['id']; ?>]" value="present">Present</label>
 
<label><input type="radio" name="attendance[<?php echo $row['id']; ?>]" value="absent">Absent</label><br /> <br />
Also there are HTML errors in your code. Please fix them

Post variables from a second table in form

I'm trying post 3 variables in order to insert them in table2. As you can see I'm using a SELECT to get 3 variables from table1 to insert into a table2 but can't see the input value when looking at the source.
The loop works. I get results in the loop but the input post echo's nothing. I've tried many different ways but can't seem to get it to work. Can someone help?
<?php
include('theconnection.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('cannot connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thebooks");
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
?>
<input type="hidden" name="name" id="name" value="<?php echo "{$_POST['name']}"; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo "{$_POST['cover']}"; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo "{$_POST['pageno']}"; ?>">
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
I don't see why there are two queries. You can just drop the first one. Also, I'd like to suggest you this-
Instead of echoing like this
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
echo it like this-
while($row = mysqli_fetch_array($result))
{
echo "<input type='text' value='" . $row[name] . "' name='name' id='name'>";
echo "<input type='text' value='" . $row[cover] . "' name='cover' id='cover'>";
echo "<input type='text' value='" . $row[pageno] . "' name='pageno' id='pageno'>";
}
In your last bit of code, please make these small changes-
<input type="hidden" name="name" id="name" value="<?php echo $_POST['name']; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo $_POST['cover']; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo $_POST['pageno']; ?>">
Please clarify- why would you echo input controls in a loop? You could potentially get hundreds of input controls rendered on your page. Now you don't want that do you? Hope my answer helps.

Why won't my variables pass along each page? Php

I can't get my variable "quiz_name" passed along several pages in order to make a quiz. After the "quiz_name" variable is sent from question_menu.php as a $_POST[] to quiz_created.php for processing the quiz_created.php is suppose to send the variable back to question_menu.php. What am I doing wrong. To be honest I think I'm approaching it wrong.
Reworded:
quiz_name = "How to score a Basket" which is submitted to question_menu.php as a $_POST['quiz_name']. Then is submitted a to quiz_created.php as a $_POST['quiz_name'] and then submitted back to question_menu.php.
I get an Variable quiz_name undefined error after it is sent back to quesion_menu.
coach_quizzes.php
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Current Quizzes </h1>
<form name="submit_button" action="create-quiz.php">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
submit to -> create_quiz.php
<?php session_start()
?>
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Enter Quiz Name </h1>
<form action="questions_menu(test).php" method="post">
<input type="text" name="quiz_name" maxlength="30" size="30">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
Enter: "How to score a Basket" then submit to question_menu.php
<!DOCTYPE html>
<html>
<head>
<title>Add Question</title>
<link rel="stylesheet" type="text/css" href="css/info_style.css" />
</head>
<body>
<div id="main">
<header>
<div id="welcome">
<h2>Prairie View A&M University</h2>
</div><!--close welcome-->
</header>
<div id="site_content">
<form enctype="multipart/form-data" method="post" action="quiz_created.php">
<table border="0" name"form_table">
<tr>
<td>Quiz Name</td>
<td><?php echo $_POST['quiz_name']?></td>
</tr>
<tr>
<td>Question</td>
<td><textarea name="description" rows="4" cols="50"></textarea></td>
</tr>
<tr>
<td>Option 1</td>
<td><input type="text" name="option1" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 2</td>
<td> <input type="text" name="option2" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 3</td>
<td><input type="text" name="option3" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Answer</td>
<td>
<select name="dropdown">
<option value='option1'>Option 1</option>
<option value='option2'>Option 2</option>
<option value='option3'>Option 3</option>
</select>
</td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="file" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000" /><br>
</td>
</tr>
<tr>
<td colspan="2"><p>
<input type="submit" value="Add Question">
</p></td>
</tr>
</table>
</form>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
//echo "Connected to mysql<br>";
mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
//update when update button pressed
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};//end of if statement
//delete when delete button pressed
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};//end of if statement
$mysql = "SELECT * FROM $table";
$mydata = mysql_query($mysql,$con);
//create table
echo "<table border=1
<tr>
<th>Question ID</th>
<th>Quiz Name</th>
<th>Question Description</th>
<th>Option 1</th>
<th>Option 2</th>
<th>Option 3</th>
<th>Answer</th>
<th>Picture</th>
</tr>";
//insert data into rows
while($records = mysql_fetch_array($mydata)){
echo "<form action=questions_menu(test).php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['quiz_name']." </td>";
echo "<td>"."<textarea name=description rows=1 cols=25>".$records['question_description']."</textarea>"."</td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>";
echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>";
echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>";
echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>";
echo "<td>". $records['image'] ." </td>";
echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>";
//update button
echo "<td>"."<input type=submit name=update value=Update onclick='return confirm(\"Are you sure you want to update question?\")'>"." </td>";
//delete button
echo "<td>"."<input type=submit name=delete value=Delete onclick='return confirm(\"Are you sure you want to delete question?\")'>"." </td>";
echo "</tr>";
echo "</form>";//end form
} echo "</table>";
mysql_close();
?> <!-- End of php code-->
</div><!--close site_content-->
<footer>
Home | Photos | Videos | Schedule | Contact<br/><br/>
</footer>
</div><!--close main-->
</body>
</html>
after the information is added submit value= "Add Question" information is sent to quiz_created.php:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";
$db = mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
$mysql = "INSERT INTO $table(question_description, quiz_name, option_a, option_b, option_c, answer) VALUES('$_POST[description]','$_POST[quiz_name]','$_POST[option1]','$_POST[option2]','$_POST[option3]','$_POST[dropdown]')";
if(!mysql_query($mysql))
echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
//die("Disconnected");
$quiz=$_POST['quiz_name'];
//Upload images
if($_POST)
{
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$image_loc=addslashes (getcwd()."/uploaded"."/".$_FILES["file"]["name"]);
$query_image = "insert into $table (image, img_location) values ('".$_FILES['file']['name']."', '$image_loc')";
if(mysql_query($query_image))
{
echo "Stored in: " . "uploaded/" . $_FILES["file"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}
}
}
//end of image upload
mysql_close();
?>
<html>
<title>User Added</title>
<body>
<h2>
Question has been added!
</h2>
</body>
<form action="questions_menu(test).php" method="post">
<input type="submit" value="Add Another Question">
<input type="hidden" name="quiz_name" value='<?php echo $quiz;?>'>
</form>
</html>
then finally sent back to question_menu.php but I get an Variable quiz_name undefined error.
I hope I can get help. Thank You.
I see many problems, but one is that you're not escaping your php inserts properly.
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
should be:
echo '<td><input type="text" name="questionID" size="5" value="'.$records['question_id'].'" /></td>';
The way you have it now, you are using the quotes for the echo and there are none for the value="". This is seen all throughout your code. Not sure where you are getting your $records array either, but that's for you to figure out.
These lines are problematic too.
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
One major problem is you're trying to set raw POST data into a database. That is a HUGE security flaw. Other problems, like the rest of your php, have to do with quotes and double quotes, semicolons, etc... Read up on syntax. Your whole script is basically wrong.

Characters not displaying after space form MySQL resultset

I am a fresher in PHP, I am trying to create a form which will be auto filled from the MySQL database. The form is getting created, and populated from the database data. But, in case the data contains a space in between, the characters after the space are not getting displayed.
The php page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
// Check if we have parameters employeeId being passed to the script through the URL
if (isset($_GET["employeeId"])) {
$employeeId = $_GET["employeeId"];
//=============Data Display=================
$con=mysqli_connect("localhost","root","","employee_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT `employee_id` , `employee_name` , `employee_email` , `employee_mobile` , `employee_address`
FROM `employee_details`
WHERE `employee_id` =$employeeId");
echo "<h2>Edit Employee</h2><br/>
<form name='myForm' action='' onSubmit='return validateForm()' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "Employee Id: ". $row['employee_id'] ."<br/>";
echo "Employee Name: <input type='text' name='EmployeeName' value=". $row['employee_name'] ."><br/>";
echo "Employee Email: <input type='text' name='EmployeeEmail' value=". $row['employee_email'] ."><br/>";
echo "Employee Mobile: <input type='text' name='EmployeeMobile' value=". $row['employee_mobile'] ."><br/>";
echo "Employee Address: <Input type='text' name = 'product_name5' value=".$row['employee_address']."><br/>";
}
mysqli_close($con);
//=============Data Display=================
}
?>
</body>
</html>
Where am I going wrong? What should I do to get all the data including spaces in the textboxes?
Value attribute of inputs should be with quotes too:
value='" . $value . "'
In your case:
echo "Employee Name: <input type='text' name='EmployeeName' value='". $row['employee_name'] ."'><br/>";
echo "Employee Email: <input type='text' name='EmployeeEmail' value='". $row['employee_email'] ."'><br/>";
echo "Employee Mobile: <input type='text' name='EmployeeMobile' value='". $row['employee_mobile'] ."'><br/>";
echo "Employee Address: <Input type='text' name = 'product_name5' value='".$row['employee_address']."'><br/>";
If you are going to create link fetch from database like download link
use this type code
$result = mysqli_query($con,$sql);
//echo $ip."<br />";REGEXP
//echo $name."<br />";
echo "<table border=2px style='border-radius=20px;' align=center><tr>
<th>Document ID</th>
<th>Document Name Type</th>
<th>Download Documents</th>
</tr>";//<th>Project Document Type</th>
while($row = mysqli_fetch_array($result)) {
$path1=$row['FOLDERNAME'] .'/'. $row['FILENAME'] .'.'. $row['DOCTYPE'];
$path=str_replace(" ", '%20', $path1);
echo "<tr>";
echo "<td>" . $row['DocID'] . "</td>";
// echo "<td>" . $row['PROJDOCTYPE'] . "</td>";Thank you. Your Apple ID is now ready for use.
echo "<td>" . $row['DOCNAME'] . "</td>";
echo '<td><a href=Tender/'.$path.'>'.$row['DOCNAME'].'</a></td>';
echo "</tr>";
}
echo "</table>";
mysqli_close($con);

keep data in form when clicking submit

EDIT: Apologies - all code now pasted below
Apologies for the first time newbie question - I've been looking for an answer here and on google - I get the feeling this is some simple coding i'm messing up.
I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish.
I have a section of the form where this doesn't work however - where you can ask multiple questions and each question can have up to 4 answers.
<!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">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<title>Test page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="stylenewtest.css" type="text/css" />
<script language="javascript" src="tableadd.js"></script>
</head>
<body>
<?php
$string = isset($_POST['quiz_format']) ? $_POST['quiz_format'] : false;
$quiz_format = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $string);
$stringquiz_100 = isset($_POST['quiz_100']) ? $_POST['quiz_100'] : false;
$quiz_100 = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_100);
$stringquiz_fback = isset($_POST['quiz_fback']) ? $_POST['quiz_fback'] : false;
$quiz_fback = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_fback);
$stringquiz_title = isset($_POST['quiz_title']) ? $_POST['quiz_title'] : false;
$quiz_title = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_title);
$stringquiz_instruct = isset($_POST['quiz_instruct']) ? $_POST['quiz_instruct'] : false;
$quiz_instruct = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\?,\"\'\._#\\/ !&()=\+;:\t\r\n-]", "", $stringquiz_instruct);
$stringquiz_time = isset($_POST['quiz_time']) ? $_POST['quiz_time'] : false;
$quiz_time = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_time);
?>
<div id="content">
<p><span class="error">* required field.</span></p>
<form method="post" action="">
<div class="datagrid">
<table>
<tr><th colspan="2">Please choose the format of the quiz<span class="error">*</span></th></tr>
<tr><td>Hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Hangman") echo "checked";?> value="Hangman"></td></tr>
<tr><td>Gap fill</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Gap fill") echo "checked";?> value="Gap fill"></td></tr>
<tr><td>Multiple choice</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Multiple Choice") echo "checked";?> value="Multiple choice"></td></tr>
<tr><td>Simple Q & A</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Simple Q & A") echo "checked";?> value="Simple Q & A"></td></tr>
<tr><td>Word formation hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Word formation hangman") echo "checked";?> value="Word formation hangman"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Continue repeating the exercise until 100% achieved?<span class="error">*</span></th></tr>
<tr><td>Yes</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="Yes") echo "checked";?> value="Yes"></td></tr>
<tr><td>No</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="No") echo "checked";?> value="No"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Show the answer as correct/incorrect after every question or just at the end?<span class="error">*</span></th></tr>
<tr><td>After every question</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="After every question") echo "checked";?> value="After every question"></td></tr>
<tr><td>Just at the end</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="Just at the end") echo "checked";?> value="Just at the end"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Please enter in the title of the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz title:</td><td><input type="text" name="quiz_title" value="<?php echo $_POST[quiz_title];?>"></td></tr>
</table>
<br />
<table>
<tr><th colspan="2">Please enter in the instructions for the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz instructions:</td><td><textarea name="quiz_instruct" rows="10" cols="40"><?php echo $_POST[quiz_instruct];?></textarea></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<script language="javascript">
window.onload = moreFields;
window.onload = init;
</script>
<table>
<tr><th colspan="5">Please enter in the quiz's questions and answers:<span class="error">*</span></th></tr>
</table>
<div id="readroot" style="display: none">
<input type="button" value="Remove a question field"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a4'][$i] ?>"></td>
</tr>
</table>
</div>
<span id="writeroot"></span><input type="button" id="moreFields" value="Click here to add further question fields" /><br />
</div>
<br />
<div class="datagrid">
<table>
<tr><td><input type="submit" name="Submit1" value="Submit"/></td></tr>
</table>
</div>
</form>
<?php
if ( isset( $_POST['Submit1'] ) ) {
echo "<form method=post action=insert.php>";
echo "<h1>Please confirm the quiz details are correct</h1>";
echo "<div class=datagrid>";
echo "<table>";
if(empty($string)){
echo("<tr><td>Quiz format:</td><td><h3>Please go back and choose a quiz format<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz format:</td><td>" . $quiz_format . "</td></tr>");
echo("<input type=hidden name=quiz_format value='" . $quiz_format . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td>" . $quiz_100 . "</td></tr>");
echo("<input type=hidden name=quiz_100 value='" . $quiz_100 . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td>" . $quiz_fback . "</td></tr>");
echo("<input type=hidden name=quiz_fback value='" . $quiz_fback . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_title)){
echo("<tr><td>Quiz title:</td><td><h3>Please go back and enter in a title for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz title:</td><td>" . $quiz_title . "</td></tr>");
echo("<input type=hidden name=quiz_title value='" . $quiz_title . "' />");
}
if(empty($stringquiz_instruct)){
echo("<tr><td>Quiz instructions:</td><td><h3>Please go back and enter in instructions for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz instructions:</td><td>" . $quiz_instruct . "</td></tr>");
echo("<input type=hidden name=quiz_instruct value='" . $quiz_instruct . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_time)){
echo("<tr><td>Time limit:</td><td>not specified</td></tr>");
}
else
{
echo("<tr><td>Time limit:</td><td>" . $quiz_time . "</td></tr>");
echo("<input type=hidden name=quiz_time value='" . $quiz_time . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td colspan=5>The questions and answers to your quiz:</td></tr>";
$aq = $_POST['q'];
$aa1 = $_POST['a1'];
$aa2 = $_POST['a2'];
$aa3 = $_POST['a3'];
$aa4 = $_POST['a4'];
$N = count($aq);
for($i=1; $i < $N; $i++){
if($aq[$i]==""){
echo("<tr><td>Question:</td><td><h3>Please go back and enter a question</h3></td></tr>");
$showbutton=1;
}
elseif($aa1[$i]==""){
echo("<tr><td>Answer:</td><td><h3>Please go back and enter at least one answer</h3></td></tr>");
$showbutton=1;
}
else{
echo ("<table><tr><td>Question:</td><td colspan=3>" . $aq[$i]. "<input type=hidden name=q[] value='" . $aq[$i] . "'></td></tr>
<tr><td>Answer 1:</td><td>Answer 2:</td><td>Answer 3:</td><td>Answer 4:</td></tr>
<tr><td>" .$aa1[$i] . " <input type=hidden name=a1[] value='" .$aa1[$i] . "'></td>
<td>" .$aa2[$i] . " <input type=hidden name=a2[] value='" .$aa2[$i] . "'></td>
<td>" .$aa3[$i] . " <input type=hidden name=a3[] value='" .$aa3[$i] . "'></td>
<td>" .$aa4[$i] . " <input type=hidden name=a4[] value='" .$aa4[$i] . "'></td> </tr></table>");
}
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td><input type=button value='<< Go Back' onclick='goBack()' /></td></tr>";
if ($showbutton =="1"){
}
else{
echo "<tr><td><input type=submit value=Submit></td></tr>";
echo "</table>";
echo "</form>";
}
}
?>
</div>
</body>
</html>
but every time I submit, the questions and answers carry through but the form fields lose all the data. How can I keep the data in the fields?
Many thanks in advance
You can simply set the value of the input to either $_POST['old_value_name] or $_GET['old_value_name'] depending on which form method you are using. Therefore, your new code would look something like this (assuming that you are looking for the key $i from your question):
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
</tr>
</table>
As far as I've understood your problem you want to keep values in a form after they were sent to server. "I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish"
It's ok. I remember I wanted the same a few months ago. This is why I tried to help.
There is only a problem that after pressing "submit" button form is sent to server and page is reloaded.
In return you get a new page from webserver which you pointed in the form field "action".
I would do it so:
Target form: <br/>
<form id="target" action="index.php" method='post'>
<input name="first_field" type="text" value="<?php if ( isset($_POST['first_field']) )
echo $_POST['first_field']; ?>" /> <br/>
<input name="second_field" type="text" value="<?php if ( isset($_POST['second_field']))
echo $_POST['second_field'];?>" /> <br/>
<p><b>What kind of girls do you like?:</b><Br>
<input type="radio" name="browser" value="brunette" <?php if( $_POST['browser']==="brunette" ) echo "checked"; ?> > brunette<Br>
<input type="radio" name="browser" value="blonde" <?php if( $_POST['browser']==="blonde" ) echo "checked"; ?> > blonde<Br>
<input type="radio" name="browser" value="red" <?php if( $_POST['browser']==="red" ) echo "checked"; ?> > red<Br>
</p>
<input type="submit" />
</form>
Any ideas how it can be changed/improved are encouraged.

Categories