Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

How to do a condition displaying input type based on table column in PHP/Javascript/Mysql?

This is my database table. I'm currently displaying option1-10 as text but I want to display it as an input type for each option depending if its answer_type column is radiobutton or checkbox. It also shouldn't display when the value of option1-10 is null. Also how do i group it into one like for example if it's a radiobutton, i should only be able to choose one? enter image description here

Here's my code of displaying the text

ajax

<html>
<head>
    <script>
        function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else {
                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 (this.readyState == 4 && this.status == 200) {
                        document.getElementById("txtHint").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET","hay.php?q="+str,true);
                xmlhttp.send();
            }
        }
    </script>
</head>


</html>

<form>
    <select name="users" onchange="showUser(this.value)">
        <option>-None Selected-</option>

        <?php
        $con = mysqli_connect('localhost','root','','imetrics') or die ("Cannot connect");
        $query=mysqli_query($con, "SELECT * FROM question");
        while($row=mysqli_fetch_array($query)) {
            ?>
            <option value="<?php echo $row["question_id"]; ?>"><?php echo $row["questiontitle"]; ?></option>
            <?php
        }
        ?>

    </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

php code

  <?php

    $con = mysqli_connect('localhost','root','','imetrics') or die ("Cannot connect to database");
    if(!$con){
        echo ('Could not connect: ' . mysqli_error($con));
    }

    $id= isset($_GET["q"])?$_GET["q"]:"";

    $query = mysqli_query($con, "SELECT * FROM question WHERE question_id = '".$id."'");

    while($row = mysqli_fetch_array($query)) {

        echo $row['Option_1'] . "<br>";
        echo $row['Option_2'] . "<br>";
        echo $row['Option_3'] . "<br>";
        echo $row['Option_4'] . "<br>";
        echo $row['Option_5'] . "<br>";
        echo $row['Option_6'] . "<br>";
        echo $row['Option_7'] . "<br>";
        echo $row['Option_8'] . "<br>";
        echo $row['Option_9'] . "<br>";
        echo $row['Option_10'] . "<br>";


    }

    ?>

What's the right condition for my problem?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You just have to put a bit more work into it. Also, using intval for SQL injection attack prevention. I am using a function to prevent duplicate copy-pasted code. I'm also escaping the data from the database as it might contain "<" or "&".

<?php

$con = mysqli_connect('localhost','root','','imetrics') or die ("Cannot connect to database");
if(!$con){
    echo ('Could not connect: ' . mysqli_error($con));
}

$id= isset($_GET["q"])?intval($_GET["q"]):"";

$query = mysqli_query($con, "SELECT * FROM question WHERE question_id = '".$id."'");

function displayOption($i, $value, $answer_type) {
   if($value == null) {
       return;
   }

   if($answer_type == "radiobutton") {
       echo '<input type="radio" name="rinput" value="'.htmlspecialchars($value, ENT_QUOTES).'">'.htmlspecialchars($value).'<br>';
   } else if($answer_type == "checkbox") {
    echo '<input type="checkbox" name="cinput['.$i.']" value="'.htmlspecialchars($value, ENT_QUOTES).'">'.htmlspecialchars($value).'<br>';
   }
}

while($row = mysqli_fetch_assoc($query)) {
    for($i = 1; $i<=10; ++$i) {
        displayOption($i, $row["Option_$i"], $row['answer_type']);
    }
}

?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...