Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. You didn't provide any details about how you were going to use this information. If it were me I would be storing this information in a database. And, if you are doing that then the ID should be dictated by the ID in the database and not dynamically generated via JavaScript
  2. Well, it does and doesn't work. It does work to add a new input row, but it will be completely unusable because it does not modify the field name so each field comes across correctly. You could either change the field name to be unique OR create the field name as an array so the PHP code can handle it more efficiently. I went with a slightly different solution. The following code will dynamically add a new row each time the user adds a value to the last one. You will need to modify your PHP code to process the multiple files. Instead of a single image in the variable $_FILES['image'], you will get an array of images in the variable $_['files']. So you would want to do a foreach loop on it. <script type="text/javascript"> function addImageField(fieldObj) { var formObj = document.getElementById('newad'); var imgInput = formObj.elements['image[]']; var lastField = (imgInput.length) ? imgInput[imgInput.length-1] : imgInput; if (lastField.value) { var tableObj = document.getElementById('formTable'); var imageRowObj = document.getElementById('imgInputRow'); inputObj = document.createElement(imageRowObj.innerHTML); var newRow = tableObj.insertRow(tableObj.rows.length-1); var newCell = newRow.insertCell(0); newCell.appendChild(inputObj); } } </script> <form name="newad2" id="newad" method="post" enctype="multipart/form-data" action=""> <table id="formTable" border="1"> <tr><td id="imgInputRow"><input type="file" name="image[]" onchange="addImageField(this);"></td></tr> <tr><td><input name="Submit" type="submit" value="Upload images"></td></tr> </table> </form>
  3. <html> <head> <script type="text/javascript"> //<![CDATA[ // array of possible team in the same order as they appear in the team selection list var teamLists = new Array(); teamLists[1] = ["Oz", "Skipjack", "Curmit", "Old Salt", "Kip"]; teamLists[2] = ["Jolly Roger", "Oscar", "Altonr77", "Northpaw", "Cabeza de Vaca"]; teamLists[3] = ["Puretexn", "ShawnQ", "Mando", "Cowboy", "Alamo"]; teamLists[4] = ["Chazbo", "Bigfost", "Fla-Fish", "DancingWithBulls Sr.", "DancingWithBulls"]; teamLists[5] = ["Artofficial", "Easily Yakked", "PierPressure", "FishinFools(Herb)", "FishinFools(Jan)"]; teamLists[6] = ["Rod Dawg", "Latestart", "Aceshooter", "Randy", "Arnulfo"]; teamLists[7] = ["Masonator", "Chainzter", "Roel115", "Gill Ripper", "Greatwhite72"]; teamLists[8] = ["Krakatoa", "El Gallo", "Junior"]; teamLists[9] = ["Johnny A", "Stray", "NickleBait", "Socom", "KiDd"]; teamLists[10] = ["Yakmon", "Mr. Champ", "RaiderRed", "Repofish", "Shindle"]; /* teamChange() is called from the onchange event of a select element. * param selectObj - the select object which fired the on change event. */ function teamChange(selectObj) { // get the value of the selected option var selectValue = selectObj.options[selectObj.selectedIndex].value; // use the selected option value to retrieve the list of items from the teamLists array if(!teamLists[selectValue]) { anglerList = new Array(); } //Error handling anglerList = teamLists[selectValue] // get the team select element via its known id var anglerSelect = document.getElementById("angler"); // remove the current options from the team select anglerSelect.options.length = 0; //Determine the starting angler index anglerIdx = 1; for(var i=1; i<selectValue; i++) { anglerIdx += teamLists[i].length; } // create new options addOption(anglerSelect, 0, 'Select Angler'); for (var i=0; i<anglerList.length; i++) { addOption(anglerSelect, (i+anglerIdx), anglerList[i]); } } function addOption(selObj, optValue, optText) { var newOption = document.createElement('option'); newOption.value = optValue; newOption.text = optText; try { selObj.add(newOption); // this will fail in DOM browsers but is needed for IE } catch (e) { selObj.appendChild(newOption); } } //]]> </script> </head> <body> <select name="team_id" id="team_id" onChange="teamChange(this);"> <option value="0">Select Team</option> <option value="1">Rockstar</option> <option value="2">Plastic Pirates</option> <option value="3">Lone Star Sharkers</option> <option value="4">Roadrunner</option> <option value="5">Hawg Wild</option> <option value="6">Mojo</option> <option value="7">TAMUK Anglers</option> <option value="8">Krakatoa</option> <option value="9">Why Knot</option> <option value="10">Kraken</option> </select> <select name="angler" id="angler" onchange="alert(this.value)"> <option value="0">Select Angler</option> </body> </html>
  4. Well, there's no noticable dealy at all in IE, but FF definitely has some issues. I would probably try and rework this some more, but here is a quick fix. I'm sure there are better ways to accomplish this - I'm just changing the "rel" parameter for the style sheet objects so the browser no longer considers them stylesheets. I've tested in IE7 and FF3, and there is no noticable lag.. First load all the stylesheets - in reverse order so the first sheet takes precedence. <link id="sheet_3" rel="stylesheet" href="http://edwardmc.com/example/sheet_3.css"> <link id="sheet_2" rel="stylesheet" href="http://edwardmc.com/example/sheet_2.css"> <link id="sheet_1" rel="stylesheet" href="http://edwardmc.com/example/sheet_1.css"> Then change the function changeStyle() as follows: function changeStyle(sheetName) { var i=1; while (document.getElementById('sheet_'+i)) { relValue = (sheetName=='sheet_'+i) ? 'stylesheet' : ''; document.getElementById('sheet_'+i).rel = relValue; i++; } setCookie('stylesheet', sheetName, 2); } The function now iterates through each sheet and changes the "rel" parameter to be "stylesheet" for the selected sheet and "" (empty string) for the others.
  5. Here you go: Tested in IE & FF. Need three stylesheets named "sheet_1.css", "sheet_2.css", & "sheet_3.css" with a class named "displayText". <html> <head> <link id="stylesheet" rel="stylesheet" href="sheet_1.css"> <script type="text/javascript"> function changeStyle(sheetName) { document.getElementById('stylesheet').href = sheetName+'.css'; setCookie('stylesheet', sheetName, 2); } function setCookie(c_name,value, expiredays) { var expDate = ''; if (expiredays!=null) { var expDateObj = new Date(); expDateObj.setDate(expDateObj.getDate()+expiredays); var expDate = ';expires=' + expDateObj.toGMTString() } var cookieVal = c_name+ "=" +escape(value) + expDate; document.cookie = cookieVal; } function getCookie(c_name) { if (document.cookie.length>0) { c_start = document.cookie.indexOf(c_name + "="); if (c_start!=-1) { c_start = c_start + c_name.length + 1; c_end = document.cookie.indexOf(";", c_start); if (c_end==-1) { c_end = document.cookie.length; } return unescape(document.cookie.substring(c_start,c_end)); } } return false; } function checkStyleSheetCookie() { var sheetName = getCookie('stylesheet'); if (sheetName!=null && sheetName!="") { changeStyle(sheetName); } return; } </script> </head> <body onload="checkStyleSheetCookie();"> <span class="displayText">Display text</span> <br><button onclick="changeStyle('sheet_1');">Style 1</button> <br><button onclick="changeStyle('sheet_2');">Style 2</button> <br><button onclick="changeStyle('sheet_3');">Style 3</button> </body> </html>
  6. That is exactly what you would do, along with innerHTML(). document.getElementById('spanID').innerHTML = 'New Text';
  7. Simply have a "switch" within the PHP to determine which style sheet to use. If you are wanting to change the style sheet dynamically after the page is loaded, then yes you would need javascript. ... and I suppose a cookie would be the best solution for persistence
  8. It looks as if IE is not pardsing some of the attributes for the button element the same as FF is. I *think* IE is trying to create an INPUT BUTTON and FF is creating just a BUTTON. I say this because the value and the textnode are concatenated in the IE scenario. I know I've done a similar script in the past, but I can't find it at the moment. You should be able to find a solution with some Googling. However, in the process of evaluating your code I have rewritten it to be much more flexible and readable. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" href="js/menu.css"> <title>Employ Me - Creating Feature List</title> <link href="images/styles.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> var addInput_count = 1; var addSec_count = 1; function addInput() { var c = addInput_count++; var newDiv = createDiv('mod_div_' + c, 'mod_div_' + c); newDiv.appendChild(document.createElement('br')); newDiv.appendChild(createText('Add Module:', 'color:#000066;font-size:10px;font-weight:normal;')); newDiv.appendChild(createInput('text', 'module_' + c, 'margin-left:50px;')); newDiv.appendChild(createButton('addSec', c, 'add sub module', 'margin-left:5px;', 'alert("hello");')); newDiv.appendChild(createButton('remMod', c, 'Remove Module', 'margin-left:5px;', 'this.parentNode.parentNode.removeChild(this.parentNode);')); newDiv.appendChild(document.createElement('br')); newDiv.appendChild(document.createElement('br')); document.getElementById('myform').appendChild(newDiv); } function removeElement(divNum) { var olddiv = document.getElementById('mod_div_'+divNum); document.getElementById('myform').removeChild(olddiv); } function addSec(c) { //var count=0; var s = addSec_count++; var objNewsec = createDiv('sec_div_' + c+'_'+s, 'sec_div_' + c+'_'+s); objNewsec.appendChild(createText("Add Sub Module:", 'color:#000066;font-size:10px;font-weight:normal;')); objNewsec.appendChild(createInput('text', 'Section-'+c +'_'+s, 'margin-left:75px;')); objNewsec.appendChild(createButton('addSec', s, 'add sub section','', 'javascript:addSubSec(this.value);')); objNewsec.appendChild(document.createElement('br')); document.getElementById('mod_div_' + c).appendChild(objNewsec); } function createDiv(name, id) { var divObj = document.createElement('div'); divObj.setAttribute('name', name); divObj.setAttribute('id', id); return divObj; } function createText(text, style) { var textObj = document.createTextNode(text); var spanObj = document.createElement("span"); spanObj.setAttribute('style', style); spanObj.appendChild(textObj); return spanObj; } function createInput(type, name, style) { var inptObj = document.createElement('input'); inptObj.setAttribute('type', type); inptObj.setAttribute('name', name); inptObj.setAttribute('style', style); return inptObj; } function createButton(name, value, text, style, onclick) { var bttnObj = document.createElement('button'); bttnObj.setAttribute('type', 'button'); bttnObj.setAttribute('name', name); bttnObj.setAttribute('value', value); bttnObj.setAttribute('style', style); bttnObj.setAttribute('onclick', onclick); // bttnObj.appendChild(document.createTextNode(text)); return bttnObj; } </script> </head> <body leftmargin="2" topmargin="0" marginheight="2" marginwidth="0"> <table width="101%" height="50%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F6F7F2"> <!-- First Row --> <tr> <!-- Second Row End --> <td height="425" valign="top" bgcolor="f6f7f2"><div align="center"> <table width="100%" cellpadding="0" cellspacing="0"> <tr> <td align="center"><span class="bigfnt"> Preparing Feature List </span></td> </tr> </table> <br /> </div> <table width="97%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#D0CDAE" > <tr> <td align="center" > <form method="POST" action="form_dyn.php" onSubmit="return check()"> <table width="80%" border="0" cellspacing="0" cellpadding="0"> <tr><td colspan="2"> </td></tr> <tr> <td valign="top" class="fields">Project Name : </td> <td><input type="text" name="txtproj_name" id="txtproj_name" /><br /></td> </tr> <tr><td colspan="2"> </td></tr> <tr> <td valign="top" class="fields">Scope of Project</td> <td><textarea name="txtproj_scope" id="txtproj_scope" rows="10" cols="50" ></textarea></td> </tr> <tr><td colspan="2"> </td></tr> </tr> <tr> <td colspan="2"> <div style="border:thin solid #000000; overflow:scroll; width:800px; height:300px;"> <input name="moreMods" type="button" id="moreMods" onclick="addInput();" value="Add Module" /><br /><br /> <div id="myform"></div> </div> </td> </tr> <tr><td colspan="2"> </td></tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="submit" /></td> </tr> <tr><td colspan="2"> </td></tr> </table> </form> </td> </tr> </table></td></tr> <tr> <td height="15" valign="top" bgcolor="f6f7f2"><?php //include("footer.php") ?></td> </tr> </table> </body> </html>
  9. If you have the ability to use PHP, you shouldn't use JavaScript at all for this type of functionality. JavaScript is dependant on whether the user has JS enabled and there can be cross-browser incompatabilities. With PHP, all the work is done server-side and is independant of the user's browser.
  10. Yes, you can absolutely have while loops within while loops. The problem is that you use mysql_free_result() after the first loop. So, there is no more data when the loop runs the second time. You would want to reset the pointer to the first row in the result set. But, there is a MUCH easier solution to your problem. Just create the select list once as a variable, then redisplay it as many time as you want. I would also keep all the PHP and HTML separate. Makes it much easier to work with in my opinion <?php $players="SELECT * FROM player ORDER BY firstname"; $playerResult =mysql_query($players); $temp = $homeTeamScore; $i=0; //Create the common select list $select_list = "<select name=\"scorer[]\">\n"; while ($row = mysql_fetch_array($playerResult)) { $select_list .= "<option value=\"{$row[0]}\">".ucwords($row[2])." ".ucwords($row[3])."</option>\n"; } $select_list .= "</select>\n"; mysql_free_result($playerResult); //Create the table rows $rows = ""; for($count=1; $count<=$temp; $count++) { $rows .= "<tr>\n"; $rows .= " <td width =\"150\">Goal Scorer $count :</td>\n"; $rows .= " <td width=\"330\">$select_list</td>\n"; $rows .= "</tr>\n"; } ?> <form action="addBlaydonResultProcess.php" method="post" enctype="multipart/form-data" name="addGS"> <input name="GS" type="hidden" value="" /> <table width="400" border="0"> <?php echo $rows; ?> <tr> <td width ="50"></td> <td width="300"><input name="btnUpload" type="submit" id="btnUpload" value="Add Goalscorers" /></td> </tr> </table> </form>
  11. That won't work in FireFox. If you create "custom" tag parameter - such as 'cmd' above, you cannot reference them via object.parameter in FireFox. You have to use: object.getAttribute('cmd') But, personally, I would go with the 3rd option I posted earlier. However, using a switch will be problematic in many situations. When dealing with a dynamic page, the JavaScript will need to be built dynamically as well. It is not impossible to do so, but it is more difficult to create/maintain the javascript programatically. For example, if you are displaying a list of records from a database and based upon certain fields in the record you will have custom JavaScript actions (e.g. a list of images with a different mouseover event based upon whether the image is in a specific group). That would require that you build the JavaScript and the HTML separately witin the same process. Again, that is perfectly doable, and sometimes absolutely necessary. But, it does make it more difficult, IMHO. In any case, I think what you presented above has merit. I'd just suggest you build it out a little more using some real-world examples.
  12. Here is a watered down version of a script I have <html> <head> <script type="text/javascript"> window.onload = function() { searchFocus(document.getElementById('search'), false); } function searchFocus(fieldObj, focusBool) { //trim the field value fieldObj.value = fieldObj.value.replace(/^\s+|\s+$/g,''); //See if the prompt text has been defined if (fieldObj.getAttribute('prompt')) { promptText = fieldObj.getAttribute('prompt'); //If onfocus & value equals the prompt text if (focusBool && fieldObj.value==promptText) { fieldObj.value = ''; } //If onblur & value is empty else if (!focusBool && fieldObj.value.replace(/^\s+|\s+$/g,'')=='') { fieldObj.value = promptText; } } return; } </script> </head> <body> <input type="text" name="search" id="search" onfocus="searchFocus(this, true);" onblur="searchFocus(this, false);" prompt="Enter search string" /> <br /> <input type="text" name="otherfield" /> </body> </html>
  13. Not bad. But, this would probably be better as a tutorial instead of a sticky. Also, you should cover methods of using parameters within functions. For example, if you have a single process which is performed by mutiple buttons (but with different parameters) you can't specify a parameter when assigning an event handler. See the following: <script type="text/javascript"> window.onload = function() { myButton1 = document.getElementById('myButton1'); myButton2 = document.getElementById('myButton2'); //myButton1.onclick = commonFunc('bar'); // NOT CORRECT //myButton2.onclick = commonFunc('other'); // NOT CORRECT myButton1.onclick = commonFunc; // CORRECT - but no parameter myButton2.onclick = commonFunc; // CORRECT - but no parameter } function commonFunc(foo) { //Do something with parameter foo } </script> So, instead of putting an event handler in-line with the HTML such as <button id="myButton1" onclick="commonFunc('bar')">Label</button> You would have to include some method to allow the specification of parameters when assinging event handlers. There are several ways to do this. 1. Add "custom" parameters to the HTML and reference in the common function <button id="myButton1" foo="bar">Label</button> 2. Add the parameters directly to the object and reference in the common function myButton1 = document.getElementById('myButton1'); myButton1.foo = 'bar'; 3. Create a custom function for each button myButton1 = document.getElementById('myButton1'); myButton2 = document.getElementById('myButton1'); myButton1.onclick = function() { commonFunc('bar'); } myButton2.onclick = function() { commonFunc('other'); } however, in my opinion, separating the event handlers from the HTML elements makes it more difficult for management and debugging. If there's a problem I can see the function call on the element that generated the error. If not, I have to dig through the onload JavaScript to see how the even handler may have been assigned.
  14. If I am understanding you correctly, the user may add different products from multiple restaurants - BUT you do not want them to add the same product from different restaurants. How about something like this: class ShoppingCart { private $items = array(); //*********FUNCTION ADDITEM********* public function AddItem($product_id, $rest_id) { if(array_key_exists($product_id, $this->items) && $this->rest_id[$product_id] == $rest_id) { $this->items[$product_id] = $this->items[$product_id] + 1; } else { $this->items[$product_id] = 1; $this->rest_id[$product_id] = $rest_id; } } } Although I can think of several ways to handle this based upon how you need the functionality to work.
  15. Right. So instead use this structure: $_SESSION['cart'][ItemID] = Quantity; $_SESSION['cart'][4] = 4; $_SESSION['cart'][3] = 1;
  16. Well, you have to create it like that. I'm assuming you have code already developed that creates the array when someone adds an item to the cart and it always adds a new element to the array. You will need to modify that code so it increments any existing elements. Simple example for a function to add a single qty to an item in the cart $cart = array(); function addItem($itemID) { global $cart; if(!isset($cart[$itemID])) { $cart[$itemID] = 1; return; } $cart[$itemID]++; } Again, that is a "simple" example. A real solution would probably allow for the value to be set to a fixed qty or to increment/decrement the quantity as needed. It all depends on your particular needs.
  17. Is it easier to explain in other forums? Seriously though, try to explain your needs in a non-technical way and let the discussion proceed from there.
  18. Don't believe so - at least not directly. There might be a way to check through the server logs/data to see if it "sent" all the packets for the file. But that is still no guarantee that they all made it to the client.
  19. If the current quantity is 3 and you want to change it to 10, then simply update the value to 10. Current cart: $cart = array( //ID => Qty '3' => 1, '4' => 3, '5' => 1 ) Then if you need to change the quantity for product with the ID of 3 you would simply state $cart[3] = 10;
  20. Why not just have a quantity for each item: array( //ID => Qty '3' => 1, '4' => 3, '5' => 1 ) EDIT: Permiso beat me to it
  21. Assuming you are dealing with records from a single table and each record has a unique ID, here is an example of how it could be done Form page: //Query for the current records $query="SELECT id, field1, field2 FROM table"; $result=mysql_query($query) or die(mysql_error()); //Open the form echo "<form action=\"somepage.php\" method=\"POST\">\n"; //Create input fields for each record with current values //Each field is an array with the id as the index while($row = mysql_fetch_array($result)) { echo "<input type=\"text\" name=\"ud_field1[{$row['id']}]\" value=\"{$row['field1']}\" />\n" echo "<input type=\"text\" name=\"ud_field2[{$row['id']}]\" value=\"{$row['field2']}\" /><br>\n" } echo "<input type=\"submit\" value=\"submit\">\n"; echo "</form>\n"; Processing page (no validation/error handling included) //Iterate through each input "set" and run query foreach($_POST['ud_field1'] as $id => $field1) { $field2 = $_POST['ud_field2'][$id]; $query = "UPDATE table SET field1='{$field1}', field2='{$field2}' WHERE id='{$id}'"; $result=mysql_query($query) or die(mysql_error()); }
  22. Yes, absolutely. I am by no means a SQL guru though. I would suggest prosting in the MySQL forum. But, looking at your first two queries, I *think* you could accomplish getting all of the same data without the two looping queries with just one query such as the one below. I have not tested it so I can't say if it will work. But, I know there is a solution, just not an expert in the syntax SELECT t1.subid as p_subid, t1.subcategory_title as p_subcategory_title FROM fr_subcategories1 t1 JOIN (SELECT * FROM fr_subcategories1 WHERE catid=t1.subid AND parent_level=3 ORDER BY priority LIMIT 6) AS t2 ON t1.subid = t2.catid WHERE t1.catid=7 AND t1.parent_level=2 ORDER BY t1.priority, t2.priority
  23. I assume these loops contain nested queries? Sounds like you need to rethink your logic so you don't have nested loops. I second that. Based upon many samples of code I have seen posted in these forums I would be that you are using three sets of nested queries when you could get all of your data with just one. Post your code around these loops and we may be able to help reduce the processing time considerably.
  24. I typically have my forms post to themselves so that if validation fails I can redisplay the form with the data the user entered. It also makes it easier to handle select, radio buttons, etc. I will create an array at the top of the page and use that to create my radio buttons, select lists, etc. and the use that same list for validation. Declare at top of page $os_list = array('Windows Vista', 'Windows XP', 'Mac OS', 'Linux'); Create OS Radio Button List <div class="operatingSystem"> <span class="system"> <label for="operatingSystem">Operating System.</label> </span> <span class="label_lineheight"> <br /> <?php foreach($os_list as $os) { echo "<input name=\"operatingSystem\" type=\"radio\" class=\"system_checkBox\" value=\"$os\" />\n"; echo "$os\n"; } ?> </span> </div> Validate the radio button list <?php if (in_array($_POST['operatingSystem'], $os_list)) { $validation = true; //Do something } else { $validation = false; //Do something else } ?>
  25. If you ONLY need to read/process the last line, there's no need to loop through all of the lines first: //Read file as an array $lines = file('path/to/file/filename.txt'); //Assign the last line, as an array split by CSV, to a variable $last_line = str_getcsv(array_pop($lines), ',');
×
×
  • 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.