Assign the value selected from drop down list to a variable

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 17266 Views - Last Post: 05 February 2011 - 03:40 PM Rate Topic: -----

#1 clarkeash  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 04-February 11

Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 01:06 PM

I have done plenty of searching and I cannot find any solutions to my problem.
The issue is..

I have a mysql table with "team names" and "team ids" in (team_name & id_team)
I then have a dropdown list which is populate with the team names from an sql query.
<?php
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
					
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$query="SELECT id_team, team_name FROM tbl_team";
					
					
$result = mysql_query ($query);
echo "<select name='teams'>";
					
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id_team]>$nt[team_name]</option>";
}
echo "</select>";
?>



Then when a team from the list is selected (possibly submitted by use of button) the id of that team is assigned to a variable to that i can use that for other queries on the same page.

Help much appreciated.

sql dump & php files are available upon request.

Thanks Clarkeash

Is This A Good Question/Topic? 0
  • +

Replies To: Assign the value selected from drop down list to a variable

#2 init.d.httpd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 132
  • Joined: 02-February 11

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 01:24 PM

I would use javascript to store the selected value (team_id) for use in the other queries on that page.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 01:25 PM

given you submit the form (where the dropdown resides in) you get the selected ID from the $_GET or $_POST array (depending whether you used the get or post method in the form). in you case this would be either $_GET['teams'] or $_POST['teams'].

@init.d.httpd: Javascript can do nothing on the server side, where the database is located.

This post has been edited by Dormilich: 04 February 2011 - 01:26 PM

Was This Post Helpful? 1
  • +
  • -

#4 init.d.httpd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 132
  • Joined: 02-February 11

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 01:29 PM

View PostDormilich, on 04 February 2011 - 01:25 PM, said:

given you submit the form (where the dropdown resides in) you get the selected ID from the $_GET or $_POST array (depending whether you used the get or post method in the form). in you case this would be either $_GET['teams'] or $_POST['teams'].

@init.d.httpd: Javascript can do nothing on the server side, where the database is located.



I'm aware of that. I'm under the impression that he is wanting to run more queries based on the id selected from that drop down. He could use javascript to populate a hidden input to send the team_id along with whatever other queries he needs to run.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 01:32 PM

if he only requires the ID, the simple approach would suffice. if there are more tasks to do, adding form elements (like a hidden field) may come in handy.
Was This Post Helpful? 1
  • +
  • -

#6 init.d.httpd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 132
  • Joined: 02-February 11

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 01:35 PM

View PostDormilich, on 04 February 2011 - 01:32 PM, said:

if he only requires the ID, the simple approach would suffice. if there are more tasks to do, adding form elements (like a hidden field) may come in handy.



I agree sir.
Was This Post Helpful? 0
  • +
  • -

#7 clarkeash  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 04-February 11

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 03:54 PM

I am no good with java so php is my only option really (and im not exactly great with php) , have tried a few things based on the comments but with no luck,

I have some code but it is not working and i cannot understand why, any help would be appreciated.

test4.php
<?php 
include('config.inc');
?>
<html>
<head>
<title>Test page</title>
</head>
<body>
<?php
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}
$query="SELECT id_team, team_name FROM tbl_team";
$result = mysql_query ($query);
echo"<form action='test4-x.php' method='post'><select name='team_selected'>"
while($nt=mysql_fetch_array($result)){
echo'<option value="' . $nt[id_team] . '">' . $nt[team_name] . '</option>'
}
echo "</select>";
?>
<input type='submit' />
</form>

</body>
</html>




test4-x.php

<?php 
include('config.inc');
?>
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1> The chosen team is : 
<?php
echo $_POST['team_selected'];
?>
</h1>
</body>
</html>



Thanks in advance guys
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 03:59 PM

View Postclarkeash, on 04 February 2011 - 11:54 PM, said:

I have some code but it is not working and i cannot understand why, any help would be appreciated.

"is not working" is something I canít make sense of. a more detailed description would be helpful.
Was This Post Helpful? 0
  • +
  • -

#9 clarkeash  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 04-February 11

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 04:08 PM

View PostDormilich, on 04 February 2011 - 03:59 PM, said:

View Postclarkeash, on 04 February 2011 - 11:54 PM, said:

I have some code but it is not working and i cannot understand why, any help would be appreciated.

"is not working" is something I canít make sense of. a more detailed description would be helpful.

Sorry, I just get a blank page.

View Postclarkeash, on 04 February 2011 - 04:07 PM, said:

View PostDormilich, on 04 February 2011 - 03:59 PM, said:

View Postclarkeash, on 04 February 2011 - 11:54 PM, said:

I have some code but it is not working and i cannot understand why, any help would be appreciated.

"is not working" is something I canít make sense of. a more detailed description would be helpful.

Sorry, I just get a blank page.

Blank page on the test4.php page
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 04:13 PM

what does the HTML source code show (is there anything at all)? what are your error display settings?
Was This Post Helpful? 0
  • +
  • -

#11 clarkeash  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 04-February 11

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 04:57 PM

View PostDormilich, on 04 February 2011 - 04:13 PM, said:

what does the HTML source code show (is there anything at all)? what are your error display settings?

Source code is empty, (i have no error display settings?)
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Assign the value selected from drop down list to a variable

Posted 04 February 2011 - 05:00 PM

this looks like there is an error any you prevent showing it up.

add error_reporting(E_ALL); first thing in your script.
Was This Post Helpful? 0
  • +
  • -

#13 clarkeash  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 04-February 11

Re: Assign the value selected from drop down list to a variable

Posted 05 February 2011 - 10:09 AM

View PostDormilich, on 04 February 2011 - 05:00 PM, said:

this looks like there is an error any you prevent showing it up.

add error_reporting(E_ALL); first thing in your script.

No erros are stated :dots:
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: Assign the value selected from drop down list to a variable

Posted 05 February 2011 - 10:15 AM

Where are the ;s after your echos in test4.php?
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: Assign the value selected from drop down list to a variable

Posted 05 February 2011 - 10:19 AM

If you check your error logs you'll probably find a fatal error. Tell us what that is.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2