Hello,

I have a question/

I want to get the selected value from a form without submit, and then to use this selected values in another php file.

i think if i can put the values in array, and then to register the array on a session, and the to use this array ob the another php file, but it doesn"t working.

this is part of the code:

$i=0;
$arrayAns = Array();
while ($row = mysql_fetch_array($result))
{
	echo "<form action='' name='myForm' id='myForm' method='post'>   <table dir='rtl' bgcolor='white' border='1' width='90%'>";
	echo "<tr><th width='10%'>question number: " . $row['ques_no'] . "</th>";
	echo "<td>the question: " . $row['ques_body'] . "</td></tr>";
	echo "<tr><th width='10%'>1:</th><td>" . $row['ansr1'] . "</td></tr>";
	echo "<tr><th width='10%'>2:</th><td>" . $row['ansr2'] . "</td></tr>";
	echo "<tr><th width='10%'>3:</th><td>" . $row['ansr3'] . "</td></tr>";
	echo "<tr><th width='10%'>4:</th><td>" . $row['ansr4'] . "</td></tr>";
	echo "<tr><th width='10%'>choose answer:</th><th align='right'><select name='corr'>
							<option value='1'>1</option>
							<option value='2'>2</option>
							<option value='3'>3</option>
							<option value='4'>4</option>
					</select></th></tr>";
	echo "</table></form>";
	$arrayAns[i]=$_POST['corr'];
	$i++;
}

session_register($arrayAns);

and this is part of the another php file code:

$sql = "SELECT * FROM $tbl_name WHERE exam_no='$examnum' and chapter_no='$chap'";


$result = mysql_query($sql);
$quest_num=0;
$answers=$_SESSION[$arrayAns];
while ($row = mysql_fetch_array($result))
{
	$ques_num = $row['ques_no'];
	$corr_ans = $row['correct_ansr'];
	$user_ansr=$answers[$quest_num];
	$query = "INSERT INTO $tbl_name2 VALUES ('$user','$examnum','$chap','$ques_num','$corr_ans','$user_ansr')";
	mysql_query($query) or die(mysql_error());
	$quest_num++;
}
$_SESSION['chap']++;
header("location:exam.php");

I hope to help me, Thank you :)

Member Avatar for diafol

In general, if you want to transfer a value from one php file to another, you have a few choices:

use an include file
pass it via form (usu. post)
pass it via querystring (url)
pass it via cookie
pass it via session

however, Ajax allows you to send data from one php file to the next without a page refresh. In fact it's a cheat - data is passed in pretty much the same way, but it requires js to 'trigger' the process.

A typical implementation would be:

user changes a value in a form widget -> js run from an onchange or onclick event.
js gets values from the form and passes them onto a php script (via get or post)
php gets data, does something with it, eg DB modification, calculation etc and usually return some data, which needs to be echoed in the script.
js picks up this data and passes it back to the page, typically updating a section with new data.

jQuery can make ajax relatively painless.

Thank you for your reply.

Can you give me a simple example to understand you well.

and, I use the session, as you see in my code, but it didn"t work, what's wrong in the code ?

Member Avatar for diafol
$query = "INSERT INTO $tbl_name2 VALUES ('$user','$examnum','$chap','$ques_num','$corr_ans','$user_ansr')";

if you use this syntax, it's generally safer to include the fieldnames list within rounded brackets after the tablename.

$arrayAns[i]=$_POST['corr'];

won't do anything until the form is POSTED. So selecting from the dropdown won't do anything. If you need that functionality, you need to use the onchange event linked to an ajax call (if you need to run something on the server).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.