Link to home
Start Free TrialLog in
Avatar of matd_
matd_

asked on

Getting the value from a select list box using php?

Hi,

I'm populating a select list from a mySQL database like so :-

<?

// Populate Type List Box with up to date Equipment Types
$sql = "SELECT * FROM types ORDER BY type ASC";

// execute SQL query and get result
$sql_result = mysql_query($sql,$dbcnx)
or die("Couldn't execute query.");

// put data into drop-down list box

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

$type = $row["type"];
$typedesc =$row["typedesc"];

$option_block .= "<OPTION value=\"$type\">$typedesc</OPTION>";
}

?>

<SELECT name="selecttype" id="selecttype">
<? echo "$option_block"; ?>
</SELECT>

What i'd like to know is how i can set my field type = to the value of the selected item in the select list.

For example : i select the item 'Electrical Motors' from the list, the value of this is 'ecm' - i need to store that value in the field named type in mysql table.

I'm getting confused as i thought u get the value using Java Script and am not sure of the best way or when to break out of php and javascript etc.

Would i be right in saying it's somthing like this :-

$type =?><script>document.theform.selecttype.value</script><?;

That doesn't work obviously.... but i'm trying  

This is probably pretty simple, but i'd really appreciate it if someone could give me an example please?

Thanks in advance
Avatar of splishsplash
splishsplash

I'm confused about what your question is. One interpretation I have is that you have a <form> with a <select> element and a submit button somewhere. You hit submit, and the <form> posts back to itself and you want the <select> to have the values that the user chose?

If that is what you are asking, you need to put an IF statement to check if the current $type is what the user submitted previously then include "CHECKED" in the <option> tag:

while ($row = mysql_fetch_array($sql_result)) {
$type = $row["type"];
$typedesc =$row["typedesc"];

$checked = "";  
//if the submitted 'type' is equal to the current $type in the loop
if ($selecttype == $type) $checked = 'CHECKED';

$option_block .= "<OPTION value=\"$type\" $checked>$typedesc</OPTION>";
}



Hoi,

Implicitely answered by splishsplash, but I will clarify what I think you wanted to know:

To obtain which option of the selectbox was selected, you don't need javascript. In your case the value will be stored
in the variable $selecttype (the name of the SELECT tag), and it will have as value the value attribute of the selected option.

So if you selected Electrical Motors and the corresponding option was:

<SELECT name="selecttype">
<OPTION value="ecm">Electrical Motors</OPTION>
...

</SELECT>

Then you will have $selecttype == 'ecm' after submission.

It depends on the server settings also how to obtain values
of submitted variables though too, but it goes the same
way as <INPUT name="textbox" type="text"> fields.
Avatar of matd_

ASKER

Sorry for the confusion.

Here's the whole section of code :-

<p>Browse through some of the equipment for sale:
           
<?
                     
  include("config/config_sql.php");
               
  // create connection
  $dbcnx = mysql_connect("$server","$user","$password")
                    or die("Couldn't make connection.");
               
  // select database
  $db = mysql_select_db("$database", $dbcnx)
                    or die("Couldn't select database.");
               
  // create SQL statement
  $sql = "SELECT * FROM types
                         ORDER BY type ASC";
               
  // execute SQL query and get result
  $sql_result = mysql_query($sql,$dbcnx)
                    or die("Couldn't execute query.");
               
  // put data into drop-down list box
  while ($row = mysql_fetch_array($sql_result)) {
               
  $type = $row["type"];
  $typedesc =$row["typedesc"];

  $option_block .= "<OPTION value=\"$type\">$typedesc</OPTION>";
               }
               
  $type = $_POST['selecttype'];
               
  ?>
                             
<FORM method="POST" name="selectform" action="listequipment.php?forsaleorwanted=s&type=<?echo $type;?>">
               
  <P>Select a category :<br>
                         
  <SELECT name="selecttype">
               
  <? echo "$option_block"; ?>
                         
  </SELECT>    
               
  <P><INPUT name="forsale" type="submit" id="forsale" value="List"></p>
               
</FORM>

I want to pass the value of the SELECT 'selectype' to a new page as a variable called $type. I thought i did this by using $type = $_POST['selecttype']; but when i check the url being passed to the new page, type is emtpy.
Avatar of matd_

ASKER

Also tried changing the line :

<FORM method="POST" name="selectform" action="listequipment.php?forsaleorwanted=s&type=<?echo $type;?>">

to :

<FORM method="POST" name="selectform" action="listequipment.php?forsaleorwanted=s&type=<?echo $selecttype;?>">

But type is still empty. i have checked $option_block and that is getting populated with all the correct values.
When you use the POST method of the form, there will
be no visible values in the URL string (as opposed
to the GET method).

I recommend to try posting your form to phpinfo.php,
where phpinfo.php contains only:

<?phpinfo()?>

Then you will certainly see where all your variables have gone :-).
Avatar of matd_

ASKER

Also tried changing the line :

<FORM method="POST" name="selectform" action="listequipment.php?forsaleorwanted=s&type=<?echo $type;?>">

to :

<FORM method="POST" name="selectform" action="listequipment.php?forsaleorwanted=s&type=<?echo $selecttype;?>">

But type is still empty. i have checked $option_block and that is getting populated with all the correct values.
Avatar of matd_

ASKER

Also tried changing the line :

<FORM method="POST" name="selectform" action="listequipment.php?forsaleorwanted=s&type=<?echo $type;?>">

to :

<FORM method="POST" name="selectform" action="listequipment.php?forsaleorwanted=s&type=<?echo $selecttype;?>">

But type is still empty. i have checked $option_block and that is getting populated with all the correct values.
ASKER CERTIFIED SOLUTION
Avatar of spookje
spookje

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of matd_

ASKER

Hmmm... heh, sorry for the duplicate posts - not sure what happened there.

Thanks for the comment about phpinfo spookje that does show that the $type IS being passed...

But on the listequipment.php page how can i access the $type variable?

     // Request the equipment from search criteria
     
     $result = @mysql_query("SELECT ID, dateadded, timeadded, manufacturer, description, type, modelorpartno, kwrating, voltage, controlvolts, pricerange, additionalinfo
      FROM equipment where type = '$type'");
     if (!$result) {
     echo("<p>Error performing query: " . mysql_error() . "</p>");
     exit();
     }

all this statement does is list equipment with type = NULL as $type is empty...
So, also in listequipment did you echo the variable
to screen, before the insert query?

If it shows the value too, then $type is REALLY passed! :-)

In that case, you may have an error in your insert query.
Perhaps type is an ENUM which doesn't allow 'ecm' ?

try

describe equipment;

in the mysql shell. Also try to use

$sql = "INSERT ..."; // fill in the rest...
echo $sql;
$result=@mysql_query($sql);

Then copy the query from your browser to the mysql shell
and see what's mysql's opinion about your query...


Avatar of matd_

ASKER

I've done it! Sorry for the confusion and silly questions, just my total lack of knowledge that caused me problems.

I'll give the points to spookje as it was posting to phpinfo that really showed me what was going on and in the ned helped me.

Thanks a lot! :-)
Avatar of matd_

ASKER

Very helpfull and prompt.

Thank you very much for your help, it's greatly appreciated.

Regards,

matd_ :-)
Welcome :-). Anyway your question is not silly. PHP is
a weird language to debug, and it takes some time
to learn ... at least I needed quite some time :-).
Avatar of matd_

ASKER

It's weird, but it seems to be the simplest things that i struggle with like getting values from form components - but i know now and it's helped me greatly.

It'll be nice when i learn more and can help others like yourself. Forums like this are invaluable :-)

Out of all the languages i've tried i still prefer PHP :-)

Don't know what platform your developing on? But i'm running Apache version 2.0.43, php Version 4.2.3, MySQL Version 3.23.55 on Windows XP Pro on a PC i use on my LAN as a dedicated server.

Really strange problem, my site only runs properly if i use it on the server itself. If i try and run any php scripts even phpinfo on any other pc on my LAN it either doesn't work at all or i get spurious results. The php pages won't appear at all or might appear if i refresh the page a lot of times.

I've tried older versions of apache and php but i get the same strange results.

The only way to test my developments is to access my server using Remote Desktop Connection which is far from ideal... i know i could run Apache etc on my development pc but i don't really want to have to run apache etc on my own pc - i could do wihout the overheads, besides thats what a server is for isn't it ;-)

Any ideas?
I am running also quite the newest releases of Apache, PHP and MySQL... but on a RedHat Linux system.

When I read about your problem, I think it has to do with your LAN configuration, unless your server has
a hard time to do what you ask it to do :-).

Maybe your network is not stable? Do you have firewalls?
Oh yes :-) Maybe you told Apache not to serve to any
other machines?
Avatar of matd_

ASKER

The server is a P4 2Ghz, with 1GB RAM so no problem there ;)

I am running Norton Personal Firewall 2003 but i have tried disabling that but it makes no difference.

html pages and normal php pages that don't query mysql work fine.

But anything like my pages or somthing like phpinfo or phpMyAdmin don't work or work irratically.....

It's very odd and very annoying ;-)
As long as you still can type smileys :-)...

Unfortunately I don't know much about apache on Windows,
but somehow everything points to your mysql-server then.

But it's weird: it is the apache from the same webserver
that makes the contact, no matter from where you are
requesting a script. What I want to say is that there's
no reason why in some cases your mysql doesn't work, if
PHP always works...

BTW, your server is strong, but maybe your network
devices are not?

In my company, all internet went through one telephone
cable for 2 years (ADSL). But the same cable could never
support a stupid FAX machine - that was the appearent
truth later and we were wondering what was wrong with
the FAX... but it was the cable that was not OK :-)
Avatar of matd_

ASKER

Haha, well i built the network from scratch and its only 3 pc's and a laptop anyway so its a very small LAN - i'll check MySQL, maybe the version i'm using has a problem with the windows platform and the combination of apache and php versions too.... i'll try an older version of MySQL and see what thats like...

1 simple question for you :-)

I can get the value from a SELECT list box now, but how can i assign a variable to the SELCT list box description for the chosen item.

For example $type holds a value of 'eccd' how can i assign a variable like $typedesc to 'Electronic Control Devices' that is the description for the chosen item in the SELET list?

Hope you don't mind me asking another, it's hard not to when you find someone so helpfull! ;-)
Avatar of matd_

ASKER

Haha, well i built the network from scratch and its only 3 pc's and a laptop anyway so its a very small LAN - i'll check MySQL, maybe the version i'm using has a problem with the windows platform and the combination of apache and php versions too.... i'll try an older version of MySQL and see what thats like...

1 simple question for you :-)

I can get the value from a SELECT list box now, but how can i assign a variable to the SELCT list box description for the chosen item.

For example $type holds a value of 'eccd' how can i assign a variable like $typedesc to 'Electronic Control Devices' that is the description for the chosen item in the SELET list?

Hope you don't mind me asking another, it's hard not to when you find someone so helpfull! ;-)
Avatar of matd_

ASKER

Haha, well i built the network from scratch and its only 3 pc's and a laptop anyway so its a very small LAN - i'll check MySQL, maybe the version i'm using has a problem with the windows platform and the combination of apache and php versions too.... i'll try an older version of MySQL and see what thats like...

1 simple question for you :-)

I can get the value from a SELECT list box now, but how can i assign a variable to the SELCT list box description for the chosen item.

For example $type holds a value of 'eccd' how can i assign a variable like $typedesc to 'Electronic Control Devices' that is the description for the chosen item in the SELET list?

Hope you don't mind me asking another, it's hard not to when you find someone so helpfull! ;-)
Hi sorry I was off for some days...

Hm, I hope you are lucky and find an answer to your MySQL
trouble - but maybe people on the PHP windows group can
help you.

Hm - there are 2 ways how to solve your problem:

1. (trivial and you know it :) Ask your database:
  "SELECT typedesc FROM items WHERE type='$type'"
Actually I think this is the best way. I will tell you
later why.

2. Pass the variables using JavaScript (actually I would
have to make a little research to realize it), you
will add a variable to the form, and set it 'on submitting'
the FORM (use the onsubmit property of the FORM),
or alternately, define the entire array of $typedesc['type']
in your FORM.

The 2nd solution depends a bit on the clientside Javascript,
and I think is even more complicated than the first one -
but maybe the only way to go, if the receiving script has
no access to the DB (because perhaps it is on another server).

I hope you didn't give up on me, but certainly you are notified that I posted something. :-)

Sorry again for the delay...
Avatar of matd_

ASKER

Hi spookje!

Sorry, i've been away too!

i got that last one working now thanks :-)

Just a quick question if you don't mind, i've run out of points now and i now it's a bit cheeky to ask questions of this original post, but i hope you don't mind...

I have a script that i include to show images on pages :-

<?php

if($id) {

     include("config/config_sql.php");

    @MYSQL_CONNECT("$server","$user","$password");

    @mysql_select_db("$database");

    $query = "select picture,mimetype from equipment where ID='$id'";
    $result = @MYSQL_QUERY($query);

    $data = @MYSQL_RESULT($result,0,"picture");
    $type = @MYSQL_RESULT($result,0,"mimetype");
     
    Header( "Content-type: $type");
    echo $data;

};

?>

What i'd like to do is display a file called nopic.jpg if there is no current pic/blob held for the item requested.

Do you know how it's done?

Thanks for your help as always :-D

Matd
Hoi Matd,

What does $data contain, when there is no picture?

If it is an empty string, something like the following
should help:

if(strlen($data) < 1) {
  header("Location: nopic.jpg");
} else {
  header("Content-type: $type");
  echo $data;
}

Don't worry about points. I am a mathematician, and as
far as I know points are infinitely small :-).

Just I hope I have been able to help you!

spookje
Avatar of matd_

ASKER

spookje,

Right again as always :-)

Thank you very much.

Your help is greatly appreciated.

matd :-)
Avatar of matd_

ASKER

Me again! ;-)

If i have the primary key of a table set as ID which is set to auto-increment, how can i find out the auto-increment value that has been assigned to a new record using the INSERT command. This is the statement i'm using below:-

      $sql = "INSERT INTO equipment SET
       
              memberid ='$memberid',
              forsaleorwanted='$forsaleorwanted',
           manufacturer ='$manufacturer',
           description ='$description',

      if (@mysql_query($sql)) {

         etc......

Is there a command i can use to set $ID = the new records ID?

Thanks again as always :-))
Avatar of matd_

ASKER

After more looking i found it!

$NEWID = mysql_insert_id();

Thanks anyway, i'm sure you knew that one ;-)

Mat :-)
Avatar of matd_

ASKER

Hi!

I've nearly finished this monster site that i've been working on at last, but!

I have a page where people can submit a new advert to the site and it has an upload facility, were they can upload images.

Is there a way i can show the temp image they just uploaded wihout having to store it in mysql?

The reason i ask is i don't want to end up with a load of images that arn't being used and filling up server space...

Or is there an easy way of removing uploaded images that arn't going to be used / stored in the database?

ie if the user quits the page or uploads another image i'll be left with a load of images i don't need...

Any ideas please? :-)
Hi matd,

It is actually not exactly clear to me, what you want to do with the image...
But one point is, you can easily manage your database content by installing
a tool like 'php-my-admin' which you can download at

http://www.phpmyadmin.net/

So then you can manually throw away images that you don't need.
If the user has to decide that an image is stored or not, you will have
to create an interface for that (the usual task of those who program PHP :)

Another idea may be you write a PHP script that deletes all 'obsolete'
images. Then you install a scheduled task that visits that script once
a day or so. In Linux you would do that in a crontab, in Windows there
is probably an analogue way.

I hope this may be any idea? :-)

Cheers,

Spookje
Avatar of matd_

ASKER

Hi Spookje :-)

It's ok, i've got it working the correct way now, where by i just unlink the file after its been uploaded into /tmp and either just store it as a BLOB or disregard it. I'll also write a script to sweep through every day or 2 to get rid of the old images that wern't un-linked.

Thanks for your help :-)
Avatar of matd_

ASKER

Hi spookje :D

Do you know how i can provide a link on my website admin's pages so they can click a link 'Backup the database' which would dump the whole mysql database as a gzip file and then prompt them to save it? - just like the 'export' feature in phpmyadmin? (tbl_dump.php)
Hi matd,

If the server were a Linux box, I would use the command
mysqldump and gzip (as a server side command).
Prompting the user to save the file as gzip is done by
sending a few headers and then the file itself.

However I don't know much about Windows PHP-mysql.
If I were you, I would try to find the essential code
in php-my-admin, and copy that part to your script.

I hope this helps :-)

Chao,

spookje