swatisonee Posted November 21, 2006 Share Posted November 21, 2006 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 :CustomersCAID(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 ? ThanksSwati Quote Link to comment Share on other sites More sharing options...
Barand Posted November 22, 2006 Share Posted November 22, 2006 Use different 'action' values and output according to those actions. Here's an example[code]<?phpinclude '../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&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> </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] Quote Link to comment Share on other sites More sharing options...
swatisonee Posted November 23, 2006 Author Share Posted November 23, 2006 This is fantastic ! I was honestly thinking that this would not be possible at all . Thank you very very much much !And the best part is that it makes the searching process so very quick.Thanks again Barand,Swati Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 14, 2006 Author Share Posted December 14, 2006 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted December 16, 2006 Share Posted December 16, 2006 I added a new function "citySelect()" to display the name instead of code[code]<?phpfunction 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&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> </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] Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 18, 2006 Author Share Posted December 18, 2006 *SOLVED* . Simple typo error - company instead of CompanyHello 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.