Jump to content

Selecting Form Option As Variable Value


wright67uk

Recommended Posts

I'm sure i'm doing things the wrong way here, but I don't seem to be able to get my head around it.

 

How can i ensure that $who has the value of the selected option from my form after it has been submitted?

 

at the moment it just has the value of the last $fname and $lname in my db. and doesnt change.

 

 

<?php
$sql="SELECT user_id, fname, lname FROM registration";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$user_id=$row["user_id"];
$fname=$row["fname"];
$lname=$row["lname"];

$options.="<OPTION VALUE=\"$user_id\">".$fname." ".$lname;
}
$who = $fname." ".$lname;
?>

 

and then in my code...

 

 

text: '<?php echo $year ?> Ranges by Club for <?php echo $who ?>',

 

 

and then at the bottom of my page...

 

 

<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>

 

 

everything together...

 

 


<?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);

$options="";

while ($row=mysql_fetch_array($result)) {

$user_id=$row["user_id"];
$fname=$row["fname"];
$lname=$row["lname"];

$options.="<OPTION VALUE=\"$user_id\">".$fname." ".$lname;
}

$who = $fname." ".$lname;

?>



<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>

Link to comment
https://forums.phpfreaks.com/topic/271804-selecting-form-option-as-variable-value/
Share on other sites

$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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.