【问题标题】:assigning values to html elements from fields of a database从数据库的字段中为 html 元素赋值
【发布时间】:2013-07-07 06:30:44
【问题描述】:

我有一个名为 quiz(quest ,op1,op2,op3,op4,answer,num) 的数据库表,除了 num 之外的所有字段都是 varchar 类型,num 是主键。现在在一个 php 页面中,我需要将问题(<p> 元素)作为 quest 字段动态检索,四个单选按钮的值由该数据库的 op(n) 字段分配。请解释我该怎么做。 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

<?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);
?>
?>

当我点击更改内容按钮时,我希望将这些 answ1 、 answ2 等值分配给 op1、op2 等值...请帮助

【问题讨论】:

    标签: php html mysql


    【解决方案1】:
    document.getElementById("op2").innerHTML
    

    设置id为op2的元素的innerHTML,例如:

    <h1 id="op2">inner HTML</h1>
    

    如果你想设置一个字段的值,你需要:

    document.getElementById("op2").value = "new value";
    

    但我建议为此使用 jquery,您将获得更少的痛苦!

    我知道它不是必需的,但为了清楚起见,我可以向您展示它如何与 jquery 一起工作(未经测试):

    <!DOCTYPE html>
    <html>
    <head>
      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <body>
    
    <script>
    
    $(document).ready(function() {
    
        $('#btn1').click(function(){
    
            var q=Math.floor((Math.random()*3)+1);
            var myurl = "questions.php?num="+q;
            $.getJSON(myurl, function(data) {
    
                $('#qs1').html(data.questionTxt);
    
                $('#op1').val(data.answ1);
                $('#aw1').html(data.answ1);
    
                $('#op2').val(data.answ2);
                $('#aw2').html(data.answ2);
    
                $('#op3').val(data.answ3);
                $('#aw3').html(data.answ3);
    
                $('#op4').val(data.answ4);
                $('#aw4').html(data.answ4);
            });
    
        });
    
    });
    
    </script>
    
    <h1 id="qs1"></h1>
    <input type="radio" id="op1" name="op" value="" ><span id="aw1"></span></br>
    <input type="radio" id="op2" name="op" value="" ><span id="aw2"></span></br>
    <input type="radio" id="op3" name="op" value="" ><span id="aw3"></span></br>
    <input type="radio" id="op4" name="op" value="" ><span id="aw4"></span></br>
    <button style="text-align:bottom" type="button" id="btn1" value="go">
    
    </body>
    </html>
    

    【讨论】:

    • document.getElementById("op2").value = "new value";但是我应该写什么而不是 new value 以便将该值选为测验表的 op2 值?我不能在 html 中这样做吗??
    • 我也试过这个,但也不起作用。点击按钮前后没有变化。 :(
    • 如果返回的 json 正常,请查看 firebug 控制台选项卡。我更新了代码,因为收音机的 id 丢失了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2014-09-21
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多