#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Ajax Autocomplete textbox


    I am coding a PHP program to make a list of items (e.g.,customer name) just below the textbox.

    My code for data entry is (customer.php)

    <?php

    /**
    * @author
    * @copyright 2012
    */
    ?>
    <head>
    <script>
    function ClearScreen()
    {
    document.f1.storename.focus();
    }

    function confirmation(url)
    {
    window.location=url;
    }

    </script>
    <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery.autocomplete.js"></script>
    <script>
    $(document).ready(function(){
    $("#suppname").autocomplete("autocomplete.php?from=supplier", {
    selectFirst: true
    });
    });
    </script>


    </head>
    <form name="f1" method="post" action="customeradd.php">
    <table>
    <tr>
    <td>Name</td>
    <td><input type="text" name="suppname" id="suppname" size="20" /></td>
    <td><input type="text" name="suppid" id="suppid" /></td>
    </tr>
    <tr>
    <td>Address</td>
    <td><textarea name="address" rows="4" cols="50"></textarea> </td>
    </tr>
    <tr>
    <td>Phone</td>
    <td><input type="text" name="phone" id="phone" size="20" /></td>
    </tr>
    <tr>
    <td>Type</td>
    <td><select name="type">
    <option value="Customer">Customer</option>
    <option value="Supplier">Supplier</option>
    </select></td>
    </tr>
    <tr>
    <td>Credit limit</td>
    <td><input type="text" name="credit" id="credit" size="20" /></td>
    </tr>
    <tr>
    <td>Notes</td>
    <td><textarea name="notes" rows="4" cols="50"></textarea></td>
    </tr>
    </table><table>
    <tr>
    <td><input type="button" value="Save" onclick="confirmation('storeadd.php?mode=1&storename='+storename.value)" /></td>
    <td><input type="button" value="Delete" onclick="deletestore.php?storeid=''" /></td>
    </tr>
    </table>
    <div id="txtHint"></div>
    </form>

    <?php
    function loadvalues()
    {
    echo "<script>javascript:alert('hello');</script>";
    }

    ?>


    The code to fetch the desired list is (autocomplete.php) is


    <?php
    $q=$_GET['q'];
    $from=$_REQUEST['from'];
    $my_data=mysql_real_escape_string($q);


    include("connect.php");
    $database="stockdata";

    mysql_select_db ($database, $link);


    if($from=="store")
    $sql="SELECT storename,storeid FROM store WHERE storename LIKE '%$my_data%' ORDER BY storename";
    if($from=="supplier")
    $sql="SELECT suppname,suppid FROM supplier WHERE suppname LIKE '%$my_data%' ORDER BY suppname";

    $result=mysql_query($sql);
    $num_rows = mysql_num_rows($result);

    if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
    }
    if($result)
    {
    $row = mysql_fetch_row($result);
    for ($i=0;$i < $num_rows ; $i++)
    {
    echo($row['0']) ."\n"; ?>
    <input type="hidden" name="suppid" id="suppid" VALUE="<?php echo($row['1'])?>" />
    <?PHP //echo $sql;
    //$answer = array ($row['1'] );
    $row = mysql_fetch_row($result);
    }
    }
    //var_dump($answer);
    ?>


    It is working fine. But I want this code to return 2 values (suppname and suppid) to the calling code. How is it possible?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,540
    Rep Power
    595
    1) Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum.
    2) To return multiple values, use an array, although I don't see any PHP functions that return anything.
    PHP Code:
    $retval[]=$value1;
    $retval[]=$value2;
    return(
    $retval); 
    3) Do not use the deprecated MySQL extensions, switch to PDO.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by gw1500se
    1) Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum.
    2) To return multiple values, use an array, although I don't see any PHP functions that return anything.
    PHP Code:
    $retval[]=$value1;
    $retval[]=$value2;
    return(
    $retval); 
    3) Do not use the deprecated MySQL extensions, switch to PDO.
    The 9th line from last echo($row['0']) ."\n"; ?> inside a for loop adds the suppname to the list which will get displayed in the dataentry screen. (customer.php)

    If you have any other method of coding Ajax that returns 2 values, pls help me.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,540
    Rep Power
    595
    This is really a Javascript/Ajax question. You can have it moved to that forum by clicking on the red triangle in the upper right.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo