Jump to content

gamblor01

Members
  • Posts

    73
  • Joined

  • Last visited

Profile Information

  • Gender
    Male
  • Location
    Austin, Texas

gamblor01's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi all, Does anyone know of a good way to track the events that get fired inside of a page? I am working on something at work and I'm not sure if I have discovered a bug in Chrome or not. The problem ONLY occurs in Chrome (and Chromium) on Linux -- when running Chrome in Windows it does not occur. We have an event handler and I am showing the events that we are receiving in a div. We are overriding the default right-click menu in the browser, so when you right-click you will not see the options "Back, Forward, Reload, etc." Instead you see our own context menu. The crazy thing is that in Linux, if you click near the top of the screen, the context menu will open BELOW the current location of the mouse pointer and the mouseup event fails to fire. If you click low enough on the screen such that the context menu must open above the mouse pointer, then mouseup fires correctly. Also, I have noticed that blur isn't always being called correctly, so in a special admin mode that we have, the context menu will open but never gets focus. Thus, the element BEHIND it (which was the element where the right-click was executed) is still in focus and you can drag it around the page. I think that ultimately I need to report this to Google as a bug but first I need: a) a simple example (since they obviously won't have access to our application) -- this is something I have to come up with on my own b) a simple way to show the events being fired in the browser (since my current example requires use of my application which again -- developers won't have access to) Is there an extension I can run or something in the dev tools that can show the events being processed? Thanks!
  2. Hmm...well I sort of got things working by using dojo. The following works to dynamically load the external .js file: function addJavaScriptFile(path) { dojo.io.script.get ( { url: gpsGateURL, load: function() { // signal that this has been loaded already gpsGateLoaded = true; } }); } However, it turns out my isReachableURL() function (which was previously commented out) doesn't work. Apparently this is due to the same-origin policy: http://stackoverflow.com/questions/5573360/test-url-availability-with-javascript This is a bit of a bummer because I'm not sure how to solve this yet. I can't let the server test for the existence of the URL because the whole point is that the URL is local to the client (i.e. localhost is the client system and *NOT* the server). GpsGate retrieves coordinates and then outputs that information to the "browser". This allows client systems to have an attached GPS and our system to show their coordinates. If the GPS were attached to the server then it would be pointless -- it would show the server's location and not the end-user's location. I was originally trying to test for the existence of the URL on the server side (in Java) until I realized that it was always returning false because GpsGate wasn't running on the server. I'm not sure how to solve this problem yet.
  3. Hi all, I have done some search on the net and found some solutions to dynamically load up JavaScript files. It looks pretty simple, just reference the HEAD element of the document via DOM and append a child. I only want to do this in the case that a particular JavaScript file is available, and I believe I have an AJAX solution do that. Here are the two functions that I wrote: function isReachableURL(url) { var request = false; if (window.XMLHttpRequest) { request = new XMLHttpRequest; } else if (window.ActiveXObject) { request = new ActiveXObject("Microsoft.XMLHttp"); } if (request) { request.open("GET", url); if (request.readyState == 4 && request.status == 200) return true; } return false; } function addJavaScriptFile(path) { var fileRef = document.createElement('script'); fileRef.setAttribute("type","text/javascript"); fileRef.setAttribute("src", path); fileRef.setAttribute("id", gpsGateDomId) document.getElementsByTagName("head")[0].appendChild(fileRef); } Armed with these two functions, I can test for the existence of .js file on an external server and reference it if it's reachable. The reason I am doing this is to facilitate some GPS functionality in the application that I work on. We are relying on a program called GpsGate to gather coordinates from a USB/bluetooth GPS device. GpsGate works by launching a small HTTP server that hosts some JavaScript files, and via cross scripting you can obtain your position, altitude, etc. More info can be found here: http://forum.gpsgate.com/topic.asp?TOPIC_ID=6102 Their server is ALWAYS at http://localhost:12175/javascript/GpsGate.js -- assuming that GpsGate is running of course! So my goal is to add a script tag to my page that references http://localhost:12175/javascript/GpsGate.js as the source. In fact, I did this and all of my code works fine. The problem is, for users that do not have GpsGate installed, the browser will output an error that it was unable to load that JavaScript file. For example, if you go into the developer tools in Chrome (CTRL+SHIFT+I) and look, there will be an error that says: GET http://localhost:12175/javascript/GpsGate.js undefined (undefined) Of course other developers are complaining about this and our users will no doubt complain eventually as well. So instead of placing this file into the HEAD tag of the page every time, I want to append it dynamically (only in the case that the user has chosen to do something which involves GPS). I am able to dynamically add the child element to HEAD -- I see it get added in Chrome's debugger if I inspect the children of document.getElementsByTagName("head")[0]. However, it still seems that the typeof(GpsGate) is undefined. If I'm understanding correctly, the SCRIPT tag seems to be getting appended under HEAD, but it's almost as if that script is never executed. Do I need to do something special for that script file to execute after appending it in the DOM tree? If it helps, here is the script file that I see when I hit http://localhost:12175/javascript/GpsGate.js in my browser. It's some strange anonymous function syntax that I'm not familiar with in JavaScript. Any body know what to do to get this function to execute in JavaScript after appending the SCRIPT tag in the DOM tree? I added an alert() in this script to see if it would print anything when I added it in the DOM tree and it did not -- reinforcing the idea that it's being appended in DOM but never executed. If I add the script manually into the HEAD tag then it does show my alert box. // // Copyright Franson Technology AB - GpsGate.com // franson.com - gpsgate.com // (function() { if (typeof(GpsGate) == 'undefined') { GpsGate = {}; } var _url = 'http://localhost:12175/gps'; var _scriptCounter = 0; function buildQueryString(params) { var strParams = ''; for (prop in params) { strParams += '&' + encodeURIComponent(prop) + '=' + encodeURIComponent(params[prop]); } return strParams; } function xssCall(methodName, params, callback) { var id = _scriptCounter++; var scriptNodeId = 'GpsGateXss_' + id; var poolName = methodName + '_' + id; GpsGate.Client._callback[poolName] = function(/*arguments*/) { var scriptNode = document.getElementById(scriptNodeId); scriptNode.parentNode.removeChild(scriptNode); delete GpsGate.Client._callback[poolName]; scriptNode = null; callback.apply(this, arguments); }; var callUrl = _url + '/' + methodName + '?jsonp=' + ('GpsGate.Client._callback.' + poolName) + buildQueryString(params); var script = document.createElement('script'); script.type = 'text/javascript'; script.id = scriptNodeId; // script.charset = 'utf-8'; // necessary? var noCache = '&noCache=' + ((new Date()).getTime().toString().substr(5) + id); script.src = callUrl + noCache; // todo: use this method on non-conforming browsers? (altough both IE6 and PC-Safari seems to work anyway) // document.write('<script src="' + src + '" type="text/javascript"><\/script>'); document.getElementsByTagName('head')[0].appendChild(script); script = null; } // API GpsGate.Client = { Copyright: 'Franson Technology AB - GpsGate', getGpsInfo: function(callback) { xssCall('getGpsInfo', {}, callback); }, getVersion: function(callback) { xssCall('getVersion', {}, callback); }, // ----- _callback: {} }; })();
  4. Putting the script in your crontab probably isn't working because the script was never marked as executable. You need to run the ls -l command on the file to see what the permissions are. If you don't know how to read the output then see these two entries: http://en.wikipedia.org/wiki/Ls http://en.wikipedia.org/wiki/File_system_permissions#Notation_of_traditional_Unix_permissions You'll probably need to run the chmod command to make it as executable (either for your user, or for everyone). The easiest way to do this is to simply run: chmod +x your_file_name You should also but the interpreter that you want to be used at the top of your script. For example, if you want it to be executed by Korn shell you should put something like: #!/bin/ksh For BASH it would be: #!/bin/bash In any case, if you get the cron job working with your script then it's going to constantly create those files but never delete them. Here's a little script that might help with that. First it will determine today's date, and then it will backup your database with the date in the filename. Finally it will look through the directory for files older than 7 days and delete them. Thus -- you always keeps the most recent week of files. If you want to keep 2 weeks worth of files, change the argument in find from -mtime 7 to mtime 14. #!/bin/bash -x backupDir=/home/myrefco/db # backup the database myDate=$(date '+%Y-%m-%d') dbName=db-$myDate.sql cd /tmp mysqldump -u root -ppassword database_name > $dbName gzip $dbName mv $dbName.gz $backupDir # delete anything older than 7 days find $backupDir -mtime +7 | xargs rm -rf I haven't thoroughly tested it so you should before using it. I'm pretty sure it will work but run it by hand and maybe even add the -x option (#!/bin/bash -x) so that you can see exactly what it's doing as it runs. Have fun.
  5. There are a few things you need to change. Not only do you need to change the name of the form from #form to submit, but you also need to invoke the AJAX code. He's using JQuery -- essentially a Javascript framework -- which (I don't think) is as straightforward as Javascript, but it's incredibly powerful with very little code. The basic idea behind AJAX is quite simple: - You create an XmlHttpRequest object - You submit the HTTP request via GET or POST - The server performs some processing and returns the result to the client - Your client does something with that response, which is generally to update the content of the page with information in the response In order for this to work however -- I think you need to do all of this through javascript. I think the reason that your page is redirecting is because you have defined the action of the form to redirect to "someautoresponderservice". Instead, what you need to do is get rid of that action there, and then update your function called by onSubmit to handle everything for you. The only bummer is that you will need to pass all of the values of the form as arguments which is a bit of a pain. It would look something like this: <form name="signup" id="signup" onSubmit="return checkSub(this.signup.action, this.signup.member, this.signup.auto, ..., this.signup.email)"> I can provide code if you want, but I think there is probably a better way to do this. Anyone else care to comment?
  6. I'm not sure if it's possible to reload an iframe or not. However, one thing you could probably do is to add a div tag to every iframe that you have. You might be able to put the ajax code in the "parent" page and then have it refresh the content inside of the div tag in the iframe. Of course, the ENTIRE content of the iframe would just need to be contained inside of that div tag. Try putting this javascript into your parent page: <script type="text/javascript"> var xmlhttp; function reloadIframe(page) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Sorry but your browser does not support AJAX or Javascript has been disabled"); return; } // append an random value to the page being called // to prevent browser caching var url = page + "?id=" + Math.random(); // execute the fully formed request xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("iframeContent").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } </script> In this example all you have to do is put a div tag with the id "iframeContent" in your iframe page (let's call it foo.php) like this: <?php echo "<div id=\"iframeContent\">\n"; // the page content goes here echo "</div>\n"; ?> Then your button should invoke the javascript function with the name of the target page. Something like: echo "<a href=\"javascript:reloadIframe('foo.php')\"><img src=...></a>\n"; Does that work? I dunno...I don't mess with iframes usually.
  7. Cool I didn't ever even think about checking if status==200 but that's a great idea! I learned something new! I was however, trying to make the code as simple as possible, just to illustrate the basic idea. That's cool -- switching between GET and POST is really pretty simple. I posted a thread on it a while back: http://www.phpfreaks.com/forums/index.php/topic,288125.0.html
  8. Hmm...what you are doing sounds a lot like something that I got working a while back. In my case I was specifically trying to get a table to reload without reloading the entire page, though reloading the entire page is certainly similar. What you need to do to is use the confirm() function in Javascript and save its return value in a variable. Then you can just test if the variable is true. If so then submit the form. You don't even need an else statement (which would just "do nothing" anyway). I have the code skeleton posted here: http://www.phpfreaks.com/forums/index.php/topic,287155.0.html In that post I actually do use an else statement to "do nothing" but that is only because the GET request is made outside of the entire if/else structure (so that I didn't have to repeat code inside of both the add and the delete sections). Are you sending a GET request or a POST request? If it's GET then you can essentially just copy my code and move things around so that it looks like this: <script type="text/javascript"> var xmlhttp; function deleteUser(user) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Sorry but your browser does not support AJAX or Javascript has been disabled"); return; } var doAction = confirm("Are you sure you want to delete id #"+user+"?"); if (doAction == true) { // we're deleting so append userID in the GET request var url = "deleteUser.php"; url = url + "?delete=" + user; // execute the fully formed request xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("myTable").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } </script> Then your backend script (in this case I have called it deleteUser.php) can simply check the value of $_POST['delete']. If it's not empty then delete the user from the DB and redirect to the page that displays the table of users.
  9. It's not really clear to me what you want to populate. I see the option in your form named chair but then you say it needs to populate the toto field and I don't even see that anywhere. I would suggest you start with the tutorial on w3schools here: http://www.w3schools.com/php/php_ajax_database.asp It demonstrates using AJAX to invoke a PHP script that returns data from a table in MySQL. It even matches up with what you are trying to do which is to change the output based off the selection of a downdown menu! Do you have something that is successfully returning value from PHP/MySQL already? If not, I suggest you get a simple example working. Then we can tweak it to do what you want.
  10. Hashing passwords is better than using them directly, but even better than that is to apply a salt and then hash. This way, 2 identical passwords will not be hashed to the same value (unless the users both have the same salt or there is a collision in your hash function, which is unlikely with good hashes such as md5 or sha1). I use a salt in my code: // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect against MySQL injection $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername'"; $result=mysql_query($sql); // mysql_num_row counts the number of rows returned $count=mysql_num_rows($result); // If result matched $myusername, table row must be 1 row // This means that the user exists in the database if($count==1) { // check the password $row = mysql_fetch_array($result); $storedPassword = $row['password']; $salt = $row['salt']; $encrypted_mypassword=md5($mypassword.$salt); if ($encrypted_mypassword == $storedPassword) { // do whatever you want first and then redirect to login_success.php header("location:login_success.php"); } } When a new user is created I simply generate a salt for them as a random number and then store that in the table with the rest of their information.
  11. Wow...indeed. I just read through it more thoroughly and realized there is a much simpler way to accomplish this than my script...as long as the ordering of the lines doesn't matter. Just use sed to delete the appropriate line from the file with a regular expression and then append the new line with a >> redirect. This makes for a much simpler 4 line solution. mv config.txt config.back sed -e '/^maxplayers/d' config.back > config.txt rm config.back echo "maxplayers $newPlayerCount" >> config.txt
  12. Sed is probably a great way to go. One thing you could do is to grep through the file for the attribute that you are looking for (for example, maxplayers) and then use cut or awk to get the value. If you're going to use cut then you might want to use "tr -s" first...just to compress all of the space characters into a single space. Taking your example you could use either one of the examples below: $ grep "maxplayers" config.txt | awk '{print $2}' 25 $ grep "maxplayers" config.txt | tr -s ' ' | cut -d ' ' -f 2 25 Now that you have that value, you can use it along with the name of the attribute to do a substitution (maybe even do it globally? Though I doubt that is necessary). You probably don't want to substitute just the value by itself, otherwise you might wind up changing other attributes that just happen to have the same value (which isn't what you want to do). Here is a quick shell script that works: #!/bin/bash confFile="config.txt" confBack="config.back" attribute=$1 newValue=$2 mv $confFile $confBack oldValue=$(grep "$attribute" $confBack | awk '{print $2}') oldString="$attribute $oldValue" newString="$attribute $newValue" sed -e "s/${oldString}/${newString}/" $confBack > $confFile # optional -- delete the backup file rm $confBack You can try it out for yourself: $ cat config.txt hostname Example port 7777 maxplayers 25 $ ./update.sh maxplayers 30 $ cat config.txt hostname Example port 7777 maxplayers 30 Perhaps not the most elegant solution but it definitely works!
  13. The easiest way to do this is going to take the combination of some javascript code, an additional div tag to your page, and a php script. You should start with some simply AJAX example. w3schools.com has a great tutorial for AJAX. I will give you a skeleton though. It should take the following changes below. You will need to fully implement getShipping.php however. 1. Start by defining the getShipping function in Javascript. This will probably reside in <script> tags in the <head> of your document: function getShipping(state, zip) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Sorry but your browser does not support AJAX or Javascript has been disabled"); return; } // we want to invoke getShipping.php var url = "getShipping.php"; // append the values from the form url = url + "?state=" + state; url = url + "&zip=" + zip; // execute the request xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("shippingCost").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } 2. Update your HTML so that it contains the div tag "shippingCost" and will therefore be updated once the AJAX request returns: <td align="right" width="148" bgcolor="#CACBCB"> <font color="#000000" face="Verdana" style="font-size: 8pt">S&H Subtotal:</font> </td> <td align="right" width="87" bgcolor="#CACBCB"> <font color="#000000" face="Verdana" style="font-size: 8pt"><b><div id="shippingCost"></div></b></font></td> <input type="hidden" name="shipping" value="0"> <td width="72" bgcolor="#CACBCB"> </td> 3. Update your form so that the "calculate" button calls the getShipping function: <br><br><input type='submit' value='Calculate' onClick="getShipping(this.form.tostate.value, this.form.tozip.value)"></td> 4. Finally, define the getShipping.php file that AJAX is calling. Here is a very simply example -- you should modify it to suit your needs and reflect a more real-world example: <?php $state = $_GET['state']; $zip = $_GET['zip']; if ($state == "HI" || $state == "AK") { return 8; } else if (substr($zip,0,3) == "787") // always ship free to Austin, TX { return 0; } else { return 5; } ?> Try it out and see if it all works. Let me know! It should work and populate the div tag with the shipping cost once you have selected a state/zip and pressed calculate. You will still need to retrieve this value and add it to the subtotal to update the total cost though. That is left as an exercise for the reader!
  14. I think the best way to accomplish this would be to perform an AJAX query (looks like you already knew that though). The easiest way to do this is to probably just modify the PHP code from earlier and just have it echo the total num rows that it finds ( "1" if the username exists, and "0" otherwise). I don't think you want to select count(*) and then call mysql_num_rows on that...otherwise you are ALWAYS going to get one row back -- the row that tells you how many rows are in the table. Anyway, let's assume you put this code into a file called checkUser.php: <?php // db connection info here // get the prefix and cusername out of the request $prefix = $_GET['prefix']; $cusername = $_GET['cusername']; $sql="select * from ".$prefix."users where username='$cusername'"; $result=mysql_query($sql, $db); $total=mysql_num_rows($result); echo "$total"; ?> Now just write a very simple function in AJAX (which can be copied into your existing Javascript code) to invoke that PHP page and retrieve the value: <script type="text/javascript"> var xmlhttp; function checkUser(prefix, cuser) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Sorry but your browser does not support AJAX or Javascript has been disabled"); return; } // call the checkUser.php script var url = "checkUser.php"; // append the prefix and cusername url = url + "?prefix=" + prefix; url = url + "&cusername=" + cuser; // execute the fully formed request xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { return xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } </script> Inside of your own Javascript code you can just do something like: var userExists = checkUser("foo", "bar"); if (var == 1) { // username already exists! } else { // allow the creation because the user doesn't exist yet } Something like that should work.
  15. Someone else just asked this very same question a few days ago: http://www.phpfreaks.com/forums/index.php/topic,292404.0.html You can look at that post and see my response or you can go directly to the thread that I started about this: http://www.phpfreaks.com/forums/index.php/topic,287155.0.html
×
×
  • 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.