Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 update data using php and javascript

views
     
TSyoyorock
post May 24 2009, 10:17 AM, updated 17y ago

New Member
*
Junior Member
18 posts

Joined: May 2009


<select name="jenama" size="1" id="brand" onChange="auto_select()">
</select> | <select name="model" size="1" id="model" style="width:150px"></select>
<script language="javascript">
navigator.appName;
var arr = new Array();
arr[0] = new Array("HP","HP1234","HPDV5228TX","HPDV2001TX");
arr[1] = new Array("ASUS","A8H","F3S");
arr[2] = new Array("ACER","Aspire4530","Aspire4935","Aspire3810T","TravelMate6493");
var IE = false;
var FF = false;
if (navigator.appName.indexOf("Microsoft") == 0) {
IE = true;
} else if (navigator.appName.indexOf("Netscape") == 0) {
FF = true;
}
function auto_select() {
while(document.getElementById("model").length > 0) {
document.getElementById("model").remove(0);
}
var si = document.getElementById("brand").selectedIndex;
if (FF) {
for (i = 1; i < arr[si].length; i++) {
document.getElementById("model").add(new Option(arr[si][i],arr[si][i]),null);
}
} else {
var newOption;
for (i = 0; i < arr[si].length; i++) {
newOption = document.createElement("option");
newOption.text = newOption.value = arr[si][i];
document.getElementById("model").add(newOption);
}
}
}
function initial() {
if (FF) {
for (i = 0; i < arr.length; i++) {
document.getElementById("brand").add(new Option(arr[i][0],arr[i][0]),null);
}
} else {
var newOption;
for (i = 0; i < arr.length; i++) {
newOption = document.createElement("option");
newOption.text = newOption.value = arr[i][0];
document.getElementById("brand").add(newOption);
}
}
document.getElementById("brand").options[0].selected = true;
}
initial();
auto_select();
</script>


i want to update data in sql that have list menu that are using javascript code..how can i display the original data in the list menu before update??how to combine with php code?? icon_question.gif
puaka_astro
post May 24 2009, 11:00 AM

Casual
***
Junior Member
331 posts

Joined: Jun 2007
From: Isketambola


learn ajax/jquery.
slier81
post May 24 2009, 11:50 AM

On my way
****
Senior Member
541 posts

Joined: Jun 2007
From: Penang
QUOTE(puaka_astro @ May 24 2009, 11:00 AM)
learn ajax/jquery.
*
not a very good advice indeed...
learning ajax/jquery dosent make u a better javascript programmer
instead learn javascript..than move on to javascript library

refer mozilla javascript reference https://developer.mozilla.org/en/JavaScript

and dude pls put your code in [code] tag and please make ur code readable by indenting it

This post has been edited by slier81: May 24 2009, 12:08 PM
ExpZero
post May 24 2009, 12:49 PM

Regular
******
Senior Member
1,522 posts

Joined: Mar 2007
From: Kuala Lumpur
QUOTE(puaka_astro @ May 24 2009, 11:00 AM)
learn ajax/jquery.
*
I'm a jquery programmer, jquery has some disadvantage than javascript, For example, if you use $('#blah').html('a') a few more times, it will leak all your user's memory. However if you use document.getElementById('blah').innerHTML = 'a'; , your memory usage is constant at that level. It's because jquery convert the 'a' into DOM object, but the pure javascript just append it directly. Otherwise, it's good because I have to write lesser with jquery smile.gif
Rowan
post May 24 2009, 01:32 PM

Getting Started
**
Junior Member
168 posts

Joined: Dec 2005
From: Singapore


CODE
Table Structure in MySQL
/-------+---------\
brandIdbrandName
+-------+---------+
     1ASUS    
     2HP      
     3ACER    
+-------+---------+

+-------+---------+-------+
modelIdmodelNamebrandId
+-------+---------+-------+
     1A8H            1
.........................
+-------+---------+-------+



Change this
CODE
arr[0] = new Array("HP","HP1234","HPDV5228TX","HPDV2001TX");
arr[1] = new Array("ASUS","A8H","F3S");
arr[2] = new Array("ACER","Aspire4530","Aspire4935","Aspire3810T","TravelMate6493");

into this betweenvar arr = new Array();, and var IE = false;
CODE
<?php
mysql_connect("localhost","username","password");
mysql_select_db("mydatabase");

$rs_brand = mysql_query("SELECT `brandId`,`brandName` FROM `brand`");
$id = 0;
while ($row0 = mysql_fetch_array($rs_brand)) {
$model = "arr[$id] = new Array(\"$row0[1]\"";
$rs_model = mysql_query(sprintf("SELECT `modelId`,`modelName` FROM `model` WHERE `brandId` = '%u'",$row0[0]));
while ($row1 = mysql_fetch_array($rs_model)) {
 $model .= ",\"$row1[1]\"";
}
$model .= ");\n";
echo $model;
$id++;
}
?>

» Click to show Spoiler - click again to hide... «

nyem
post May 25 2009, 10:19 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Jan 2007


QUOTE(Rowan @ May 24 2009, 01:32 PM)
CODE
$rs_brand = mysql_query("SELECT `brandId`,`brandName` FROM `brand`");
while ($row0 = mysql_fetch_array($rs_brand)) {
  $rs_model = mysql_query(sprintf("SELECT `modelId`,`modelName` FROM `model` WHERE `brandId` = '%u'",$row0[0]));
     while ($row1 = mysql_fetch_array($rs_model)) {
        ...
     }
  }
}

This is not a recommended way of doing database query. If you have 100 brands you are going to query the database 100 times.

QUOTE(yoyorock @ May 24 2009, 10:17 AM)
if (navigator.appName.indexOf("Microsoft") == 0) {
IE = true;
} else if (navigator.appName.indexOf("Netscape") == 0) {
FF = true;
}

I wonder why the need to detect Netscape. Anyone still using Netscape?

Here's a revamped code that should work on IE, Firefox, Opera, Chrome and Safari. I don't have Netscape to test this.
CODE

<select name="jenama" size="1" id="brand" onChange="auto_select(Brands,'jenama','model')">
</select> | <select name="model" size="1" style="width:150px"></select>

<script>
var Brands = {

<?php
mysql_connect("localhost","username","password");
mysql_select_db("test");

$sql = <<<EOT
  SELECT CONCAT( "'", a.brandName, "':['", GROUP_CONCAT( b.modelName SEPARATOR "','" ), "']")
  FROM brand a, model b
  WHERE a.brandId = b.brandId
  GROUP BY a.brandId
  ORDER BY a.brandName

EOT;

$rows = array();
$dbquery = mysql_query($sql);
while ($row = mysql_fetch_array($dbquery)) {
  array_push($rows, $row[0]);
}
echo( implode(",\n", $rows) );
?>

};

function init_select(arr, list1name)   {

  // remove all existing options from List1
  var List1 = document.getElementsByName( list1name )[0];
  while (List1.options.length > 0)
     List1.options[List1.options.length-1] = null;

  // populate options
   for (var i in arr)
     List1.options[ List1.options.length ] = new Option(i, i, false, false);

}

function auto_select(arr, list1name, list2name)   {

  // remove all existing options from List2
  var List2 = document.getElementsByName( list2name )[0];
  while (List2.options.length > 0)
     List2.options[List2.options.length-1] = null;

  // populate options
  var List1 = document.getElementsByName( list1name )[0];
  for (var i in arr)
     if (List1.options[ List1.selectedIndex ].text == i)
        for (var k=0;k<arr[i].length;k++)
           List2.options[ List2.options.length ] = new Option(arr[i][k], arr[i][k], false, false);
}

init_select(Brands,'jenama');
auto_select(Brands,'jenama','model');

</script>


This post has been edited by nyem: May 25 2009, 10:20 AM
Rowan
post May 25 2009, 11:12 AM

Getting Started
**
Junior Member
168 posts

Joined: Dec 2005
From: Singapore


You're right for this, nyem.
TSyoyorock
post May 25 2009, 04:45 PM

New Member
*
Junior Member
18 posts

Joined: May 2009


$rs_brand = mysql_query("SELECT `brandId`,`brandName` FROM `brand`");
while ($row0 = mysql_fetch_array($rs_brand)) {
$rs_model = mysql_query(sprintf("SELECT `modelId`,`modelName` FROM `model` WHERE `brandId` = '%u'",$row0[0]));
while ($row1 = mysql_fetch_array($rs_model)) {
...
}
}
}


what can i do with this code?? smile.gif

 

Change to:
| Lo-Fi Version
0.0171sec    0.30    5 queries    GZIP Disabled
Time is now: 29th November 2025 - 02:58 PM