0

i need to show different values only in drop down list.drop down list selection based result cal in ajax and display the related data show below in the drop down list.

How to pass the string value to ajax in this script.

Anyone pls guide me

<!doctype html>
<html class="no-js" lang="en">
  <head>
    <script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  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) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>
  </head>
  <body>

    <div class="row">
      <div class="large-12 columns">
        <h1></h1>
      </div>
    </div>

    <div class="row">
      <div class="large-12 columns">
        <!--Main Tab Start-->
        <?php
        $res=mysql_query("select DISTINCT tag,id from password");

        if($res === false )
        {
            die(mysql_error());
        }
        ?>
        <form action="" method="post">
            <select name="tagg" onchange="showUser(this.value)">
            <option value="">Select Accounts</option>
        <?php
        while($row=mysql_fetch_array($res))
        {
            $dess=$row['tag'];
            $id=$row['id'];
            ?>
            <option value="<?php echo $id;?>"><?php echo $dess; ?></option>

            <?php
        }

        ?>
                    </select>
            </form>
             <div id="txtHint"><b>Person info will be listed here.</b></div>
        <!--Main Tab Ent-->
      </div>
    </div>
  </body>
</html>

Getuser.php

<?php
$q = intval($_GET['q']);
$result=mysql_query("select * from password WHERE id = '".$q."'");
if($result === false )
{
    die(mysql_error());
}
while($row=mysql_fetch_array($result))
{
    $desss=$row['tag'];
}
$res=mysql_query("select * from password WHERE tag = '".$desss."'");
if($res === false )
{
    die(mysql_error());
}
?>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>username</th>
<th>Password</th>
<th>Description</th>
<th>Link</th>
</tr>
</thead>
<?php

while($row=mysql_fetch_array($res))
{
    $id=$row['id'];
     $name=$row['name'];
    $url=$row['url'];
    $uname=$row['username'];
    $pass=$row['password'];
    $tag=$row['tag'];
    $des=$row['description'];

    ?>
    <tbody>
    <tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $name; ?></td>
    <td><?php echo $uname; ?></td>
    <td><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo $pass; ?>">View</span></td>
    <td><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo $des; ?>">Description</span></td>
    <td><a href="<?php echo $url; ?>" title="<?php echo $name; ?>" target="_blank">Link</a></td>

    </tr>
    </tbody>
    <?php

}
?>
</table>
CodeMan
  • 1,941
  • 6
  • 26
  • 44
  • 1
    what problem you are facing?? your code seems to be fine – Nishant Solanki Apr 17 '15 at 06:45
  • i need help how to pass the option value in string in this script . My script does not work to pass the string values .only work for int values. – CodeMan Apr 17 '15 at 06:55
  • 1
    Stop using MySQL it is **deprecated**, use instead MySQLi or PDO. Plain MySQL has security flaws and is no longer maintained - avoid it!! – Martin Apr 17 '15 at 07:13
  • @ Martin: Hi pls guide me you mention your security tips. i am a beginner. – CodeMan Apr 17 '15 at 07:16
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – krishna Apr 17 '15 at 07:20

3 Answers3

4

I have run your code, it is working fine.There is only one problem ,if you want to pass string then change following code on your gestuser.php change

$q = intval($_GET['q']);

to

$q =($_GET['q']);

and change

<option value="<?php echo $id;?>"><?php echo $dess; ?></option>

to

<option value="<?php echo $dess;?>"><?php echo $dess; ?></option>

Other wise your code is working

krishna
  • 4,069
  • 2
  • 29
  • 56
Sourabh
  • 500
  • 2
  • 14
  • @Gobinath: I hope you didn't take this advice - removing `intval()` will introduce a second SQL injection vulnerability into your code. – halfer Apr 20 '15 at 13:03
  • @Sourabh: can you fix this security problem in your answer? I suggest `mysql_real_escape_string()` here, even though - as the OP is aware - the library is deprecated. – halfer Apr 20 '15 at 13:04
0

Edit your function as following and see what string you are passing through ajax

function showUser(str) {
    alert(str);
      if (str=="") {
Amrita Gupta
  • 37
  • 1
  • 11
0

the javascript ajax function fires when the select value has changed

<select name="tagg" onchange="showUser(this.value)">

now this.value means you are passing the selected value to the javascript function

<option value="<?php echo $id;?>"><?php echo $dess; ?></option>

you have to put string in your value attribute

something like

<option value="<?php echo 'YOUR STRING HERE';?>"><?php echo $dess; ?></option>

now in your getuser.php file your query is

"select * from password WHERE id = '".$q."'"

so you will have to change the query respectively

Nishant Solanki
  • 2,119
  • 3
  • 19
  • 32
  • Hi , i try to pass the string in option value but ajax cal not working only working for integer. – CodeMan Apr 17 '15 at 07:13
  • as said by @Amrita what are you getting in alert box?? – Nishant Solanki Apr 17 '15 at 07:15
  • Hi , i got an string value in alert box following your steps. but "getuser.php" i am try to echo the alert box value . now the value is "0" for all the drop down list selection. – CodeMan Apr 17 '15 at 07:22