Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Thank you. I guess I started using JavaScript about 10 years ago. Other than a very superficial 1 day class in JavaScript all of my skills have been self-taught. Most of my learning has been done by 1) reviewing scripts by other people and breaking them down so I understand them, 2) Reading tutorials - especially when there is one specific to a new "thing" I want to do (e.g. AJAX), 3) Reading other "documentation" at good sites such as devguru.com, tizag.com, or w3schools.com. I don't own any JavaScript books, and the only programming books I have I aquired through various employment. All the information I need is available on the internet. You just have to be careful about the source of the information. Many of the "free script" sites contain really bad code in my opinion. But, they are useful for a starting point if you know what you are doing.
-
It is very easy to view the source of external JS files. Just look in the code of the HTML page for any references to external JS files and then put that URL into your browser and . . . presto, you can download the JS file. It's your site, but IMHO, I've never understood why some people go through great lengths to try and prevent the casual user from certain actions as a security feature (e.g. disabling right-click) when it is the advanced users that need to be protected against. Especially when those extra measures can really foul up the site depending upon which browsers are being used. But, as long as you have what you want, all is well.
-
Try this. The function takes four parameters: imgID = the id of the image maxWidth = the maximum horizontal distance to move the image moveStep = the number of pixels to move the image on each step through the function revs = the number of revolutions to move the image until the function stops You also need to set the global variables revCount and startMargin outside the function. <html> <head> <script type="text/javascript"> function animate(imgID, maxWidth, moveStep, revs) { imgObj = document.getElementById(imgID); var currentMargin = parseInt(imgObj.style.marginLeft); var moveStep = parseInt(moveStep); if (revCount==0) { revCount = 1; startMargin = currentMargin; } if ( (currentMargin+moveStep)>maxWidth || (currentMargin+moveStep)<startMargin ) { moveStep = moveStep * -1; } var newMargin = ( currentMargin + moveStep ); imgObj.style.marginLeft = newMargin+'px'; if (newMargin==startMargin) { revCount++; } if (revCount<=revs) { setTimeout("animate('"+imgID+"','"+maxWidth+"','"+moveStep+"','"+revs+"')", 30); } else { revolutions = 0; } } var revCount = 0; var startMargin = 0; </script> </head> <body> <img src="wisk.jpg" id="wisk" style="margin-left:0px;"> <br /> <button onclick="animate('wisk', 600, 20, 3);">Animate</button> </body> </html>
-
As NoorAdiga stated, having looping queries such as that is terribly inefficient. Learn how to do JOINS and you will unlock the real power of a relational database. Also, theres no need to select '*' when you only need a particular field. This single query would get you the same result $query = "SELECT `group.name` FROM `group` JOIN `groupuser` ON `groupuser.gid` = `group.gid` WHERE `groupuser.user` = '$username'"; $result = mysql_query($query) or die (mysql_error()); while ($record = mysql_fetch_assoc($result)) { echo "Name{$record['name']}<br />"; }
-
OK, but I hope you realize that any security holes you think this is closing are still there. Anyone can look at the source of a page and manipulate it however they wish.
-
Why not just have two versions of the wisk image. One that is static and one that is an animated gif? You can then swap out the images instead of trying to mainipulate the content on the page. In any event this is an entirely different question than you originally posed and should be a new thread.
-
Here's a very simple example. It requires that you have the images (one.jpg, two.jpg & three.jpg) on the local computer inthe folder "C:\img\". However, the same logic would work for images on a web server. <html> <head> <script> function showImg(selID) { selObj = document.getElementById(selID); selValue = selObj[selObj.selectedIndex].value; window.open('c:\\img\\' + selValue + '.jpg'); } </script> </head> <body> <select name="img" id="img"> <option value="one">One</option> <option value="two">Two</option> <option value="three">Three</option> </select> <button onclick="showImg('img');">Show Image</button> </body> </html>
-
And where's your code?
-
<html> <head> <script> var ingredientNames = new Array('eggs', 'flour', 'milk'); var ingredientMeas = new Array('' , 'cups' , 'cups'); var recipies = new Array(); recipies[0] = '2.4.4-Cookies'; recipies[1] = '1.4.1-Bread'; recipies[2] = '0.4.1-Pie Crust'; var ingredientCount = new Array(); for (i=0; i<ingredientNames.length; i++) { ingredientCount[i] = 0; } function addIngredient(itemName) { for (i=0; i<ingredientNames.length; i++) { if (itemName==ingredientNames[i]) { ingredientCount[i]++; break; } } var ingredientsHTML = ''; for (i=0; i<ingredientNames.length; i++) { if (ingredientCount[i]) { ingredientsHTML += ingredientNames[i] + ': ' + ingredientCount[i] + ' ' + ingredientMeas[i] + '<br>'; } } document.getElementById('ingredients').innerHTML = ingredientsHTML; } function mixIngredients() { var userIngredients = ingredientCount.join('.'); var recipieFound = false; for (i=0; i<recipies.length; i++) { index = recipies[i].indexOf('-') recipieIngredients = recipies[i].substr(0, index); recipieName = recipies[i].substr(index+1); if(userIngredients==recipieIngredients) { recipieFound = true; break; } } if (recipieFound) { alert('You have chosen to make ' + recipieName); return true; } else { alert('we suggest you dont make that, it won\'t be nice...try again'); for (i=0; i<ingredientCount.length; i++) { ingredientCount[i] = 0; } addIngredient(false) return false; } } </script> </head> <b>Recipies:</b><br> <ul> <li>Cookies: <ul> <li>2 Eggs</li> <li>4 Cups Flour</li> <li>4 Cups Milk</li> </ul> </li> <li>Bread: <ul> <li>1 Egg</li> <li>4 Cups Flour</li> <li>1 Cup Milk</li> </ul> </li> <li>Pie Crust: <ul> <li>4 Cups Flour</li> <li>1 Cup Milk</li> </ul> </li> </ul> <table border="5" cellspacing="5" cellpadding="5"> <tr> <td width=100px, height=80px><input type="image" src="images/eggs.jpg" width="60px" height="80px" onClick="addIngredient('eggs');"; <br /> eggs</td> <td width=100px, height=80px><input type="image" src="images/flour.jpg" width="80px" height="80px" onClick="addIngredient('flour');"; <br /> flour</td> <td width=100px, height=80px><input type="image" src="images/milk.jpg" width="40px" height="80px" onClick="addIngredient('milk');"; <br /> milk</td> </tr> </table> <br /> <b>Ingredients:</b><br> <span id="ingredients"></span> <br><br> <button onclick="mixIngredients();">Mix Ingredients</button> <html>
-
First lay out the page. Put in placeholder text for where you want text to appear. Next step would be to create a function or functions to perform the different processes. If this is a fixed list of ingredients I would probably use global variables to keep count of how many of each item are used. It really kind of difficult to only give a little help. if you don't want someone to provide the solution make an attempt at solving it yourself and then posting what you come up with.
-
I have a logic issue and I just can't figure it out ... pls help!
Psycho replied to phporcaffeine's topic in PHP Coding Help
Yeah, you definitely need to provide more details. If you were to combine the two examples you posted what would the result be: - 193.122.140.207 - 193.122.140.20 - 193.122.140.80 - 193.122.159.255 Would it be - 193.122.140.20-207 - 193.122.159.255 Or just: - 193.122.*.* I would start by putting all of the IPs into multiple-dimensional arrays with each octet as a separate array element. Then sort by each octet. You would them have them in a logincal order to process - once you have clear rules for processing them. -
OK, ther are a few problems. Do you know that you can have the JS errors displayed when they occur? You can set the browser to always display. But even if they don't you can double-click the error icon at the bottom of the browser to see the error. This will help you in finding and fixing the errors. Anyway: Problem 1 enableEdit(document.getElementbyId('cabinet_lst'), 'edit_cab'); enableEdit(document.getElementbyId('folder_lst'), 'edit_fol'); enableEdit(document.getElementbyId('item_lst'), 'edit_item'); It should be getElementById() - with a capital B Problem 2 if (selVal!='Show all') { editObj.disabled=false; } else { editObj.disabled=true; } The test value should be 'showall' NOT 'Show all'. Plus, you can replace all of those lines with just a single line: editObj.disabled = (selVal=='showall'); Problem 3 window.onload=function() { enableEdit(document.getElementById('cabinet_lst'), 'edit_cab'); enableEdit(document.getElementById('folder_lst'), 'edit_fol'); enableEdit(document.getElementById('item_lst'), 'edit_item'); } The last function call is passing 'edit_item' as the button ID. But there is no such button - you combined the Load/Edit buttons into one. not sure if you want to disable this button or not. You need to either remove that line or make the necessary changes to support that button.
-
Javascript help- headings need to turn red when clicked
Psycho replied to technotool's topic in Javascript Help
Try this. It doesn't require you to create the two arrays for managing the elements - and I think it works as you would want. But I'm just guessing on that part. <script type="text/javascript"> function init() { show(1); // document.getElementById('subj_txtOutput').focus(); } function show(section) { var sec_count = 0; var sec_no = 1; while (document.getElementById('heading-'+sec_no)) { headObj = document.getElementById('heading-'+sec_no); if (sec_no == section) { headObj.style.color = (headObj.style.color !='#ff0000') ? '#ff0000' : '#000000'; } if (headObj.style.color=='#ff0000') { sec_count++; } sec_no++; } sec_no = 1; while (document.getElementById('part-'+sec_no)) { document.getElementById('part-'+sec_no).style.display = (sec_no==sec_count) ? 'block' : 'none'; sec_no++; } return; } </script> <html> <body onload="init();"> <span id="heading-1" onClick="show(1);">HeadingOne</span> | <span id="heading-2" onClick="show(2);">HeadingTwo</span> | <span id="heading-3" onClick="show(3);">HeadingThree</span> | <span id="heading-4" onClick="show(4);">HeadingFour</span> <div id="part-1" style="display:none;">stuff</div> <div id="part-2" style="display:none;">more stuff</div> <div id="part-3" style="display:none;">even more stuff</div> <div id="part-4" style="display:none;">too much freakin' stuff already</div> <html> -
Javascript help- headings need to turn red when clicked
Psycho replied to technotool's topic in Javascript Help
It worked for me after I fixed one typo. You define the array "sectionsHeadings", but in the function show() you refer to it as "sectionHeadings" (without the s). Your code is malformed as well. The last HTML tag is an opening tag, not a closign tag. Also, you should move the javascript code within the HTML tags - I would suggest putting it within HEAD tags as well. But, I don't know if your code is doing what you really want it to do as the logic seems awkward to me. -
If the current code is still running the 'reload' AND the 'enable' functions then it will not work. Once you initiate a page reload and other function calls will be dropped. However, if you have an appropriate onload function it should handle it appropriately. How complex are the lists that need to be populated into the select lists? You could include all the data for those lists in the javascript and repopulate them dynamically. But, looking at the code you just posted, the line you commented out is the one that is required for disabling/enabling the edit button. Don't know why you commented it out. But, it is also the line you had a question about. editObj.disabled=(selVal=='Show all') ? true : false; This is what is called a Ternarry (sp?) Operator. It is basically a shorthand if/else statement. In this particular line it is setting the editObj.disabled value to true or false depending upon the condition (selVal=='Show all'). This could be rewritten as this: if (selVal=='Show all') { editObj.disabled = true; } else { editObj.disabled = false; } However, after reviewing my own code I see I could have simply used this: editObj.disabled=(selVal=='Show all') Snce the condition will result in true/false.
-
If you are getting this data from a csv file, then all you need is to use mysql_real_escape_string() on the data before including it in the query. Showing some of your code will let us help you better.
-
You could also use mysql_fetch_assoc(). mysql_fetch_row(): returns numerically indexed array mysql_fetch_assoc(): returns array indexed by the column names mysql_fetch_array(): returns one or both of the above (withthe appropriate switch) All fo this information is readily available in the manual at www.php.net
-
function getURLVar(varName) { url = unescape(self.document.location); if (varstring = url.substring(url.indexOf('?')+1)) { varpairs = varstring.split(/&/); for (i=0; i<varpairs.length; i++) { varpair = varpairs[i].split(/=/); if (varName==varpair[0]) { return varpair[1]; } } } return false; } var cab_id = getURLVar(cab); var fol_id = getURLVar(fol); var item_id = getURLVar(item);
-
Calling Javascript function with variables in PHP
Psycho replied to pdhharris's topic in Javascript Help
The probelem is probably due tot he fact that the page isn't fully loaded and you are trying to add elements. But, there's no need to use JavaScript. Just use the PHP code to create those elements. You're making your page way to dependant on JavaScript when it doesn't need to be and shouldn't be. <p>Program Date(s): <!--- If dates are set, re-add them ---> <font color="#FF0000"><strong><?php echo $dateErr ?></strong></font> <div id="area"></div> <br /> <?php $date_count = 0; if($DATES_SET == 1) { foreach($_POST["date"] as $theDate) { $date_count++; echo "<div id=\"my{$date_count}Div\">"; printf("<input type='text' name='date[]' value='%s'/>", $theDate); echo "</div>\n"; } } ?> <a onclick='addArea("01-02-2009")' href='#'>add a date</a> <br /> <input type='text' value='<?php echo $date_count; ?>' id='intVal'/> </fieldset> <br /> <fieldset> -
Calling Javascript function with variables in PHP
Psycho replied to pdhharris's topic in Javascript Help
Need more info here. What is the JS function addArea() supposed to do? Can't provide any reason why it should/should not work within the loop if we have no clue what it is doing. -
This is the Javascript forum, not the PHP forum. You should just provide just the post-processed code (i.e. not the PHP; run the page in your browser then capture the code and post). It is much easier to work with javascript from a working page. We don't have your database to test against. I'm having a problem following your code. Each of the onchange triggers first runs the related 'reload' script and then the related 'enable' script. But, the 'reload' scripts will all reload the page, so the other script will never run. Not sure what you are trying to accomplish with the reload scripts. Also, having multiple functions that do the same things is inefficient and error prone. Create single functions and pass the appropriate variables. So, I have no idea what you are trying to accomplish with the "reload" functions (should they be associated withthe LOAD buttons?), but this will work for enabling/disabling the EDIT buttons onchange and onload <html> <head> <script type="text/javascript"> window.onload=function() { enableEdit(document.getElementById('cabinet_lst'), 'edit_cab'); enableEdit(document.getElementById('folder_lst'), 'edit_fol'); enableEdit(document.getElementById('item_lst'), 'edit_item'); } function enableEdit(selObj, editID) { var editObj = document.getElementById(editID); var selVal = selObj[selObj.selectedIndex].value; editObj.disabled = (selVal=='showall') ? true : false ; } </script> </head> <body> <form method='post'> <table width='100%'> <tr> <td colspan='3'> <b>ORGANIZATION EXPLAINATION<br> <a href='help.php#1.a'>Cabinets</a> are broken down<br> into folders and <a href='help.php#1.b'>folders</a> into <a href='help.php#1.c'>items</a>.</b> </td> </tr> <tr> <td width='30'>Cabinets</td> <td width='250'> <select name='cabinet_lst' id="cabinet_lst" onchange="enableEdit(this, 'edit_cab');"> <option value='showall'>Show all</option> <option value='test'>test</option> </select> </td> <td> <input type='submit' name='cabinet' class='button' value='LOAD'> <input type='submit' name='edit_cab' id='edit_cab' class='button' value='EDIT'></td> </tr> <tr> <td>Folders</td> <td> <select name='folder_lst' id="folder_lst" onchange="enableEdit(this, 'edit_fol');"> <option value='showall'>Show all</option> <option value='test'>test</option> </select></td> <td> <input type='submit' name='folder' class='button' value='LOAD'> <input type='submit' name='edit_fol' id='edit_fol' class='button' value='EDIT'> </td> </tr> <tr> <td>Items</td> <td> <select name='item_lst' id='item_lst' onchange="enableEdit(this, 'edit_item');"> <option value='showall'>Show all</option> <option value='test'>test</option> </td> <td> <input type='submit' name='item' class='button' value='LOAD'> <input type='submit' name='edit_item' id='edit_item' class='button' value='EDIT'> </td> </tr> <tr> <td colspan='3'><hr></td> </tr> </table> </form> </body> </html>
-
[SOLVED] Validate - numbers and decimals only for money field
Psycho replied to Jason28's topic in Javascript Help
function valid_number(fieldObj) { if (fieldObj.value!='' && isNaN(fieldObj.value)) { alert('You must enter a valid number'); fieldObj.select(); fieldObj.focus(); return false; } return true; } <input type="text" name="cost" id="cost" onchange="valid_number(this);"> -
[SOLVED] Validate - numbers and decimals only for money field
Psycho replied to Jason28's topic in Javascript Help
Why would you want to loop through each character when there are much better alternatives? That function also has a bug in that it would accept input with multiple decimals. Of course you could use the built-in function within JavaScript, isNaN(), which will return true if the value is not a number, else it returns false. function valid_number(inputVal) { return (!isNaN(inputVal)); } Of course, if you're talking about a currency field you might also want to round to the nearest two decimal places. However, you can handle the problem of invalid input in a few different ways:remove non-numeric characters, remove all input if any character is not valid, etc. It's up to you. -
Why would you not allow '+' (plus) or '.' period? those are perfectly acceptable characters for an email - a period is a very common character for an email. But, you can modify it if you wish. This shoudl do it as you asked, but I havent tested it: function is_email($email) { $formatTest = '/^[a-z0-9][-\w]+@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); }
-
Yes, I forgot to add that (I also used a '=' instead of '+' when creating the array_to_string to add it at then end of that) - thanks. Fixed: function in_array(searchArray, searchString) { var array_to_string = '~' + searchArray.join('~') + '~'; return ( (array_to_string.search('~'+searchString+'~')!=-1) ? true : false); }