Jump to content

max_power

Members
  • Posts

    43
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

max_power's Achievements

Member

Member (2/5)

0

Reputation

  1. Hi, I have about 40 select boxes and I've allocated each of them an individual id because the amount of select boxes will never change. I've put each value in a JS array and then receive it in PHP through AJAX. The problem I am having is to do with inserting/updating data in the db. The problem is currently I have the following logic set when the user presses the save button: [center] $sql = "SELECT * FROM tbl_supervision WHERE workload_id = '".$_SESSION['active_workload']."' AND semester = '1';"; $result = mysql_query($sql) or die ("Error in query: $sql. " . mysql_error()); $rows = mysql_num_rows($result); $supervision_data = array(); while($row = mysql_fetch_array($result)) { $supervision_data[] = $row; } if(!empty($supervision_data)) { for($i = 0; $i < count($supervision_data); $i++) { $sql4 = "UPDATE tbl_supervision SET workload_id = '".$_SESSION['active_workload']."', degree = '".$degree1array[$i]."', student_name = '".$student1array[$i]."', student_eft = '".$eft1array[$i]."', supervision_role = '".$role1array[$i]."' WHERE supervision_id = '".$supervision_data[$i]['supervision_id']."' AND semester = '1';"; $result4 = mysql_query($sql4) or die ("Error in query: $sql4. " . mysql_error()); if (!$result4) { die('Error: ' . mysql_error()); } else { $success = 1; } } } else { for($i = 0; $i < count($degree1array); $i++) { if($degree1array[$i] != "None" || $student1array[$i] != "" || !empty($eft1array[$i]) || $role1array[$i] != "") { if($degree1array[$i] != "None") { $sql7 = "INSERT into tbl_supervision (workload_id, degree, student_name, student_eft, supervision_role, semester) VALUES ('".$_SESSION['active_workload']."', '".$degree1array[$i]."', '".$student1array[$i]."', '".$eft1array[$i]."', '".$role1array[$i]."', '1');"; $result7= mysql_query($sql7) or die ("Error in query: $sql7. " . mysql_error()); if (!$result7) { die('Error: ' . mysql_error()); } else { $success = 1; } } } else { $success = 0; } } }[/center] What I want to achieve is: 1. If there are the same number of inputs in the db compared to the number of inputs received from the select boxes, the update query will simply kick in. 2. If there there are less number of inputs in the db compared to the number of inputs received from the select boxes, the insert query will only insert those values that aren't already in the db. Currently, with the code I have posted above, I can only insert values from scratch and only update those values that are already there in the db but I can't add in new values after the initial data has been inserted. I've tried using count() and sizeof() functions but I can't seem to work out how could I manipulate the code to achieve what should be straight forward functionality. Could anybody please help me out? The degree1array, student1array, eft1array & role1array are the arrays that contain the select box values.. var degree1 = new Array (document.getElementById('degreetype1').value, document.getElementById('degreetype2').value, document.getElementById('degreetype3').value, document.getElementById('degreetype4').value, document.getElementById('degreetype5').value, document.getElementById('degreetype6').value, document.getElementById('degreetype7').value, document.getElementById('degreetype8').value, document.getElementById('degreetype9').value, document.getElementById('degreetype10').value); var student1 = new Array (escape(document.getElementById('studentname1').value), escape(document.getElementById('studentname2').value), escape(document.getElementById('studentname3').value), escape(document.getElementById('studentname4').value), escape(document.getElementById('studentname5').value), escape(document.getElementById('studentname6').value), escape(document.getElementById('studentname7').value), escape(document.getElementById('studentname8').value), escape(document.getElementById('studentname9').value), escape(document.getElementById('studentname10').value)); var eft1 = new Array (document.getElementById('eft1').value, document.getElementById('eft2').value, document.getElementById('eft3').value, document.getElementById('eft4').value, document.getElementById('eft5').value, document.getElementById('eft6').value, document.getElementById('eft7').value, document.getElementById('eft8').value, document.getElementById('eft9').value, document.getElementById('eft10').value); var role1 = new Array (document.getElementById('suprole1').value, document.getElementById('suprole2').value, document.getElementById('suprole3').value, document.getElementById('suprole4').value, document.getElementById('suprole5').value, document.getElementById('suprole6').value, document.getElementById('suprole7').value, document.getElementById('suprole8').value, document.getElementById('suprole9').value, document.getElementById('suprole10').value); Thanks
  2. Because the formulas will be changed later on by someone else who won't be coding savvy..
  3. Hi all, I've stored a formula in a mysql db table that is pretty basic. To give an example: IF(x=1) { y = 2; } IF(x=2) { y = 3; } IF(x>2) { y = 0; } x represents the value from the input field from while y represents the output value (that will be shown through a span element innerHTML). My question is, how can I firstly retrieve this formula from the table and then secondly adapt the x as the value from the input field and then show the result as the output? Thanks
  4. Hi, I would just like to know how I would dynamically add another table once text fields in the existing table is clicked on. So pretty much what I have is a table with 5 textboxes lined up horizontally in the first row along with couple of buttons in the second row. What I want is that once one text box is clicked, another table like the one above is created and appears below that initial table. So this is the inital table that should be replicated on each click: <table> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" ></td> </tr> <tr> <td><button /></td> <td><button /></td> </tr> </table> How would I go about achieving this? Thanks
  5. Nope but could you please provide the link?
  6. Hi, I have created an autosuggest via javascript from code I found on the internet. What I have done is that firstly I have put all the mysql result data in a php array whose contents are passed onto the javascript array. Now it all works but the problem is, that I have to refresh the page each to see any changes I make to the mysql db. How can I used ajax with my code? Everything is in the one file with the php + javascript (I have bolded the relevant bits): <html> <?php [b]// select database mysql_select_db($dbName) or die ("Unable to select database!"); $rs=mysql_query("select username from tbl_user"); $records=mysql_num_rows($rs); $i=1; while(list($name)=mysql_fetch_row($rs)) { echo $name.'<br />'; if($i<$records) { $array_names.="'$name',"; } //if it's the last item from the series, don't put a comma after the value else { $array_names.="'$name'"; } $i++; }[/b] ?> <head> <title>Autosuggest</title> <link rel="stylesheet" type="text/css" href="autosuggest.css" /> <script type="text/javascript"> function AutoSuggestControl(oTextbox /*:HTMLInputElement*/, oProvider /*:SuggestionProvider*/) { /** * The currently selected suggestions. * @scope private */ this.cur /*:int*/ = -1; /** * The dropdown list layer. * @scope private */ this.layer = null; /** * Suggestion provider for the autosuggest feature. * @scope private. */ this.provider /*:SuggestionProvider*/ = oProvider; /** * The textbox to capture. * @scope private */ this.textbox /*:HTMLInputElement*/ = oTextbox; //initialize the control5/24/2010 this.init(); } AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/, bTypeAhead /*:boolean*/) { //make sure there's at least one suggestion if (aSuggestions.length > 0) { if (bTypeAhead) { this.typeAhead(aSuggestions[0]); } this.showSuggestions(aSuggestions); } else { this.hideSuggestions(); } }; AutoSuggestControl.prototype.createDropDown = function () { var oThis = this; //create the layer and assign styles this.layer = document.createElement("div"); this.layer.className = "suggestions"; this.layer.style.visibility = "hidden"; this.layer.style.width = this.textbox.offsetWidth; //when the user clicks on the a suggestion, get the text (innerHTML) //and place it into a textbox this.layer.onmousedown = this.layer.onmouseup = this.layer.onmouseover = function (oEvent) { oEvent = oEvent || window.event; oTarget = oEvent.target || oEvent.srcElement; if (oEvent.type == "mousedown") { oThis.textbox.value = oTarget.firstChild.nodeValue; oThis.hideSuggestions(); } else if (oEvent.type == "mouseover") { oThis.highlightSuggestion(oTarget); } else { oThis.textbox.focus(); } }; document.body.appendChild(this.layer); }; AutoSuggestControl.prototype.getLeft = function () /*:int*/ { var oNode = this.textbox; var iLeft = 0; while(oNode.tagName != "BODY") { iLeft += oNode.offsetLeft; oNode = oNode.offsetParent; } return iLeft; }; AutoSuggestControl.prototype.getTop = function () /*:int*/ { var oNode = this.textbox; var iTop = 0; while(oNode.tagName != "BODY") { iTop += oNode.offsetTop; oNode = oNode.offsetParent; } return iTop; }; AutoSuggestControl.prototype.handleKeyDown = function (oEvent /*:Event*/) { switch(oEvent.keyCode) { case 38: //up arrow this.previousSuggestion(); break; case 40: //down arrow this.nextSuggestion(); break; case 13: //enter this.hideSuggestions(); break; } }; AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) { var iKeyCode = oEvent.keyCode; //for backspace ( and delete (46), shows suggestions without typeahead if (iKeyCode == 8 || iKeyCode == 46) { this.provider.requestSuggestions(this, false); //make sure not to interfere with non-character keys } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) { //ignore } else { //request suggestions from the suggestion provider with typeahead this.provider.requestSuggestions(this, true); } }; AutoSuggestControl.prototype.hideSuggestions = function () { this.layer.style.visibility = "hidden"; }; AutoSuggestControl.prototype.highlightSuggestion = function (oSuggestionNode) { for (var i=0; i < this.layer.childNodes.length; i++) { var oNode = this.layer.childNodes[i]; if (oNode == oSuggestionNode) { oNode.className = "current" } else if (oNode.className == "current") { oNode.className = ""; } } }; AutoSuggestControl.prototype.init = function () { //save a reference to this object var oThis = this; //assign the onkeyup event handler this.textbox.onkeyup = function (oEvent) { //check for the proper location of the event object if (!oEvent) { oEvent = window.event; } //call the handleKeyUp() method with the event object oThis.handleKeyUp(oEvent); }; //assign onkeydown event handler this.textbox.onkeydown = function (oEvent) { //check for the proper location of the event object if (!oEvent) { oEvent = window.event; } //call the handleKeyDown() method with the event object oThis.handleKeyDown(oEvent); }; //assign onblur event handler (hides suggestions) this.textbox.onblur = function () { oThis.hideSuggestions(); }; //create the suggestions dropdown this.createDropDown(); }; AutoSuggestControl.prototype.nextSuggestion = function () { var cSuggestionNodes = this.layer.childNodes; if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length-1) { var oNode = cSuggestionNodes[++this.cur]; this.highlightSuggestion(oNode); this.textbox.value = oNode.firstChild.nodeValue; } }; AutoSuggestControl.prototype.previousSuggestion = function () { var cSuggestionNodes = this.layer.childNodes; if (cSuggestionNodes.length > 0 && this.cur > 0) { var oNode = cSuggestionNodes[--this.cur]; this.highlightSuggestion(oNode); this.textbox.value = oNode.firstChild.nodeValue; } }; AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) { //use text ranges for Internet Explorer if (this.textbox.createTextRange) { var oRange = this.textbox.createTextRange(); oRange.moveStart("character", iStart); oRange.moveEnd("character", iLength - this.textbox.value.length); oRange.select(); //use setSelectionRange() for Mozilla } else if (this.textbox.setSelectionRange) { this.textbox.setSelectionRange(iStart, iLength); } //set focus back to the textbox this.textbox.focus(); }; AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/) { var oDiv = null; this.layer.innerHTML = ""; //clear contents of the layer for (var i=0; i < aSuggestions.length; i++) { oDiv = document.createElement("div"); oDiv.appendChild(document.createTextNode(aSuggestions[i])); this.layer.appendChild(oDiv); } this.layer.style.left = this.getLeft() + "px"; this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px"; this.layer.style.visibility = "visible"; }; AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) { //check for support of typeahead functionality if (this.textbox.createTextRange || this.textbox.setSelectionRange){ var iLen = this.textbox.value.length; this.textbox.value = sSuggestion; this.selectRange(iLen, sSuggestion.length); } }; [b]function StateSuggestions() { this.customarray = new Array(<?php echo $array_names; ?>); } StateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/, bTypeAhead /*:boolean*/) { var aSuggestions = []; var sTextboxValue = oAutoSuggestControl.textbox.value; if (sTextboxValue.length > 0){ //search for matching states for (var i=0; i < this.customarray.length; i++) { if (this.customarray[i].indexOf(sTextboxValue) == 0) { aSuggestions.push(this.customarray[i]); } } } //provide suggestions to the control oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead); }; [/b] [b]window.onload = function () { var oTextbox = new AutoSuggestControl(document.getElementById("txt1"), new StateSuggestions()); }[/b] </script> </head> <body> <p><input type="text" id="txt1" /></p> </body> </html> Also, since I am echoing the php array values into the javascript array, on the page it unnecessarily echos those values, how to I find a way to stop that? Thanks.
  7. Thanks for the reply mate. I have been looking into ajax and have worked out something but the problem is the search box autosuggest list isn't populating from the javascript array. Here are the codes: Ajax.js file /* ---------------------------- */ /* XMLHTTPRequest Enable */ /* ---------------------------- */ function createObject() { var request_type; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ request_type = new ActiveXObject("Microsoft.XMLHTTP"); }else{ request_type = new XMLHttpRequest(); } return request_type; } var http = createObject(); /* -------------------------- */ /* SEARCH */ /* -------------------------- */ function autosuggest() { q = document.getElementById('search-q').value; // Set te random number to add to URL request nocache = Math.random(); http.open('get', 'autosuggest.php?q='+q+'&nocache = '+nocache); http.onreadystatechange = autosuggestReply; http.send(null); } function autosuggestReply() { if(http.readyState == 4){ var response = http.responseText; e = document.getElementById('results'); if(response!=""){ e.innerHTML=response; e.style.display="block"; } else { e.style.display="none"; } } } search.php file: <?php include('config.php'); $SQL_FROM = 'tbl_user'; $SQL_WHERE = 'username'; ?> <?php $searchq = strip_tags($_GET['q']); $getRecord_sql = 'SELECT username FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%"'; $getRecord = mysql_query($getRecord_sql); if(strlen($searchq)>0){ $records=mysql_num_rows($getRecord); $i=1; echo '<ul>'; while ($row = mysql_fetch_array($getRecord)) { if($i<$records) { $array_names.="'$row',"; } //if it's the last item from the series, don't put a comma after the value else { $array_names.="'$row'"; } $i++; ?> <li><a href="#"><?php echo " <script> var customarray = new Array(".$array_names."); for (i=0;i<customarray.length;i++) { document.write(customarray[i] + <br >); } </script>"; ?></a></li> <?php } echo '</ul>'; ?> <?php } ?> autosuggest.php file: <!-- AJAX AUTOSUGGEST SCRIPT --> <script type="text/javascript" src="lib/ajax.js"></script> <style type="text/css"> /* ---------------------------- */ /* CUSTOMIZE AUTOSUGGEST STYLE */ #search-wrap input{width:400px; font-size:16px; color:#999999; padding:6px; border:solid 1px #999999;} #results{width:260px; border:solid 1px #DEDEDE; display:none;} #results ul, #results li{padding:0; margin:0; border:0; list-style:none;} #results li {border-top:solid 1px #DEDEDE;} #results li a{display:block; padding:4px; text-decoration:none; color:#000000; font-weight:bold;} #results li a small{display:block; text-decoration:none; color:#999999; font-weight:normal;} #results li a:hover{background:#FFFFCC;} #results ul {padding:6px;} </style> <div id="search-wrap"> <h1>Search with Auto Suggest</h1> <input name="search-q" id="search-q" type="text" onkeyup="javascript:autosuggest()"/> <div id="results"></div> </div> What's going wrong, why isn't the javascript array spitting out the values?
  8. Right guys, I have been working on it and this is where I have gotten so far: <html> <?php // select database mysql_select_db($dbName) or die ("Unable to select database!"); $rs=mysql_query("select customer_name from customers"); $records=mysql_num_rows($rs); $i=1; while(list($name)=mysql_fetch_row($rs)) { if($i<$records) $array_names.="'$name',"; //if it's the last item from the series, don't put a comma after the value else $array_names.="'$name'"; $i++; } ?> <head> <script> var customarray = new Array(<?php echo $array_names; ?>); </script> </head> <body> <input type='text' style='font-family:verdana;width:300px;font-size:12px' id='names' value=''/> </body> </html> Now that I have managed to echo the names into a javascript array, how do I loop through the array using the textbox as well as build the autosuggest list? Also, just one more thing, if I add in another customer and I don't referesh the search page, will it contain the new customer in the js array? If not, how do I do that? Thanks
  9. Hi all, First sorry if this is in the wrong section. I am trying to create a search function that first performs the mysql query and then uses php to pass the values to a javascript array. So instead of numerous connections being made to the database, all the data gets stored in the javascript array at once and from there I can loop through the array to display values. The second part of this is, using a search text box to loop through the array to find the relevant values but at the same when I am typing a letter or something, a div should provide a list of existing things, so basically an autosuggest list. After that, when the value is clicked on, it will be displayed as an alert. I would greatly appreciate help in this as I really don't know that much of working with both javascript/ajax and PHP. $query = "SELECT * FROM Customers";
  10. Thanks for the reply. Would the coding look something like this: session_start(); $_SESSION['FailedLogin']; mysql query... If($result != 1) { echo "login failed"; $_SESSION['FailedLogin'] = $_SESSION['FailedLogin'] + 1; if($_SESSION['FailedLogin'] == 3;) { //lock out user. } else { //redirect user back to login form code. } } That is where I am getting confused. I can't map out the logic in code. I will look into protecting the user passwords once I get this sorted.
  11. Hi all, I want to create a login function for my .hta file that allows the user to only make three invalid login attempts before he/she gets locked out. So the process is like the following: If the login details do not exist, a session called $_SESSION[‘FailedLogin’] is created and is given the value of 1, while the ‘Login failed – try again’ message appears to the user. Basically, before showing the login form I want to check if the value of $_SESSION[‘FailedLogin’] is equal to 3. If it is not and the user fails their login again, the value is updated by 1 again (so +1). If the value of $_SESSION[‘FailedLogin’] reads as 3 then the user will get a message ‘three login attempts failed – login now blocked’. How would I create such a function? The fields in the userAuthentication table are UserID, Username and UserPassword. Thanks, Max
  12. Hi all, Basically I have created a sponsor database and I am trying to add a rule that only two Core Sponsor relationship types can exist at any one time. So this rule is for adding and editing. During the adding stage, I have no problem but during the editing phase I have one problem. Now the existing non-Core Sponsor I can edit without any problem and the rule is enforced with no problem as well but if I want to edit an existing Core Sponsor, even if I don't edit the relationship type, it will show me an error saying that 2 core sponsor already exist. It applies to both existing core sponsors. Not sure why this is doing that since both of them are the EXISTING core sponsors and no more core sponsors are being added! Here is the edit part of my code concerning this rule: if ($relation == "Core Sponsor") { $query1 = "SELECT relationship FROM tbl_sponsor where relationship = '$relation'"; $result2 = mysql_query($query1); $rows = mysql_num_rows($result2); if($rows == 2) { echo "Two core sponsors already exist"; } else { //update query. } } The 'if($rows == 2)' is probably causing this but is there any other way to carry out this rule during the editing stage for the two existing Core Sponsors and the rest of the non-Core Sponsors? It seems it is one or the other for me. Thanks, Max Power
×
×
  • 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.