Avatar of mattibutt
mattibutt
Flag for United States of America asked on

insert option value to database after posting

hi
i want to insert user selected option value in the database  

i have wrote the following script
 $query      = "insert into codes  VALUES ('','". $_SESSION['quiz_data'][$i]['option3']."','".$_SESSION['quiz_grade']."')"; $sql = mysql_query($query);?>
i can do manual insertion but i want to insert user response            

<?php
 
    echo $_SESSION["current_question"].','.$_SESSION['quiz_total_question'];
 
?>
 
<form id="quiz_submit" name="quiz_submit" action="index.php?menu=<?php if ($_SESSION["current_question"] == (int)$_SESSION['quiz_total_question']){echo 'finish';}else{echo 'process';} ?>" method="post">
<table width="100%" align="center">
<tr>
<td width="10%" bgcolor="#CC99FF"  height="30" align="center" style="border-width:1px"><b>Quiz ID :</b> <?php echo $_SESSION['quiz_id']; ?></td>
<td width="10%" bgcolor="#CC99FF"  height="30" align="center" style="border-width:1px"><b>Name :</b><span id="name_js"><?php echo $_SESSION['full_name'];  ?></span><input type="hidden" name="full_name" id="full_name" value="<?php echo $_SESSION['full_name'];  ?>" /></td>
<td width="10%" bgcolor="#CC99FF"  height="30" align="center" style="border-width:1px"><b>Category :</b> <?php echo $_SESSION['quiz_cat1']; ?></td>
<td width="10%" bgcolor="#CC99FF"  height="30" align="center" style="border-width:1px"><b>Questions :</b> <?php echo $_SESSION['quiz_total_question']; ?></td>
<td width="10%" bgcolor="#CC99FF"  height="30" align="center" style="border-width:1px"><b>Time : </b><?php echo $_SESSION['quiz_time_min']; ?> min</td>
 
</tr>
 
</table>
 
<table  width="auto"  align="center">
<tr><td bgcolor="#99FF33" height="50"><font size="2" color="#FF6600" face="Comic Sans MS" style="padding-left:10" >Time Remaining : </font><font size="5" color="#000000" style="padding-left:10" >
  <script language="JavaScript">
 
var countDownInterval=<?php echo $_SESSION['quiz_time_sec']; ?>;
//configure width of displayed text, in px (applicable only in NS4)
var c_reloadwidth=200
 
</script>
<!--   /////////////////////////// Floating               -->
 
<ilayer id="c_reload" width=&{c_reloadwidth}; ><layer id="c_reload2" width=&{c_reloadwidth}; left=0 top=0 ></layer></ilayer>
 
 
<script>
if (!document.layers)
document.write('<div id="divStayTopLeft" style="position:absolute">')
</script>
 
<layer id="divStayTopLeft">
 
<!--EDIT BELOW CODE TO YOUR OWN MENU-->
<table  width="80" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%"  bgcolor="#9900FF">
      <p align="center"><b><font size="4" color="#FF9900">Timer</font></b></td>
  </tr>
  <tr>
    <td width="100%" bgcolor="#FFFFFF">
<input type="text" id="quiz_time_float" readonly="" size="4"  value="" style=" text-align:center; font-size:20px; font-stretch:expanded; background-color:#66FF00" />
	  </td>
  </tr>
</table>
<!--END OF EDIT-->
 
</layer>
 
 
<script type="text/javascript">
 
 
//Enter "frombottom" or "fromtop"
var verticalpos="frombottom"
 
if (!document.layers)
document.write('</div>')
 
function JSFX_FloatTopDiv()
{
	var startX = 3,
	startY = 150;
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
	function ml(id)
	{
		var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
		if(d.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x;this.style.top=y;};
		el.x = startX;
		if (verticalpos=="fromtop")
		el.y = startY;
		else{
		el.y = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
		el.y -= startY;
		}
		return el;
	}
	window.stayTopLeft=function()
	{
		if (verticalpos=="fromtop"){
		var pY = ns ? pageYOffset : document.body.scrollTop;
		ftlObj.y += (pY + startY - ftlObj.y)/8;
		}
		else{
		var pY = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
		ftlObj.y += (pY - startY - ftlObj.y)/8;
		}
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml("divStayTopLeft");
	stayTopLeft();
}
JSFX_FloatTopDiv();
</script>
 
 
 
<script>
 
var countDownTime=countDownInterval+1;
function countDown(){
countDownTime--;
 
if (countDownTime <= 0){
//countDownTime=countDownInterval;
clearTimeout(counter)
document.getElementById("quiz_time_unused").value = countDownTime;
document.quiz_submit.submit();
return
}
if (document.all) //if IE 4+
document.all.countDownText.innerText = countDownTime+" ";
else if (document.getElementById) //else if NS6+
document.getElementById("countDownText").innerHTML=countDownTime+" "
else if (document.layers){ //CHANGE TEXT BELOW TO YOUR OWN
document.c_reload.document.c_reload2.document.write('<font id="countDownText" size="10" color="red"> '+countDownTime+' </font>');
 
document.c_reload.document.c_reload2.document.close()
}
counter=setTimeout("countDown()", 1000);
document.getElementById("quiz_time_float").value=countDownTime;
}
 
function startit(){
if (document.all||document.getElementById) //CHANGE TEXT BELOW TO YOUR OWN
document.write('<b id="countDownText">'+countDownTime+' </b>')
countDown()
}
function save_unused_time()
{
	document.getElementById("quiz_time_unused").value = countDownTime;
}
/*var name;
if(name=prompt("Name"))
{
document.getElementById("name_js").innerHTML=name;
document.getElementById("full_name").value=name;
*/
if (document.all||document.getElementById)
startit()
else
window.onload=startit
//}
</script></font><font size="2" color="#FF6600" face="Comic Sans MS" style="padding-right:10" >seconds</font></td>
</tr>
</table>
 
<input type="hidden" name="quiz_time_unused" id="quiz_time_unused" value="" />
 
<?php
 
//for($i=0;$i<$_SESSION['quiz_total_question'];$i++)
//{
 
$i = $_SESSION["current_question"] - 1;
 
?>
<table width="80%" align="center" >
<tr>
<td width="5%" bgcolor="#CC99FF" align="center">Q.<?php echo $i+1; ?></td>
<td width="95%" bgcolor="#CC99FF" style="padding-left:10"><b><?php echo $_SESSION['quiz_data'][$i]['question']; ?></b></td>
</tr>
<tr>
<td width="5%" >&nbsp;</td>
<td width="95%">
<table width="100%" >
<tr>
<td width="5%" bgcolor="#CCCCFF" style="padding-left:5">a.</td>
<td width="5%"  align="center" bgcolor="#CCCCFF"><input type="radio" name="<?php echo $i+1; ?>" value="option1" /></td>
<td width="35%" bgcolor="#CCCCFF" style="padding-left:5"><?php echo $_SESSION['quiz_data'][$i]['option1']; ?></td>
<td width="10%">&nbsp;</td>
<td width="5%"  style="padding-left:5">b.</td>
<td width="5%"  align="center"><input type="radio" name="<?php echo $i+1; ?>" value="option2" /></td>
<td width="35%"  style="padding-left:5"><?php echo $_SESSION['quiz_data'][$i]['option2']; ?></td>
</tr>
<tr>
<td width="5%" style="padding-left:5">c.</td>
<td width="5%"  align="center"><input type="radio" name="<?php echo $i+1; ?>" value="option3" /></td>
<td width="35%" style="padding-left:5"><?php echo $_SESSION['quiz_data'][$i]['option3']; ?></td>
<td width="10%">&nbsp;</td>
<td width="5%" style="padding-left:5" bgcolor="#CCCCFF">d.</td>
<td width="5%"  align="center" bgcolor="#CCCCFF"><input type="radio" name="<?php echo $i+1; ?>" value="option4" /></td>
<td width="35%" style="padding-left:5" bgcolor="#CCCCFF"><?php echo $_SESSION['quiz_data'][$i]['option4']; ?></td>
</tr>
 
</table>
</td>
</tr>
 <?php
 $i = $_SESSION["current_question"];
 $query	= "insert into codes  VALUES ('','". $_SESSION['quiz_data'][$i]['option3']."','".$_SESSION['quiz_grade']."')"; $sql = mysql_query($query);?>
			
</table>
<?php
 
 
//}
?>
<table align="center" width="80%">
<tr>
<td  bgcolor="#99FF00" align="center"><input type="image" src="images/finish.jpg" onclick="save_unused_time()" /></td>
</tr>
</table>
 
 
 
 
 
</form><?php // DUMP VARIABLES
//echo "<pre>\n";
//echo "\nSESSION:";
//var_dump($_SESSION);
//echo "\n\nPOST:";
//var_dump($_POST);
//echo "\n</pre>";
	echo $query;
	echo $_SESSION["current_question"];
 
//?>

Open in new window

PHP

Avatar of undefined
Last Comment
mattibutt

8/22/2022 - Mon
Unspoken1

If you are wanting to put the selected radio button use something like this perhaps.
 
if (isset($_POST['radiobutton'])) {
$selected_radio = $_POST['radiobutton'];

}
//create a new mysql column for insertion
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...WHERE username='$username');
INSERT INTO only works the first time, after that you have to use a mysql update query.
$result = mysql_query("UPDATE column SET radio='$radiovalue' WHERE username='$username'") or die(mysql_error());  
mattibutt

ASKER
hi
thanks for getting back i don't want to get all the values i only want the selected value of radio button and each record will be different
mattibutt

ASKER
these are option values when user select one and click on the button then i want to write the selected value inside the table


<td width="5%" bgcolor="#CCCCFF" style="padding-left:5">a.</td>
<td width="5%"  align="center" bgcolor="#CCCCFF"><input type="radio" name="<?php echo $i+1; ?>" value="option1" /></td>
<td width="35%" bgcolor="#CCCCFF" style="padding-left:5"><?php echo $_SESSION['quiz_data'][$i]['option1']; ?></td>
<td width="10%"> </td>
<td width="5%"  style="padding-left:5">b.</td>
<td width="5%"  align="center"><input type="radio" name="<?php echo $i+1; ?>" value="option2" /></td>
<td width="35%"  style="padding-left:5"><?php echo $_SESSION['quiz_data'][$i]['option2']; ?></td>
</tr>
<tr>
<td width="5%" style="padding-left:5">c.</td>
<td width="5%"  align="center"><input type="radio" name="<?php echo $i+1; ?>" value="option3" /></td>
<td width="35%" style="padding-left:5"><?php echo $_SESSION['quiz_data'][$i]['option3']; ?></td>
<td width="10%"> </td>
<td width="5%" style="padding-left:5" bgcolor="#CCCCFF">d.</td>
<td width="5%"  align="center" bgcolor="#CCCCFF"><input type="radio" name="<?php echo $i+1; ?>" value="option4" /></td>
<td width="35%" style="padding-left:5" bgcolor="#CCCCFF"><?php echo $_SESSION['quiz_data'][$i]['option4']; ?></td>

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Unspoken1

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mattibutt

ASKER
hi Unspoken1
i think i have not explained to you properly what i want to acheive
i have a quiz which may have 10 questions in each quiz
the user will respond to each question and each question answer will be stored in the database
for instance if user has taken 10 questions then the ten different records will be inserted in the database
something like this
quizid questionid userresponse

right now i am stuck because i am not able to get the post value for each question i have tried it several different things but it doesn't work
so all i want to be able to do is
insert each question response as a seperate record
thanks

mattibutt

ASKER
sorry but i didnt get any help from you
Unspoken1

do you have access to phpmyadmin? create a different column...
use post like this
$_['POST']['htmlnamehere'];
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Unspoken1

I wrote the post wrong. $_POST['radiobutton'];
... If you are unable to get the $_POST vars in your php script you need to make sure your html input tags have the name attribute... this is what is used to name the post variable when the form is submitted.
mattibutt

ASKER
hi buddy
i have ressolved my problems