pb1uk Posted February 19, 2010 Report Share Posted February 19, 2010 Hello all, I'm having a bit of head scratching problem that i just can't get my head around. I'm using Dreamweaver CS4 and phpmyadmin for my site. I have a form that inputs a record into a table in my database. The form is a user picking a fantasy sports team. A user selects a player from a drop down menu. The players name is got from a recordset that accesses the player database, with the values coming from the id field and the values from the name, so a user selects a player by there name and not there unique id value. The code for this is as follows: GoalKeeper: <?php do { ?> <?php echo $row_rs_pt_gk['gkname_gk']?> <?php } while ($row_rs_pt_gk = mysql_fetch_assoc($rs_pt_gk)); ?> The form is displayed as a table. In a row that i have added below i would like to display the players value, however this will not be input into the database, it's purely there so the the overall value of the team can be calculated, as the teams total value must not exceed a certain amount. I need the players value to be shown for the correct player. So for example if the user changes there choice from the menu of player name the players value will change to the player selected. I guess i need to use some kind of onclick javascript function, but i haven't used javascript before and i'm not even sure if it's the correct way to go. Any help or advice would really be appreciated as i just can't seem to get my head around it, Thanks, pb1uk Quote Link to comment Share on other sites More sharing options...
bsmbahamas Posted February 19, 2010 Report Share Posted February 19, 2010 you can populate a drop down menu with the player names from your database, and add a submit button next to the drop down menu, once they click the submit button it would reload the page and pass the player name in a variable to your script so it would know what info to load. you'd need the javascript onchange event, or you can add it directly to the drop down box by adding onlick="javascript or function goes here" try these... http://lab.artlung.com/dropdown/ http://www.javascriptkit.com/jsref/select.shtml http://www.faqs.org/docs/htmltut/forms/_SELECT_onChange.html if you don't want the page to reload, then you'd need to use 'ajax' which is a combo of html, css, javascript and using the DOM, i haven't quite wrapped my head around ajax and mysql yet though, but i have about 10 years experience with html,javascript, and php. Quote Link to comment Share on other sites More sharing options...
pb1uk Posted February 19, 2010 Author Report Share Posted February 19, 2010 Thanks for the response. The dropdown menu is already populated with values from the player database. The label is given from the name and the value that is submitted to the database table is the players id. I do need it to be so the page doesn't have to be reloaded, so think ajax is the way to go. Imagine a form with a drop down menu: (col 1) (col 2) (col 3) Select player: |dropdown menu| ??? what i need to happen is when a player is selected from the menu their value appears in the column of the table where the question marks are. I notice of those links and i've seen a few places where you state option1, option 2 etc which would be ok, however i have hundreds of players in the database so this would take ages and i'm sure there's a much more efficient way to do it. Quote Link to comment Share on other sites More sharing options...
krillz Posted February 19, 2010 Report Share Posted February 19, 2010 Thanks for the response.The dropdown menu is already populated with values from the player database. The label is given from the name and the value that is submitted to the database table is the players id. I do need it to be so the page doesn't have to be reloaded, so think ajax is the way to go. Imagine a form with a drop down menu: (col 1) (col 2) (col 3) Select player: |dropdown menu| ??? what i need to happen is when a player is selected from the menu their value appears in the column of the table where the question marks are. I notice of those links and i've seen a few places where you state option1, option 2 etc which would be ok, however i have hundreds of players in the database so this would take ages and i'm sure there's a much more efficient way to do it. if you got the data in the database, you can simply just add another dropdown filled with that info. without seeing how your database is built up it's hard to tell you exactly what to do. Quote Link to comment Share on other sites More sharing options...
pb1uk Posted February 19, 2010 Author Report Share Posted February 19, 2010 if you got the data in the database, you can simply just add another dropdown filled with that info. without seeing how your database is built up it's hard to tell you exactly what to do. In the the table of players i have: playerid - unique id stored as an integer playername - players name playervalue - players value playerclubid - unique id of players club The form is being enterred into the team table of the database. The labels for the drop down menu are got from playername and the id actually enterred into the team database for each player is got from playerid. What i need to do is dynamically populate a cell in a table based on the selection made from the drop down menu. This value will not be enterred into teams database it is merely being used to calculate the total value of the team. Is that the kind of thing you need to know? Quote Link to comment Share on other sites More sharing options...
krillz Posted February 20, 2010 Report Share Posted February 20, 2010 (edited) you can either treat it as a form that will be sent whenever a change is done to the dropdown menu then display the specific team's point using php or another language in that particular table cell. check the onSelect() javascript function as a eventlistener to whenever a change is made in the dropdown box, you could call a function let say submit() that sends the form each time a change is done ( in other words a new selection is made) using a code like: document.myform.submit(); inside the submit() javascript option. The downside is that the site would have to reload, if you do not fancy that you should check some "ajax" methods instead. > </pre> <form> Select a Customer: NameOne NameTwo NameThree </form> <br><br><div id="points">Points will be shown here</div> <br selectuser.js var xmlhttp; function showUser(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="getuser.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("points").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; } Now in the javascript file we call getuser.php and beneath is the code for that: $q=$_GET["q"]; $conn = mysql_connect('localhost', 'username', 'pass'); // your mysql info if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("youDatabase", $conn); // change to your database $sql="SELECT * FROM user WHERE playerclubid = '".$q."'"; // the var q might be different in your case, so in the original html code you need to send the attribute needed. // Then query the database, perform the addition in php or in mysql using an appropriate querry. Do the formatting etc. echo "" // Echo the thing you want, in your case the team score. ?> This is just a very rough layout, just to give you an idea on how you can go about doing it. Just be adviced that it also means the code right now is open for exploits and you should add measures to prevent people from manually sending in harmful queries to the javascript function. Look out for sql injections, as I presume the clubid is an integer you could just check that the attibute is in fact an integer and nothing else, and this should be done in the php code before the query and not in the javascript. in the html is a instead, but I assume it should work with your current layout by setting the particular cell elemt's ID and using that. in this example we used the id: points. Edited February 20, 2010 by krillz Quote Link to comment Share on other sites More sharing options...
pb1uk Posted March 4, 2010 Author Report Share Posted March 4, 2010 Hi, Sorry for my late response, but the above worked a treat thank you Krillz Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.