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>
5
  • 1
    what problem you are facing?? your code seems to be fine Commented Apr 17, 2015 at 6: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. Commented Apr 17, 2015 at 6: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!! Commented Apr 17, 2015 at 7:13
  • @ Martin: Hi pls guide me you mention your security tips. i am a beginner. Commented Apr 17, 2015 at 7:16
  • Why shouldn't I use mysql_* functions in PHP? Commented Apr 17, 2015 at 7:20

3 Answers 3

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

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

2 Comments

@Gobinath: I hope you didn't take this advice - removing intval() will introduce a second SQL injection vulnerability into your code.
@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.
0

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

function showUser(str) {
    alert(str);
      if (str=="") {

8 Comments

Hi , i have copied getuser.php pls guide me
@amrita this is not an answer.. this should be posted as a comment.. you can read further here for how to write an answer in stackoverflow stackoverflow.com/help/how-to-answer
@NishantSolanki: I need 50 reputation to add a comment dats why
@AmritaGupta oh ok.. but if you will post comments as answers than someone will downvote it and you will loose your reputations instead of gaining it :D, best luck ..
@Amrita Gupta : Hi i try , i got an "int" value in alert box. i need help how to pass the string value to ajax
|
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

3 Comments

Hi , i try to pass the string in option value but ajax cal not working only working for integer.
as said by @Amrita what are you getting in alert box??
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.

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.