Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

assigning values to html elements from fields of a database

Codes here !

Moderators: macek, egami, gesf

assigning values to html elements from fields of a database

Postby ayushigarg » Tue Jul 09, 2013 5:43 am

I have a database table called quiz(quest ,op1,op2,op3,op4,answer,num), all fields except num are of type varchar ,num being the primary key. now in a php page i need the question(<p> element) to be dynamically retrieved as quest field, four radio buttons to have their values assigned by the op(n) fields of this database. Please explain how i can do that.
html:
Code: Select all
<!DOCTYPE HTML>
<html>
<head>
<script language="javascript">
function loadXMLDoc()
{
var q=Math.floor((Math.random()*3)+1);
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   [color=#8000FF]document.getElementById("op1").innerHTML=xmlhttp.responseText;
   document.getElementById("op2").innerHTML=xmlhttp.responseText;
   document.getElementById("op3").innerHTML=xmlhttp.responseText;
   document.getElementById("op4").innerHTML=xmlhttp.responseText;[/color]//this is the code i need help //with
   }
  }
xmlhttp.open("GET","questions.php?num="+q,true);
xmlhttp.send();
}
</script></head>
<h1 align='center'><b>Object oriented programming-#1</b></h1>
<h2 align='center'>Supriya Raheja<h2>
<title>quiz has started</title>
<body>
<div style="text-align:left">
<table border="1">
<tr>
<td width="25%"><table border="0">
<tr>
<td><button type="button">Goto Q1</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q2</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q3</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q4</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q5</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q6</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q7</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q8</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q9</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q10</button></td>
<td><input type="checkbox">
</tr>
<tr>
<td><button type="button">Goto Q11</button></td>
<td><input type="checkbox">
</tr>
</td>
</table>
<td width="1000px">

<input type="radio" name="op1" value="" ></br>
<input type="radio" name="op2" value="" ></br>
<input type="radio" name="op3" value="" ></br>
<input type="radio" name="op4" value="" ></br>
<button style="text-align:bottom" type="button" onclick="loadXMLDoc()">Change

Content</button>
</td>
</tr>
</table>
</body>
</html>

php
Code: Select all
<?php
$q=$_GET["q"];

$con = mysqli_connect('localhost','root','ayushigarg','quiz4');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"quiz4");
$sql="SELECT * FROM quiz WHERE num = '".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='1'>


$row = mysql_fetch_array($result)
 
   echo '{"questionTxt" : "' . $row['quest'] .
        '", "answ1" : "' . $row['op1'] .
        '", "answ2" : "' . $row['op2'] .
        '", "answ3" : "' . $row['op3'] .
        '", "answ4" : "' . $row['op4'] .
        '", "hint" : "' . $row['hint'] . '"}';



mysqli_close($con);
?>
?>


i want these answ1 , answ2 etc values to be assigned to op1, op2 etc values whecn i click on change content button... pls help
ayushigarg
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jul 08, 2013 5:25 am

Re: assigning values to html elements from fields of a datab

Postby johnj » Tue Jul 09, 2013 7:38 pm

you have $row = mysql_fetch_array($result). Send this back to the html page and display the values.

For e.g. if you were sending the result row as a string like this "Which of the following is the capital of India#Mumbai#Chennai#New Delhi#Pune#New Delhi#2" then once you have received this string in your calling html page, split this string (maybe using explode()) and assign each of the values to the appropriate fields.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.