How to do a condition displaying input type based

2019-09-20 12:31发布

问题:

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?

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?

回答1:

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']);
    }
}

?>


回答2:

Try this,

if($row['answer_type']=='radiobutton'){
    echo '<input type="radio" name="" value="" /> '.$row['Option_1']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_2']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_3']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_4']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_5']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_6']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_7']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_8']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_9']."<br />";
    echo '<input type="radio" name="" value="" /> '.$row['Option_10']."<br />";
}
else if($row['answer_type']=='checkbox'){
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_1']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_2']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_3']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_4']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_5']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_6']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_7']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_8']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_9']."<br />";
    echo '<input type="checkbox" name="" value="" /> '.$row['Option_10']."<br />";
}
else{
    // nothing.
}


回答3:

Try this:

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

if($row['answer_type']=='radiobutton'){
echo '<input type="radio" name="options" value='".$row['Option_1']."' > echo $row['Option_1'] . "<br>";
.
.
.//all other options
}else{
echo '<input type="checkbox" name="options" value='".$row['Option_1'] ."'>'$row['Option_1'].<br>
. "<br>";
.
.
.//all other options
}


}