-1

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?
6
  • 2
    same question you asked two days ago and also you get reply for that .Possible duplicate of Displaying data from database not working properly in PHP/javascript Commented Jun 21, 2017 at 9:00
  • It's different logic sir im using ajax on this one while I'm not on my last one Commented Jun 21, 2017 at 9:08
  • i don't see any ajax related query in this question and not even tagged with ajax tag . @Jola Commented Jun 21, 2017 at 9:12
  • Here sir sorry I forgot to include pastebin.com/e13qexiy @JYoThI I pressed the button ^ on your answer in my previous question sir thank you it really helped me Commented Jun 21, 2017 at 9:17
  • update your pastebin link in your question with some description . @Jola Commented Jun 21, 2017 at 9:20

3 Answers 3

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

?>
Sign up to request clarification or add additional context in comments.

7 Comments

It's displaying 0000000000
Ouch, too much java coding. I fixed it to use the dot as a string concatenator.
Sir it's working but it's also displaying the option1-10 that have null values which it shouldnt :( also how do i make the radiobutton only allowed to choose one? coz atm u can choose many.
My fault that with selecting multiple radios. I have to provide a name for the radios/checkboxes. Wait a second please, i have to edit it again.
Also whereabouts do I add that it shouldnt display the input type when the column is null of option option1-10?
|
0

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.
}

Comments

0

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
}


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.