Change my Query Value , following Select, selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stirnpanzer
Forum Newbie
Posts: 7
Joined: Tue Jun 02, 2009 6:38 am

Change my Query Value , following Select, selection

Post by stirnpanzer »

See below my code......

I populate a select box, with a list of Countries, which have and ID number associated with each.
I then want to run my query using the countries ID value (currently hardcoded to 81)

Any help greatly appreciated.... as you will see i'm still new to php... :!:

<html>
<head>
</head>
<body>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydbname", $con);

// Go and get Country's
print "<h4>Country : </h4>";
$query="SELECT country_name ,id FROM countries";
$result = mysql_query ($query);
?>

<!-- Populate Select with countries & country ID -->
<form action="display.php" method="post">
<select onchange="display_data(this.value);" name='countryname'>

<?php
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[country_name]</option>";
}
?>

</select>
</form>

<?php

//Want to select items_country from Select Option ID above....
$query = "select * FROM items WHERE items_country = 81" ;

$data = mysql_query($query) ;
Print $query;


Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Country:</th> <td>".$info['items_country'] . "</td> ";
Print "<th>Era :</th> <td>".$info['items_era'] . " </td>";
Print "<th>title :</th> <td>".$info['items_desc'] . " </td>";
Print "<th>Description :</th> <td>".$info['items_description'] . " </td></tr>";
}
Print "</table>";
?>

</body>
</html>
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Change my Query Value , following Select, selection

Post by mikemike »

So you want to populate your second query with the ID number from the first select box? Simply not possible how you're trying it.

PHP is server-side, this means it's parsed on the server before you see any of the page.

JavaScript is client-side, this means your browser parses it and does with it what it see's fit.

Because of this you can't use a value from JavaScript, go back in time to before the PHP was processed and change a PHP variable with one from JS.

What you need is AJAX so you can use JS to call a PHP page which will output to your original page the content that you want. Try googling 'php ajax tutorial'.
stirnpanzer
Forum Newbie
Posts: 7
Joined: Tue Jun 02, 2009 6:38 am

Re: Change my Query Value , following Select, selection

Post by stirnpanzer »

Thanks for pointing me in the right direction......

I now have a screen with 3 selections... What i want to do is take the result of the three queries and run one query on a table.
This is where i'm missing something...

See code i'm using (two php forms...):

Code: Select all

 
<?     
     echo "<form name=sel>\n";
     echo "Country : <font id=province><select>\n";
     echo "<option value='0'>==== Country ====</option> \n" ;
     echo "</select></font>\n";
     
     echo "era : <font id=amper><select>\n";
     echo "<option value='0'>==== Era ====</option> \n" ;
     echo "</select></font>\n";
     
     echo "Type : <font id=tumbon><select>\n";
     echo "<option value='0'>==== Type ====</option> \n" ;
     echo "</select></font>\n";
?>
 
<script language=Javascript>
function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};
 
function dochange(src, val) {
     var req = Inint_AJAX();
     req.onreadystatechange = function () { 
          if (req.readyState==4) {
               if (req.status==200) {
                    document.getElementById(src).innerHTML=req.responseText; //
               } 
          }
     };
     req.open("GET", "locale.php?data="+src+"&val="+val); //connection
     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
     req.send(null); //
}
 
window.onLoad=dochange('province', -1);     
</script>
 

Code: Select all

 
<?
     // cache
     header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
     header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
     header ("Cache-Control: no-cache, must-revalidate");
     header ("Pragma: no-cache");
     
     header("content-type: application/x-javascript; charset=tis-620");
     
     $data=$_GET['data'];
     $val=$_GET['val'];
     
     //
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname    = "mydb";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");  
     
     if ($data=='province') { 
          echo "<select name='province' onChange=\"dochange('amper', this.value)\">\n";
          echo "<option value='0'>==== Country ====</option>\n";
         // $result=mysql_db_query($dbname,"select loc_code,loc_abbr from location where loc_name = location_name and loc_code != '000000' and flag_disaster is null order by loc_abbr");
          $result=mysql_db_query($dbname,"SELECT id,country_name FROM countries");
          while(list($id, $name)=mysql_fetch_array($result)){
               echo "<option value=\"$id\" >$name</option> \n" ;
          }
     } else if ($data=='amper') {
          echo "<select name='amper' onChange=\"dochange('tumbon', this.value)\">\n";
          echo "<option value='0'>======== Era ========</option>\n";
          $val2=$val;
          $val = substr($val,0,2);                                 
          //$result=mysql_db_query($dbname,"SELECT loc_code, loc_abbr FROM location WHERE loc_code != '000000' and loc_code != '$val2' AND loc_code LIKE '$val%'  AND flag_disaster IS NULL ORDER BY loc_code, loc_abbr ");
          $result=mysql_db_query($dbname,"SELECT id,era_name FROM eras");
          while(list($id, $name)=mysql_fetch_array($result)){       
               echo "<option value=\"$id\" >$name</option> \n" ;
          }
     } else if ($data=='tumbon') {
          echo "<select  name='tumbon' >\n";
          echo "<option value='0'>======== Type ========</option>\n";
          $val2=$val;
          $val = substr($val,0,4);
          //$result=mysql_db_query($dbname,"SELECT loc_code, loc_abbr, loc_name, location_name FROM location WHERE loc_code != '000000' and loc_code != '$val2' AND loc_code LIKE '$val%' AND flag_disaster IS NULL ORDER BY loc_code, loc_abbr");
          $result=mysql_db_query($dbname,"SELECT id,type_name From type");
          while(list($id, $name)=mysql_fetch_array($result)){
               echo "<option value=\"$id\" >$name</option> \n" ;
          }
     }
     echo "</select>\n";  
     
?>
 
Last edited by stirnpanzer on Mon Jun 08, 2009 8:00 pm, edited 1 time in total.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Change my Query Value , following Select, selection

Post by mikemike »

If you want to properly format your code in [ code=php] tags then i might have time to read it, not sure i can be bothered to go through that much and format it all myself so it's readable I'm afraid
stirnpanzer
Forum Newbie
Posts: 7
Joined: Tue Jun 02, 2009 6:38 am

Re: Change my Query Value , following Select, selection

Post by stirnpanzer »

Wondered how it was done... thanks
Post Reply