Jump to content

Please Help with a passing variables in a drop down


srhino

Recommended Posts

Hey guys and gals,

 

I want to pass two variables over in a drop down menu. How can I doo this.

I have attached my code below. I want to send both the school name and the school id over.

 

Thanks

 

<?php

include("includes/connection.php");

?>

<?php



echo"<form action=\"newscore.php\" method=\"post\">";

echo"ENTER NEW MATCH";

echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n";

echo" <tr><td> School 1:</td><td><select name=\"school1\">";

$query="SELECT * from schools";

   $result=mysql_query($query);

   while($row=mysql_fetch_array($result,MYSQL_ASSOC))

   {

$schoolid = $row['schoolid'];
$schoolname = $row['schoolname'];
$schoolabb = $row['schoolabb'];


echo "<option value=\"$schoolname\">$schoolname</option>\n";
   }
echo "</select></td>";


echo" <tr><td> School 2:</td><td><select name=\"school2\">";

$query="SELECT * from schools";

   $result=mysql_query($query);

   while($row=mysql_fetch_array($result,MYSQL_ASSOC))

   {

$schoolid = $row['schoolid'];
$schoolname = $row['schoolname'];
$schoolabb = $row['schoolabb'];


echo "<option value=\"$schoolname\">$schoolname</option>\n";
   }
echo "</select></td></tr>




</table>";

echo "<input type=\"submit\" value=\"Submit\">\n";

   echo "<input type=\"hidden\" name=\"content\" value=\"newscore\">\n";

   echo "</form>\n";

   
?>

 

 

Link to comment
Share on other sites

I don't think you understand what you code is doing. Here is a basic idea of how your code should flow:

 

1. SELECT data from database using a query, ie: "SELECT * FROM schools"

1a: Here I will explain that this line: $result=mysql_query($query); is most likely returning a _set_ of school records from the table. This means you will be getting data on a number of different schools, not just one.

1b: If you want to fetch information on specific schools, you need to alter your $query string. For example: "SELECT * FROM schools WHERE schoolname='West Hill High'" will return all data fields (*) from table schools, where the school name is "West Hill High". It is always better to fetch only the data you are interested in by altering the SQL query rather than get all the table data then try to sift through it in PHP.

 

2. Fetch the _row_ data from the result set: $row=mysql_fetch_array($result,MYSQL_ASSOC)

2a: you will now have an associative array called $row containing the row data.

 

3. use the field data from the row as you wish: $schoolid = $row['schoolid'];

$schoolname = $row['schoolname'];

$schoolabb = $row['schoolabb'];

 

To send both the schoolid and schoolname through a html form, you could try this:

<select name="schoolname"><option value=$schoolname/></select>

<select name="schoolid"><option value=$schoolid/></select>

 

then in the handling script you will either have $_GET['schoolname'] $_GET['schoolid'] or $_POST['schoolname'] $_POST['schoolid'] depending on your form method used.

Link to comment
Share on other sites

I am trying to populate the drop down list with all the schools in my database. So in response to 1a I do want more than one school. This page I am creating is to set up matchups between two of the 16 schools in the database. I want this form to pass both the school name and school id of the two schools picked over to a page that creates the matchup and then inserts the matchup into the matches database.

 

Link to comment
Share on other sites

ok now i get what you want to do.

 

afaik, the only way you could send both the school name and id through the one form <select><option> is to make the option value contain both values with a delimiter like:

$output .= '<option value="'.$schoolid.':'.$schoolname.'"/>';

 

where : is the delimiter. In the handling script you would have to explode this value on the delimiter to get the two values again.

 

The other option i can see is to only pass either the school name or the school id, and in the handling script look up each schools data from the database based on either the ids or names passed to the script.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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