Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Javascript equivalent of PHP mysql_real_escape_string
Psycho replied to RIRedinPA's topic in Javascript Help
There shouldn't be any need to make a string db safe on the client-side. The data has to go through server-side code before it can go to the database. Can you elaborate on why you want to handle this through javascript? -
A bit testy aren't you? I thought I did give some constructive comments. However, you seem to post comments based upon assertions you believe to be "facts" which are not. Well, in fact, you can access a database with Javascript. http://ajaxian.com/archives/server-side-javascript-databases-access I prefer to not make assumptions as to someone's level of expertise or knowledge. Didn't the first line of my post state "Yes, you need to use AJAX" - I thought I was agreeing with you. yes I made a spelling error, but it was not intentional. And "text speak", in my opinion, is polluting our communication. Really?
-
I don't believe you can do that directly. The only thing I know of that will natively change an array key is array_change_key_case() which only changes the case of all the keys in an array. What's wrong with simply creating a new key with the target value and destroying the old key? A simple function could do this for you: <?php function array_change_key(&$array, $old_key, $new_key) { $array[$new_key] = $array[$old_key]; unset($array[$old_key]); return; } ?>
-
No offense, but I would disagree. Your comment was of value, yet it lacked clarity. For example: You startt the sentence talking about a javascript function and then transition to "another script" without explaining that the other script would be a PHP, or other srver-side, script. That would easily cause confusion in my opinion. I tried to break it down into a logical flow for the OP. Also, I didn't denigrate the English language. U != You
-
Yes, you need to use AJAX. There are many tutorials on the net for using AJAX. I suggest you find a javascript AJAX class so you don't have to worry about all the issues in initiating and making the actual request. Here is an example of how the process could work: 1. The button/link to add a song would call a javascript "addSong" function, passing the ID of that song to the function. 2. The "addSong" function would take that ID and make an AJAX call to a PHP page (e.g. "addToPlaylist.php") 3. The PHP page would take the ID passed to it and add the song to the playlist in the database 4. The PHP page would then do a query to get the entire playlist and format it for display in the div and pass back that code to the javascript function (anything the PHP echo's to the page is passed back to the AJAX call). 5. When Javascript receives the formatted HTML from the AJAX call it populates it into the div using innerHTML. Hope that helps get you started.
-
how to hide/show label along with drop down menu?
Psycho replied to matt121400's topic in Javascript Help
You can make two calls within an even trigger like so: onclick="function1();function2()" But you shouldn't do that in this case. It is better to separate the javascript from the presentation. Also, you can make it much easier by just putting a span around the label and the select list and just changing the visibility of the span: <html> <head> <script type="text/javascript"> function showRelo(visiState) { document.getElementById('reloSpan').style.visibility = visiState; } </script> </head> <body> <input type="radio" name="relocate2" class="fieldradio" onclick="showRelo('visible');" />Yes <input type="radio" name="relocate2" class="fieldradio" onclick="showRelo('hidden');" checked="checked" />No <br /> <span id="reloSpan" style="visibility:hidden"> <label for="relocate">Relocation:</label> <select name="relocate" id="relocate"> <option value="No" checked='checked'>No Relocation</option> <option value=N"Alabama">Alabama</option> <option value="Alaska">Alaska</option> <option value="Arizona">Arizona</option> <option value="Arkansas">Arkansas</option> <option value="California">California</option> <option value="Colorado">Colorado</option> </select> </span> </body> </html> -
I prefer to use usort() (or uasort() if you need to maintain index associations). This is especially helpful with multidimensional arrays where you may need to add more logic than just simple sorting. <?php function sortByDate($a, $b) { if ($a[3] == $b[3]) { return 0; } return ($a[3] < $b[3]) ? -1 : 1; } usort($theArray, 'sortByDate'); ?> EDIT: Or you can make it a one liner like so <?php function sortByDate($a, $b) { return ($a[3] == $b[3])?0:(($a[3] < $b[3]) ? -1 : 1); } ?>
-
There are ALWAYS different solutions. For example you could simply run a query to get the results and then another to get the records. Althoug I agree that separating your logic and content makes the most sense, here is an alternative: <?php $sum_query = "SELECT SUM(number1+number2+number3+number4+number5) FROM numbers"; $sum_result = mysql_query($sum_query, $conn) or die(mysql_error()); $sum_record = mysql_fetch_array($sum_result); ?> <html> <body> <p><?php echo $sum_record[0]; ?></p> <table> <?php $get_num = "SELECT number1, number2, number3, number4, number5 FROM numbers"; $get_num_res = mysql_query($get_num,$conn) or die(mysql_error()); while ($numresult = mysql_fetch_array($get_num_res)) { echo "<tr>\n"; foreach ($numresult as $value) { echo "<td>$value</td>\n } echo "<\tr>\n"; } ?> </table> </body> </html>
-
<html> <head> <script type="text/javascript"> function selectAll(checkBool) { checkAry = document.cityFRM['city[]']; for (var i=0; i<checkAry.length; i++) { checkAry[i].checked = checkBool; } return true; } function selOne(checkBool) { selAllObj = document.getElementById('selAll'); if (!checkBool) { selAllObj.checked = false; } return true; } </script> </head> <body> <form name="cityFRM" id="cityFRM" METHOD=GET ACTION="city.php"> <input type="checkbox" onclick="selectAll(this.checked);" id="selAll">Check All<BR> <input type="checkbox" onclick="selOne(this.checked);" value="New York" name="city[]" id="city[]">New York <input type="checkbox" onclick="selOne(this.checked);" value="Chicago" name="city[]" id="city[]">Chicago <input type="checkbox" onclick="selOne(this.checked);" value="Los Angeles" name="city[]" id="city[]">Los Angeles <input type="checkbox" onclick="selOne(this.checked);" value="Detroit" name="city[]" id="city[]">Detroit </form> </body> </html>
-
Using square brakets in that manner creates an array obect with that name (minus the brackets) when the form is received by a PHP page, but javascript does not look at it that way. To javascript is creates an array with that name - including the brackets! function selAll( check ){ alert( document.cityFRM['city[]'].length ); }
-
<?php $get_num = "SELECT number1, number2, number3, number4, number5 FROM numbers"; $get_num_res = mysql_query($get_num,$conn) or die(mysql_error()); $int=0; while ($numresult = mysql_fetch_array($get_num_res)) { $int += array_sum($numresult); $display_block .= "<tr>\n<td>\n" . implode("</td>\n<td>\n", $numresult) . "</td>\n<\tr>\n"; } ?> <html> <body> <p><?php echo $int; ?></p> <table> <?php echo $display_block; ?> </table> </body> </html>
-
The simple solution is to instead of writing the table to the page as you create it you can build the table as a variable. Then when the process ends you can echo out the "totals" and then the content. Example: <?php while ($record = mysql_fetch_assoc($result)) { $tableContent .= "<tr>\n"; $tableContent .= "<td>{$record['name']}</td>\n"; $tableContent .= "<td>{$record['age']}</td>\n"; $tableContent .= "<td>{$record['salary']}</td>\n"; $tableContent .= "<tr>\n"; $count++; $totalAge += $record['age']; $totalSalary += $record['salary']; } //Show the totals echo "Number of staff: " . $count . "<br>\n"; echo "Average Age: " . ($totalAge/$count) . "<br>\n"; echo "Average Salary: " . ($totalSalary/$count) . "<br>\n"; //Show the content echo "<table>\n"; echo "<tr>\n"; echo "<th>Name</th>\n"; echo "<th>Age</th>\n"; echo "<th>Salary</th>\n"; echo "</tr>\n"; echo $tableContent; echo "</table>\n"; ?>
-
Here are my opinions: No I would NOT use htmlspecialchars on data before inserting it into the database. The reason why is that it is easier to keep data in it's original state until you know the context that you need to display it. For example, if you want to display the content as text on the page then you would use htmlspecialcharacters before you display it. But, if you need to insert the content into a textarea for editing purposes you would display it as is. If you had used htmlspecialcharacters then you would need to reverse that before displaying for editing purposes. The same goes for numbers. I would store numbers as integers or floats in the database and save the formatting of the number for just before I display it. htmlentities() is much more comprehensive than htmlspecialcharacters(), which only tanslates about a half dozen characters. I would probably stick with htmlentities unless a specific problem is found. But, it really depends on how and where you are using the content.
-
The problem is that you are using getElementById() for elements where you did not specify an ID. IE will "assume" that the ID is the same as the name, but FF does not. For example you have this in the JS var peincipal = document.getElementById("peincipal"); But the field looks like this: <input type="text" name="peincipal" size="15"> Change the field to this: <input type="text" name="peincipal" id="peincipal" size="15"> Need to make the same change for anything else you are trying to reference by ID.
-
Not enough information here. I think you need to go back and really examine the overall design. For example you state "Each time a player signs up they auto-matically need to be assigned an opponent". That's not possible, who would the first player be assigned to, or the third (assuming the first and second are assigned. Also, at what point does the tournament get locked? If eight players have signed up and one or more of the first round matches have been played, can other players join? I would assume that add'l players would not be able to join once the 2nd round matches have begun. What are you going to do when there are a number of players that do not equal a factor of 2 (2, 4, 8, 16, etc.). If you only have 6 teams youwould have to give one of the initial three winners a bye for the 2nd round. As you can see there are lots of questions that need to be answered. I would simply allow players to sign-up until some point in time when the tourney will be locked - either by reaching a max limit (such as 16) or where an admin locks it because a set amount of time has expired. You would also want something to cancel a tourney if there are not enough players. I don't have the time to write all the code that will "build" the brackets, but the following should help you: $players = 6; $rounds = ceil(log($players, 2)); //Result 3 $byes = pow(2, $rounds) - $players; //Result 2 (8 being a perfect bracket) The calculations above will determine the number of rounds needed and the number of byes (for the current bracket)
-
This would be a javascript problem not a PHP problem. Please post the generated HTML for a page that should be displaying the countdown.
-
Well, file() reads a file as an array and substr() expects a string object. Not sure what you are trying to accomplish, but you could use implode() to convert the file contents into a string or, probably better, use file_get_contents() to read the file as a string to begin with. <?php #Link files into variables $vatsim_file = file_get_contents($vatsim); $metacraft_file = file_get_contents($metacraft); $fsproshop_file = file_get_contents($fsproshop); #Get the last UPDATE variable $vatsim_time = substr($vatsim_file,2543,14); $metacraft_time = substr($metacraft_file,2543,14); $fsproshop_time = substr($fsproshop_file,2543,14); ?>
-
Yeah, I'm still a little confused as to what you are really trying to accomplish. There is no onfocus event for a div. So, you couldn't do a "return" capture to see what field had focus. I would suggest using a form with input fields (you can use CSS t make the input fields look like regular text if you want). Trying to capture a keypress is problematic when being multi-browser compatible. I try to avoid it unless I can't. How about something like this: <html> <head> <style> .noInput { border: 0px; } </style> <script type="text/javascript"> function setVal(fieldObj) { //Reset all fields to white document.getElementById('test_1').style.backgroundColor = '#ffffff'; document.getElementById('test_2').style.backgroundColor = '#ffffff'; document.getElementById('test_3').style.backgroundColor = '#ffffff'; //Set current field to yellow and set value fieldObj.style.backgroundColor = 'yellow'; document.getElementById('theValue').value = fieldObj.value; return true; } </script> </head> <body> <form name="test" onsubmit="return doThis(document.getElementById());"> <input type="text" id="test_1" onfocus="setVal(this);" value="Text1" readonly="readonly" class="noInput" /><br /> <input type="text" id="test_2" onfocus="setVal(this);" value="Text2" readonly="readonly" class="noInput" /><br /> <input type="text" id="test_3" onfocus="setVal(this);" value="Text3" readonly="readonly" class="noInput" /><br /> <br />This would be a hidden field:<br> <input type="text" id="theValue"> </form> </body> </html>
-
You need to be a little more specific. How are you wanting the function to be initialized (other than the onclick). As xtopolis stated, you can do it through onfocus of the element or through onload of the page, or there are many other triggers you could use.
-
Why do you think something is wrong? I can make an "assumption" of what I think it's supposed to do, but you don't state what the script IS doing as opposed to what you EXPECT it should be doing. My assumption is that if you click the radio button field, then the value of '1' should be populated into the hidden field. If that's what it is supposed to do, then it works fine for me (tested by just changing the hidden field to a text field). If there is something else it is supposed to do you need to provide more details.
-
Well, seeing as your query doesn't "appear" to have company_name in it, I would have to assume either 1) That is not the query creating the error or 2) The variable you are using has more information in it that it should. Also, you are not enclosing the value of $_SESSION['Company'] within single quotes in the query. Unless the value is a number, the query will also fail. You should create your queries as a variable so you can echo them to the page when an error occurs for debugging purposes. <?php $query = mysql_connect("*********", "******", "***********") or die(mysql_error()); mysql_select_db('******', $query) or die(mysql_error()); session_start(); if(isset($_SESSION['Company'])) { echo "logged in <br>" ; $query = "SELECT url FROM links WHERE company = '" . $_SESSION['Company'] . "' LIMIT 1"; $result = mysql_query($query) or die("Query:<br>$query<br>Error:<br>".mysql_error()); list($url) = mysql_fetch_row($result); header('Location: . $url .'); die('<a href=". $url .">Contiune[/url]'); } else { header('location: login.php'); } ?>
-
Just an FYI: AJAX is a combination of JavaScript AND a server-side language such as PHP. Plus, what you are wanting will require at least a general knowledge of CSS (aka Style Sheets).
-
Sounds like you would have to build somethign from scratch. I can't think of any way to customize a standard select list to do that. You are also making multiple requests. One would be about creating a multi-line select list and second would be a search capability. The fist would be done with a combination of CSS and DHTML, the second could be accomplished with just JavaScript or with AJAX. In any event, you are asking for someone to provide the whole ball of wax. If you want that, then you should post in the freelancing forum and perhaps someone would be willing to do it for compensation.
-
Well, as stated you cannot do it with cleint-side javascript. And, to do server-side javascript would require a webserver that supports it. That is a much more limited environment than one that supports PHP or ASP.
-
Well, you *can* run JavaScript server-side, but that's not very common from my experience. But, if you did do that, then you can connect to a database (not sure about MySQL): http://ajaxian.com/archives/server-side-javascript-databases-access But, it would take a lot more effort to do it and there are much better languages for working with a database (such as PHP). If you are looking for a way to add/retrieve information from the client-side, then you should be looking into AJAX.