Author Topic: How to run a query from a drop down box.  (Read 326 times)

0 Members and 1 Guest are viewing this topic.

Offline pobmanTopic starter

  • Irregular
  • Posts: 8
    • View Profile
How to run a query from a drop down box.
« on: March 17, 2010, 05:19:50 PM »
Hi,

I am very new to php and mysql and want to write a little php app to display data from a DB.

with help I have managed to get a dropdown box working using the code below.
In the dropdown box I get a list of servers eg "merak"
I now want to use this varrible "merak" and run the following query and display it in the page, how do I make this happen.

The query was going to be: (straight from phpmyadmin)
Code: [Select]
$sql = 'SELECT `Name`, `Make`, `OS`, `Version` FROM `CES` WHERE `Name` = CONVERT(_utf8 \'merak\' USING latin1) COLLATE latin1_swedish_ci';
My current dropdown script:
Code: [Select]
<form class="table" action="check.php" method="post">
               <div class="inner-form">
                  <div class="msg msg-info">
                        <p>Please select which server you wish to edit.</p>
                        </div>
                  <?
mysql_connect(localhost, root, admin);
@mysql_select_db(servers_db) or die( "Unable to select database");

echo "<select name=form>n<option selected>Choose a Server</option>";
$query=mysql_query("SELECT * FROM `CES` WHERE `Name` IS NOT NULL");
while($row = mysql_fetch_array($query)) {
echo "<option value='{$row['Server']}'> {$row['Name']}</option>";

//echo "<option value=\"" . $row[Server] . "\">" . $row[NAME] . "</option>";
}

    ?></div>
    </select>
<input type="submit" VALUE="Submit">
            </form>


Thanks

Offline nafetski

  • Enthusiast
  • Posts: 270
    • View Profile
Re: How to run a query from a drop down box.
« Reply #1 on: March 17, 2010, 06:10:33 PM »
If you want to have it do when an option is chose on the dropdown, you will need to use javascript.

That, or you can put a submit button and do a regular POST :)
Dev Environment: Mac - OSX Snow Leopard / Eclipse / Kohana PHP Framework
Job: Sr Software Developer: (Large scale enterprise)
Notice:  Most of my forum posts I write on my iPhone while taking a dump.  This means that I don't test most of my code, and I might sound like I'm impatient...really I'm just busy punching a grumpy
Also: I've hit Google so many times it's asking for a divorce

Offline pobmanTopic starter

  • Irregular
  • Posts: 8
    • View Profile
Re: How to run a query from a drop down box.
« Reply #2 on: March 17, 2010, 06:29:49 PM »
Thanks for your reply, it would be the submit button I am after, javascript another day.

How do I change the code to do this.

Offline jacsdev

  • Irregular
  • Posts: 4
    • View Profile
Re: How to run a query from a drop down box.
« Reply #3 on: March 17, 2010, 07:26:31 PM »
Hi, what's up??? i think right now in a simple solution. there are many more solutions, i would like with ajax, but first the first

Code: [Select]
<?php
    
// @jacsdev
// define your data connection
$host '';
$user '';
$pwd  '';

$db mysql_connect($host$user$pwd) or die("Sorry, it's not possible");
mysql_select_db($dbName);


if(isset($_GET['server']) && !empty($_GET['server']))
{
$server $_GET['server'];
// now, as you see, we have in $server the value selected in dropdown list
// so, we can do our query now:

// this query is an example, you can modify
$cn_server mysql_query("select * from other_table_with_data where server_name='".$server."'");
//..
}

?>


<form action="" onsubmit="return validateForm();">
Choose a Server:
<select name="myServers" id="myServers" onchange="selectMyServer(this.value);">
    <option value="-">-</option>
        <?php
$cn mysql_query("select server_name, server_ip, server_type from server_list_table");
while($rs mysql_fetch_array($cn))
{
?>

                <option value="<?php echo $rs['server_name']; ?>"><?php echo $rs['server_name']; ?></option>
                <?php
}
?>

    </select>
    <br />
    <br />
    <input type="submit" value="Send Data" />
   
</form>

<script language="javascript" type="text/javascript">
function selectMyServer(server)
{
if(server != '-')
{
// i get current page
var myCurrentPage = location.href;

// now, i setting my current page.  we must add selected server in new url
location.href = myCurrentPage +'?server=' + server;  // redirecting to curret page
}
}


function validateForm()
{
if(document.getElementById('myServer').value !='-')
{
alert('please, choose a server');
return false;
}
}
</script>

when you choose a server in dropdown, the onChange event is launched. this event invoke to selectMyServer function, passing selected server as argument... so, the function redirect to same page with the name of selected server and then you can do a query with this value..

i hope help you friend
@jacsdev


Offline pobmanTopic starter

  • Irregular
  • Posts: 8
    • View Profile
Re: How to run a query from a drop down box.
« Reply #4 on: March 17, 2010, 09:06:37 PM »
Hi jacsdev,

Thanks for your post.

I have made the changes to your script as follows for the sql querys:

  
if(isset($_GET['server']) && !empty($_GET['server']))
   {
      
$server $_GET['server'];
      
// now, as you see, we have in $server the value selected in dropdown list
      // so, we can do our query now:

      // this query is an example, you can modify
      
$cn_server mysql_query("SELECT * FROM CES WHERE Name ='".$server."'");
      
//..
   
}

?>

<form action="" onsubmit="return validateForm();">
   Choose a Server:
   <select name="myServers" id="myServers" onchange="selectMyServer(this.value);">
       <option value="-">-</option>
        <?php
         $cn 
mysql_query("SELECT * FROM `CES` WHERE `Name` IS NOT NULL");
         while(
$rs mysql_fetch_array($cn))
         {
            
?>
                   <option value="<?php echo $rs['server_name']; ?>"><?php echo $rs['server_name']; ?></option>
                <?php
         
}
      
?>


Howver It is not returning a list of the servers in the dropdown box from the "Name" column in "CES"

Have I missed out some configuration?

Thanks


« Last Edit: March 17, 2010, 09:15:45 PM by pobman »

Offline pobmanTopic starter

  • Irregular
  • Posts: 8
    • View Profile
Re: How to run a query from a drop down box.
« Reply #5 on: March 17, 2010, 10:30:11 PM »
I have been going over the script and realised I had a few configuration errors.

I have now made the changes to what I think is all the correct places but the results in the drop down box is still empty except the "-" value.

Can you suggest some other changes I need to make.

thanks

   if(isset($_GET['server']) && !empty($_GET['server']))
   {
      
$server $_GET['server'];
      
// now, as you see, we have in $server the value selected in dropdown list
      // so, we can do our query now:

      // this query is an example, you can modify
      
$cn_server mysql_query("SELECT * FROM CES WHERE Name ='".$server."'");
      
//..
   
}

?>

<form action="" onsubmit="return validateForm();">
   Choose a Server:
   <select name="myServers" id="myServers" onchange="selectMyServer(this.value);">
       <option value="-">-</option>
        <?php
         $cn 
mysql_query("SELECT * FROM `CES` WHERE `Name` IS NOT NULL");
echo 
"SOME TEXT";
         while(
$rs mysql_fetch_array($cn))
echo 
"<option value='{$rs['Name']}'> {$rs['Name']}</option>";

         {
            
?>
                   <option value="<?php echo $rs['Name']; ?>"><?php echo $rs['Name']; ?></option>
                <?php
         
}
      
?>
    </select>
    <br />
    <br />
    <input type="submit" value="Send Data" />

</form>


Offline jacsdev

  • Irregular
  • Posts: 4
    • View Profile
Re: How to run a query from a drop down box.
« Reply #6 on: March 18, 2010, 09:22:49 AM »
what's up man!!! how are you?, maybe you must verify the mysql query string, check if it produces any data result.
 you can try the following: copy and paste your query in sql option of phpmyadmin, and run it... then verify if your query works :D

now, when you choose a server and the page is loaded again, you must define the query that you'll use for extract new results of the other table.. also you must write the instruction that transforms query in a array type.. ex:

//below line where it say, we need write other instruction ..
$cn_server = mysql_query("SELECT * FROM CES WHERE Name ='".$server."'");
//here
while ($rs_server = mysql_fetch_array($cn_server))
{
  echo $rs_server['here i write name of field'];
}


//hey, a little fix,  inside validateForm function, instead  myServer, write myServers

by te way, i saw than you write ' echo "SOME TEXT" '  inside select tag, well, in that place you can't see output data, just it possible in the option tag value:  <option>here i am visible</option>
 :D

@jacsdev