Populating checkboxes using php

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

pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Post by pyoungson »

Now I get the following:
Array ( [Solvents] => Array ( [Vinyl S-ethyl mercapto ethyl ether] => Vinyl S-ethyl mercapto ethyl ether [Vinyl trifluoro acetate] => Vinyl trifluoro acetate [Vinyl-2-ethyl hexanoate] => Vinyl-2-ethyl hexanoate [Water] => Water ) ) You can't see this page without submitting the form.
However I now get no value for the maxdispersive so the above array can not be in the form to get anything from the mysql. I'm getting very confused about what exact input is wanted.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

That's what we wanted.. :D

Now change

Code: Select all

if ( isset($_POST['Solvents']) ) {
     $_POST['Solvents'] = implode('\', \'', $_POST['Solvents']);
   }
to

Code: Select all

if ( isset($_POST['Solvents']) ) {
     $SolventList = implode('\', \'', $_POST['Solvents']);
   }
and in your SQL statement

Code: Select all

$dispersive = mysql_query("select name, dispersive as max_dispersive
from solvents
where name in('{$_POST['Solvents']}')
and dispersive = ( select max(dispersive)
                          from solvents
                          where name in('{$_POST['Solvents']}')
                        )");
to

Code: Select all

$dispersive = mysql_query("select name, dispersive as max_dispersive
from solvents
where name in('{$_POST['Solvents']}')
and dispersive = ( select max(dispersive)
                          from solvents
                          where name in('{$SolventList}')
                        )");
The problem you are having is two fold.
First Solvent without setting it as an array in HTMLwill only store 1 value (the last set) hence the [] on the name. I included the name as an index purely to check what we are getting. It is prefectly OK to have it as name="Solvent[]".

Second problem is that $_POST is readonly. You cannot set it to something different.
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Post by pyoungson »

I do not understand why but it does not see SolventList as a variable:

I have changed the code in the way you noted:

Code: Select all

<?php

print_r($_POST);

if (array_key_exists('check_submit', $_POST)) {
   
   if ( isset($_POST['Solvents']) ) {
     $SolventList = implode('\', \'', $_POST['Solvents']);
   }

} else {
    echo "You can't see this page without submitting the form.";
}

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

mysql_select_db("solvents", $con);

$result = mysql_query("SELECT * FROM solvents WHERE Name 

IN('{$_POST['Solvents']}')");



$dispersive = mysql_query("select name, dispersive as max_dispersive
from solvents
where name in('{$_POST['Solvents']}')
and dispersive = ( select max(dispersive)
                          from solvents
                          where name in('{$SolventList}')
                        )");

while($row = mysql_fetch_array($dispersive))
{
$maxdispersive = $row['max_dispersive'];
}
I now get the following:
Array ( [Solvents] => Array ( [Vinyl S-ethyl mercapto ethyl ether] => Vinyl S-ethyl mercapto ethyl ether [Vinyl trifluoro acetate] => Vinyl trifluoro acetate [Vinyl-2-ethyl hexanoate] => Vinyl-2-ethyl hexanoate [Water] => Water ) ) You can't see this page without submitting the form.
Notice: Undefined variable: SolventList in C:\ I have edited this out \process1.php on line 32
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

No idea why $SolventList is not set... Will need to test and unfortunately haven't the time at present.
What happens if you change

Code: Select all

if ( isset($_POST['Solvents']) ) {
     $SolventList = implode('\', \'', $_POST['Solvents']);
   }
to

Code: Select all

if ( isset($_POST['Solvents']) ) {
     $SolventList = implode("', '", $_POST['Solvents']);
     echo "DEBUG --- Solvent id set";
   }
You would also need to change all your "IN ($_POST['Solvent']) queries to $SolventList when you get it working not just the one I did previously.
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Post by pyoungson »

Nothing happens if I make that change unfortunately. Thank you for your help so far, I wouldn't have got this far on my own.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

The reason that $SolventList is not being set is because there is no 'check_submit' key in the $_POST array ... hence the "You can't see this page without submitting the form." being displayed.

You didn't happen to remove the hidden "check_submit" field from your form did you?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

It might be easier to just change your submit button to something like ...

Code: Select all

<input type="submit" name="submit" value="1" />
You can then do ...

Code: Select all

if (array_key_exists('submit', $_POST)) {
... and lose the hidden field altogether. I'm not sure if you even have to set the value.
Post Reply