Pratik
Pratik

Reputation: 126

Datalist is not showing complete name after space in dropdown

Datalist is not printing complete name after space. It is just giving first word of name i.e ignoring complete name after space. The names are fetched from database. Suppose the bank name is "XYZ Global Bank" then it is showing only "XYZ" in drop down and rest name is not shown. Please help me here.

$query = "SELECT name FROM issuer";
$result = $conn->query($query);
if(!$result) die($conn->error);
$rows=$result->num_rows;
echo "<datalist id='bankNames'>";
for($i=0; $i<$rows; $i++)
{
$result->data_seek($i);
$row = $result->fetch_array(MYSQLI_ASSOC);
echo "<option value='".$row['name']."'>";
 }
 echo "</datalist>";

Upvotes: 1

Views: 1066

Answers (3)

mickmackusa
mickmackusa

Reputation: 47864

It is not made clear by your question details, but if the exerienced problem isn't related to your specific browser, the it is possible that an option value contains a syntax breaking character or that your input tag syntax is flawed.

As Rick James demonstrated in his answer, but neglected to explain, you should escape dynamic values being printed to your HTML document to ensure that everything is populated and rendered correctly.

You can safely iterate the result set object returned from a mysqli query -- this spares the need to call fetch() in the loop.

Code: (PHPize Demo)

<label for="bankname">Name of Bank: </label>
<input id="bankname" list="bank_names" name="bank_name">
<datalist id="bank_names">
<?php foreach ($conn->query("SELECT name FROM issuer") as $row) { ?>
    <option value="<?php echo htmlentities($row['name']); ?>">
<?php } ?>
</datalist>

Pay careful attention to the similar identifying attributes and their relationships within the form.

  1. The <label> tag's for="bankname" refers to the <input> tag's id="bankname".
  2. The <input> tag's list="bank_names" refers to the <datalist> tag's id="bank_names".
  3. The <input> tag's name="bank_name" is used as the submitted value's key in the submission payload array. Access the submitted value via $_GET['bank_name'] or $_POST['bank_name'] depending on your form method.

Additional references:

  1. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist
  2. https://www.techonthenet.com/html/elements/datalist_tag.php

Upvotes: 0

Rick James
Rick James

Reputation: 142218

$name = htmlentities($row['name']);
echo "<option value='$name'>";

Upvotes: 0

Ritesh Kumar Sharma
Ritesh Kumar Sharma

Reputation: 31

Hi, Use this code it ll resolve all your issue with the space. For your reference i have attached one image file so it ll be very help full for you.

<?php
 $conn = new mysqli('localhost', 'username', 'password', 'database-name') 
              or die ('Cannot connect to db');
 $result = $conn->query(" SELECT DISTINCT name FROM issuer;");
   echo "<select name='name' class='required-entry form-control' id='name' >";
     while ($row = $result->fetch_assoc()) {
           unset($name);
           $name = $row['name'];
           echo '<option value=""></option>';
           echo '<option value="'.$name.'">'.$name.'</option>';
       }
       echo "</select>";
?>

Image for Output how it will show.

Upvotes: 1

Related Questions