wright67uk Posted December 13, 2012 Share Posted December 13, 2012 The code below displays a graph of golf club ranges. This particular fileis for an admin user who can look at any users graphs using the form at the bottom of the page. Rather than having a submit button can i change the button method to an onchange event? Can I select users and years without page refreshes? <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Golf Trust - Ranges</title> <?php ############ $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR); @mysql_select_db($database_connect) or die (mysql_error()); $tbl_name = "snag"; $user_id = $_POST["user_id"]; $year = (isset($_GET['year'])) ? (int)$_GET['year'] : date('Y'); $year_links = ''; $_GET['year'] = $year-1; $year_links .= "<a href='?" . http_build_query($_GET, '', '&') . "'>-</a> "; $year_links .= " $year "; $_GET['year'] = $year+1; $year_links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>+</a>"; $sql = "SELECT user_id, fname, lname FROM registration"; $result = mysql_query($sql); $selected_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : false; $who = ''; $options = ''; while ($row=mysql_fetch_array($result)) { if($selected_id==$row['user_id']) { $who = "{$row['fname']} {$row['lname']}"; $selected = ' selected="selected"'; } else { $selected = ''; } $options .= "<option value=\"{$row['user_id']}\"{$selected}>{$row['fname']} {$row['lname']}</option>\n"; } ?> <style type="text/css"> a:link {text-decoration:none;} </style> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript"> $(function () { var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'line', marginRight: 130, marginBottom: 25 }, title: { text: '<?php echo $year ?> Ranges by Club for <?php echo $who ?>', x: -20 //center }, subtitle: { text: 'Averages for month', x: -20 }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, yAxis: { title: { text: 'Score' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +''; } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'top', x: -10, y: 100, borderWidth: 0 }, series: [{ name: 'Driver', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'Driver' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '3 Wood', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '3 wood' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '5 Wood', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '5 wood' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] /* SELECT yard FROM `snag` WHERE club = '5 wood' AND user_id = 1 */ }, { name: 'Hybrid', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'Hybrid' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '3 iron', data:[<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '3 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '4 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '4 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '5 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '5 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '6 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '6 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '7 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '7 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '8 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '8 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: '9 iron', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = '9 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: 'P Wedge', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'P wedge' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }, { name: 'S Wedge', data: [<?php $sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month = MONTH( STR_TO_DATE( sdate, '%d/%m/%y' ) ) AND club = 'S wedge' AND user_id = $user_id AND YEAR = $year GROUP BY `month`"; $result=mysql_query($sql); while($row = mysql_fetch_array($result) ){ echo $row['avscore'].',';} ?>] }] }); }); }); </script> </head> <body> <script src="../../js/highcharts.js"></script> <script src="../../js/modules/exporting.js"></script> <form method="post" action="<?php echo $PHP_SELF;?>"> <select name="user_id"> <option>Select User</option> <?php echo $options ?> </select> <input type="submit" value="Submit"> </form> <?php echo $year_links;?> <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/271976-onchange-and-jsphp-page-refreshes/ Share on other sites More sharing options...
Christian F. Posted December 14, 2012 Share Posted December 14, 2012 I would recommend keeping the button, so that people without JS would still be able to use your site. In addition to that, you can add a JS to hook into the change event, as well as hiding the button (not really recommended). You'll need to use AJAX to communicate with the server, if you don't want a page-load, or have the JS manually trigger the submit event. Either way: Perfectly possible, and quite easy to do with something like jQuery. Quote Link to comment https://forums.phpfreaks.com/topic/271976-onchange-and-jsphp-page-refreshes/#findComment-1399327 Share on other sites More sharing options...
wright67uk Posted December 14, 2012 Author Share Posted December 14, 2012 Thank you Christian, I did try some basic tutorials with AJAX and found it great. However when I came to try it with the file above, I was a bit lost! Maybe some further reading to do... Thanks for your help though! Quote Link to comment https://forums.phpfreaks.com/topic/271976-onchange-and-jsphp-page-refreshes/#findComment-1399334 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.