Jump to content

[SOLVED] Combining a radio button search with a dropdown


swatisonee

Recommended Posts

hi,

a. I have a form where a user has to select a company name to update.

b. Currently, i use a code where the first few alphabets of the company name are typed, a radio button used to selected one of the results that pop up and then another 2 scripts update the data.

c. Is it possible to select company name and update it on the same form ?

d. My table structures are :

CID(int), Company (varchar) : Table :Customers

CAID(int), CID(int), Cityid(int), Street(var char), Postcode(varchar) : Table : Custaddress

e. So if a user types "exam" , company names like Example inc, Example Ltd get listed. From these, the user chooses say, Example inc. whose address is to be modified.

f. Then another script selects say the 3 addresses linked to Example inc and then again the user chooses one of them to be modified.

g. Then next script allows editing this address and the data thus posted in the final script updates it in the db.

h. I would like steps (e) (f) (g) and (h) to be done on a single form in one script .

i. I cannot use JS as the number of cos. are over 500 .

Would anyone be able to suggest how best I could go about this ?

Thanks
Swati
Link to comment
Share on other sites

Use different 'action' values and output according to those actions. Here's an example
[code]
<?php
include '../test/db2.php';

function companyList($co) {
    $sql = "SELECT CID, Company
            FROM customer
            WHERE company LIKE '$co%'
            ORDER BY company";
    $res = mysql_query($sql) or die(mysql_error());
    $str = "<table cellspacing='0' cellpadding='2' border='0'>\n";
    while (list($id,$name)=mysql_fetch_row($res)) {
        $str .= "<tr><td><a href='?action=select&amp;company=$id'>Select</td><td>$name</td></tr>\n";     
    }
    $str .= "</table>";
    return $str;
}

function addressList($cid) {
    global $fmCompany;
    $sql = "SELECT a.CAID, a.street, a.cityid, a.postcode, c.company
            FROM custaddress a
            INNER JOIN customer c ON a.CID = c.CID
            WHERE a.CID = '$cid'";
    $res = mysql_query($sql) or die(mysql_error());
    $str = '<form method="post">
            <input type="hidden" name="fname" value="fmAddress">
            <table border="1">
            <tr><th>Street</th><th>City</th><th>Postcode</th><th>Select</th></tr>';
    while (list($caid, $st, $city, $pc, $co)=mysql_fetch_row($res)) {
        $fmCompany = $co;
        $str .= "<tr><td>$st</td><td>$city</td><td>$pc</td>
                <td><input type='radio' name='address' value='$caid'></td></tr>";
    }
    $str .= '</table><br>
    <input type="submit" name="action" value="Edit selected">
    </form>';
    return $str;
}

function editForm($caid) {
    global $fmCompany;
    $sql = "SELECT a.caid, a.street, a.cityid, a.postcode, c.company
            FROM custaddress a
            INNER JOIN customer c ON a.CID = c.CID
            WHERE a.CAID = '$caid'";
    $res = mysql_query($sql) or die(mysql_error());
    $str = "<form method='post'>
            <input type='hidden' name='caid' value='$caid'>
            <table border='1'>";
    list($caid, $st, $city, $pc, $co)=mysql_fetch_row($res);
    $fmCompany = $co;
    $str .= "<tr><td>Street</td><td><input type='text' name='street' value='$st' size='25'></td></tr>
            <tr><td>City</td><td><input type='text' name='cityid' value='$city' size='3'></td></tr>
            <tr><td>Postcode</td><td><input type='text' name='postcode' value='$pc' size='8'></td></tr>
            <tr><td>&nbsp</td><td><input type='submit' name='action' value='Update'></td></tr>";
   
    $str .= '</table></form>';
    return $str;
}

function updateAddress() {
    $sql = "UPDATE custaddress SET
            street = '{$_POST['street']}',
            cityid = '{$_POST['cityid']}', 
            postcode = '{$_POST['postcode']}' 
            WHERE CAID = '{$_POST['caid']}'";
    mysql_query($sql);
}

if (isset($_REQUEST['action'])) {

    switch ($_REQUEST['action'])  {
        case 'Search':
                $srch = $_POST['srch'];
                $fmCompany = stripslashes($_POST['srch']);
                $body = companyList($srch);
                break;
        case 'select':
                $body = addressList($_GET['company']);
                break;
        case 'Edit selected':
                $body = editForm($_POST['address']);
                break;
        case 'Update':
                $body = '';
                updateAddress();
                break;
    }
}
else $body = '';
?>
<form name='fmSearch' method='POST'>
        <input type="hidden" name="fname" value="fmSearch">
        Company Name <input type="text" name="srch" value="<?php echo $fmCompany ?>" size="20">
        <input type="submit" name="action" value="Search">
</form>

<?php echo $body ?>
[/code]
Link to comment
Share on other sites

  • 3 weeks later...
Hello Barand,

This is working great except that at each selection after the first, the Company name returns $cid value rather than $name  and $cityid gets echoed rather than $city so if i have chosen ABC &Co.  in London, when i select it to update the address , i get 23 and 10 corresponding to their ids in the table. How would i change that please ? $name gets pulled from the Customers table and $city is from the City Table whose id $cityid is recorded in Custaddress.
Thanks. Swati
Link to comment
Share on other sites

I added a new function "citySelect()" to display the name instead of code
[code]
<?php
function citySelect ($current, $dis='') {
    $sql = "SELECT cityid, city
            FROM city
            ORDER BY city";
    $res = mysql_query($sql) or die(mysql_error());
    $str = "<SELECT name='cityid' $dis>\n";
    while (list($id,$name)=mysql_fetch_row($res)) {
        $selected = $id==$current ? 'selected' : '';
        $str .= "<option value=$id $selected>$name</option>\n";     
    }
    $str .= "</select>\n";
    return $str;
}


function companyList($co) {
    $sql = "SELECT CID, Company
            FROM customer
            WHERE company LIKE '$co%'
            ORDER BY company";
    $res = mysql_query($sql) or die(mysql_error());
    $str = "<table cellspacing='0' cellpadding='2' border='0'>\n";
    while (list($id,$name)=mysql_fetch_row($res)) {
        $str .= "<tr><td><a href='?action=select&amp;company=$id'>Select</td><td>$name</td></tr>\n";     
    }
    $str .= "</table>";
    return $str;
}

function addressList($cid) {
    global $fmCompany;
    $sql = "SELECT a.CAID, a.street, a.cityid, a.postcode, c.company
            FROM custaddress a
            INNER JOIN customer c ON a.CID = c.CID
            WHERE a.CID = '$cid'";
    $res = mysql_query($sql) or die(mysql_error());
    $str = '<form method="post">
            <input type="hidden" name="fname" value="fmAddress">
            <table border="1">
            <tr><th>Street</th><th>City</th><th>Postcode</th><th>Select</th></tr>';
    while (list($caid, $st, $city, $pc, $co)=mysql_fetch_row($res)) {
        $fmCompany = $co;
        $str .= "<tr><td>$st</td><td>" . citySelect($city, 'disabled') . "</td><td>$pc</td>
                <td><input type='radio' name='address' value='$caid'></td></tr>";
    }
    $str .= '</table><br>
    <input type="submit" name="action" value="Edit selected">
    </form>';
    return $str;
}

function editForm($caid) {
    global $fmCompany;
    $sql = "SELECT a.caid, a.street, a.cityid, a.postcode, c.company
            FROM custaddress a
            INNER JOIN customer c ON a.CID = c.CID
            WHERE a.CAID = '$caid'";
    $res = mysql_query($sql) or die(mysql_error());
    $str = "<form method='post'>
            <input type='hidden' name='caid' value='$caid'>
            <table border='1'>";
    list($caid, $st, $city, $pc, $co)=mysql_fetch_row($res);
    $fmCompany = $co;
    $str .= "<tr><td>Street</td><td><input type='text' name='street' value='$st' size='25'></td></tr>
            <tr><td>City</td><td>" . citySelect($city) . "</td></tr>
            <tr><td>Postcode</td><td><input type='text' name='postcode' value='$pc' size='8'></td></tr>
            <tr><td>&nbsp</td><td><input type='submit' name='action' value='Update'></td></tr>";
   
    $str .= '</table></form>';
    return $str;
}

function updateAddress() {
    $sql = "UPDATE custaddress SET
            street = '{$_POST['street']}',
            cityid = '{$_POST['cityid']}', 
            postcode = '{$_POST['postcode']}' 
            WHERE CAID = '{$_POST['caid']}'";
    mysql_query($sql);
}

if (isset($_REQUEST['action'])) {

    switch ($_REQUEST['action'])  {
        case 'Search':
                $srch = $_POST['srch'];
                $fmCompany = stripslashes($_POST['srch']);
                $body = companyList($srch);
                break;
        case 'select':
                $body = addressList($_GET['company']);
                break;
        case 'Edit selected':
                $body = editForm($_POST['address']);
                break;
        case 'Update':
                $body = '';
                updateAddress();
                break;
    }
}
else $body = '';
?>
<form name='fmSearch' method='POST'>
        <input type="hidden" name="fname" value="fmSearch">
        Company Name <input type="text" name="srch" value="<?php echo $fmCompany ?>" size="20">
        <input type="submit" name="action" value="Search">
</form>

<?php echo $body ?>[/code]
Link to comment
Share on other sites

*SOLVED* . Simple typo error - company instead of Company

Hello Barand,

Thanks for writing back. I've spent this weekend trying to find out why i'm not able to achieve 2 updations to your code :

a) Adding an echo " Address Updated". I set this up to echo after 

[code] case 'Update':
                $body = '';
                updateAddress();
                break;[/code]

but it returns a T_ELSE error. If i place after  [code]else $body = '';[/code], it comes up even during the selection process.

b) The Company Name continues to reflect the CID  ie , 197 instead of Php Ltd at the time of address selection (when the name pops up on screen). So if i want to change the name to Ajax Ltd I cannot do this.

I'm trying to understand the function list as i read thru each line of code but i dont get the parts where the above 2 should get triggered.

Would really appreciate your help.

Thanks ,
Swati
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.