$_POST function operation
05-22-2006, 08:45 AM
|
$_POST function operation
|
Posts: 47
|
I have not been able to find an understandable complete definition of how $_POST works in making information available to the receiving script. Can anyone explain why the following code passes the info as shown, and where the rest of the info may be found?
This is a sample of the code for some of many checkboxs on the "sending" page:
Code:
<tr id="EH0" name="EH0"><td><div style="float:left"><input type="checkbox" name="chosenSkills" value="0" onclick="return checkCount('chosenSkills[0]', 'checkCounter', 'EH0');"></div>Product Positioning</td></tr>
<tr id="EH1" name="EH1"><td><div style="float:left"><input type="checkbox" name="chosenSkills" value="1" onclick="return checkCount('chosenSkills[1]', 'checkCounter', 'EH1');"></div>Advertising</td></tr>
<tr id="EH2" name="EH2"><td><div style="float:left"><input type="checkbox" name="chosenSkills" value="2" onclick="return checkCount('chosenSkills[2]', 'checkCounter', 'EH2');"></div>Pricing</td></tr>
Here is the initial code on the "receiving" page:
Code:
$sscboxcount=$_POST['checkCounter1'];
$chosenskill=$POST['chosenSkills'];
print_r($_POST);
And here is the output of the print_r($_POST statement when boxes 1 and 3 are checked:
Code:
Array ( [checkCounter1] => 2 [chosenSkills] => 3 [chooseskills_x] => 46 [chooseskills_y] => 9 )
"checkCounter1" is a count of the number of boxes checked, and is correct. "chosenSkills" contains a value assigned to each checkbox for identification purposes and therein lies my problem. Even when multiple boxes are checked, "chosenSkills" always displays the value assigned to the checkbox with the highest value . What I want is an array containing the values of ALL boxes checked. Can anyone help me with this??
Frodocyber
|
|
|
|
05-22-2006, 09:03 AM
|
Re: $_POST function operation
|
Posts: 40
Location: Denmark
|
Either use name="chosenSkills[]" in your form (then you will find the values as an array in $_POST['chosenSkills']) .
or change the name so the 3 element names are unique (chosenSkills1, chosenSkills2... or whatever)
I prefer the first 
|
|
|
|
05-23-2006, 07:55 AM
|
Re: $_POST function operation
|
Posts: 47
|
Thanks. The array suggestion is a good one but......
The checkbox script is all html and javascript. The html produces the checkbox page for selection...and the javascript validates and keeps track of the number of boxes checked. (1 to 3 required.) This count is also passed to the processing script for indexing purposes. The processing script (php) needs the values associated with each box checked and the number of boxes checked.
Any time I changed the vars in the html to create an array it screwed up the javascript functions. Catch 22.  As currently coded, the javascript does its job correctly but the html only passes one of the box values regardless of how many are checked.
The entire script is lengthy due to the number of check boxes. I could post it here or PM it to someone interested in helping sort this out.
Frodocyber
|
|
|
|
05-23-2006, 10:00 AM
|
Re: $_POST function operation
|
Posts: 31
|
See how you've named your rows? and given them IDs? theres no need to do that.
The reason why your'e only getting one value in the _POST array is because you've given every checkbox the same name
Radio button groups have the same name, because each group produces one value only.
try this in your HTML form...
HTML Code:
<input type="checkbox" name="skill_one" >
<input type="checkbox" name="skill_two" >
<input type="checkbox" name="skill_three" >
and this in your php script...
PHP Code:
echo "<pre>"; print_r($_POST); echo "</pre>";
Oh, and that validation script is a little messy, try writing a function that runs when the Submit button is clicked instead.
|
|
|
|
05-23-2006, 11:54 PM
|
Re: $_POST function operation
|
Posts: 47
|
Ok folks. Your help is working so far. My learning curve is not so steep now..but steep enough to lead me into dark corners. Here's where things sit at the moment.
Modified checkbox code:
Code:
<tr><td><div style="float:left"><input type="checkbox" name="chosenSkills[]" value="5"> </div>Recruiting</td></tr>
<tr><td><div style="float:left"><input type="checkbox" name="chosenSkills[]" value="6"> </div>Benefits management</td></tr>
<tr><td><div style="float:left"><input type="checkbox" name="chosenSkills[]" value="7"> </div>Employee Evaluations</td></tr>
<tr><td><div style="float:left"><input type="checkbox" name="chosenSkills[]" value="8"> </div>OSHA</td></tr>
Beginning code in the receiving script to see what arrived:
Code:
$chosenskill=$_POST['chosenSkills'];
echo "chosenskill =..";
echo $chosenskill;
echo "...";
print_r($chosenskill);
$counter=count($chosenskill);
echo "counter = ";
echo $counter;
And the results:
Code:
chosenskill =..Array...Array ( [0] => 6 [1] => 7 [2] => 8 ) counter = 3
The beginnings of success! The correct, and complete, data arrived ... but how to get at it in code. I want to compare the value of each array element to a constant and exit the compare loop if a match is found. I can handle the code to manage the loop but don't know the php syntax to point to each array element for the comparison. The php manual was not helpful! Anybody???
Frodocyber
|
|
|
|
05-24-2006, 03:38 AM
|
Re: $_POST function operation
|
Posts: 40
Location: Denmark
|
foreach is commonly used to compare elements. It works like this:
PHP Code:
foreach( $chosenskill as $skill ) {
print "Chosen skill: $skill<br />";
}
It should print all elements - one at a time.
Now, you can use if() or switch() instead of print and do whatever you need. Like:
PHP Code:
foreach( $chosenskill as $skill ) {
if( $skill == 1 ) {
print "Do the dance<br />";
}
else {
print "..<br />";
}
}
which will print "Do that dance" if you select the skill with value 1. Any other skill will result in ".." printed. If you dont know foreach() this would be a good time to read about it - its pretty useful.
With switch():
PHP Code:
foreach( $chosenskill as $skill ) {
switch( $skill ) {
case 1:
print "Do the dance<br />";
break;
case 2:
print "Do the surf!<br />";
break;
case 3:
print "Kill the smurf<br />";
break;
default:
print "..<br />";
}
}
Will print 3 different texts (for 1,2,3) and ".." if otherwise.
If you want to check the existance of a certain value in the array you can use in_array(). Eg check if 3 is "checked":
PHP Code:
if( in_array(3, $chosenskill) { ... }
Or you can use array_flip() that will make an array from "Array( 0=>value1, 1 => value2 ... )"; to "Array( value1 => 0, value2 => 1 ... )" (this assumes that your values are unique). and then use isset().
PHP Code:
$skills = array_flip($chosenskill);
if( isset($skills[3]) ) ) {... }
I prefer this one if i need to do alot of checks.
The following is how i handle alot of checkboxes (so it may or may not suit your needs)
If we return to my switch example (where the poor smurf is killed), that example can be written shorter. Depending on what you need, you might be able to do similar.
PHP Code:
# An array with all possible values:
$skills = array(
'1' => 'Do the dance',
'2' => 'Do the surf!',
'3' => 'Kill the smurf',
'4' => 'etc..' );
# Post data
$chosenskill=$_POST['chosenSkills'];
# To print the selected
print "You have seleted: <br />";
foreach( $chosenSkill as $s )
print $skills[$s] . "<br />";
# And similar when you want to make the form
forach( $skills as $k => $v ) {
print '<tr><td><div style="float:left"><input type="checkbox" name="chosenSkills[]" value="' . $k . '"> </div>'. $v . '</td></tr>';
}
I said it was shorter - which is is, if you consider what it do. With this "minimal" code, it can create the checkbox and post out whatever you selected. If you ever need to add more checkboxes, you only need to add more elements to the array.
I hope that some of this makes sense to you, otherwise feel free to ask.
|
|
|
|
05-24-2006, 03:45 AM
|
Re: $_POST function operation
|
Posts: 31
|
have a look at this example. I'm going to do a search on an array, and determine if "apple" exists.
PHP Code:
$fruit = array('pear', 'orange', 'apple', 'mango');
$find_me = "apple";
$key = array_search($find_me, $fruit);
if ($key == FALSE) { echo "$find_me was not found"; } else { echo "$find_me was found"; }
// OR
if ($key == FALSE) { echo "$find_me was not found"; } else { echo "$fruit[$key] was found"; }
|
|
|
|
05-24-2006, 03:20 PM
|
Re: $_POST function operation
|
Posts: 47
|
Thanks that was very helpful. I program in many languages but just starting in php and the syntax is more worrisome than the logical stuff. Most tutorials just give one syntax example and never bother to explain the full "normal" use of the feature such as ALL the issues involved with passing info to other scripts and unraveling it upon arrival. Kudos to all that offered help!
Frodocyber
|
|
|
|
05-25-2006, 09:42 PM
|
Re: $_POST function operation
|
Posts: 47
|
Thanks!! Each of those examples worked fine for what they did.
However, I'm trying to see if ANY of five fields in each database row contain ANY of the values in the passed array. If yes, record row id somewhere (and NOT in an array!) for later use and continue searching to end of database. If no, get the next database row and check that one and continue to end of DB rows.
There are from 1 to 3 possible values in the passed array and 36 possible values in each of 5 fields in each database row. Fun huh?
Would something like the following also work using multiple variables that might hold a matching value?
Code:
if(in_array($a or $b or $c or $d or $e, $chosenskill)
Here is the pertinent section of the code where I am checking for a match.
Code:
$foundcount=0;
$i=0;
while ($i < $num) {
$Afname=mysql_result($result,$i,"fname");
$Alname=mysql_result($result,$i,"lname");
$Aco_name=mysql_result($result,$i,"co_name");
$Aid=mysql_result($result,$i,"id");
$Acity=mysql_result($result,$i,"city");
$Astate=mysql_result($result,$i,"state");
$Acountry=mysql_result($result,$i,"country");
$Askill1_code=mysql_result($result,$i,"skill1_code");
$Askill2_code=mysql_result($result,$i,"skill2_code");
$Askill3_code=mysql_result($result,$i,"skill3_code");
$Askill4_code=mysql_result($result,$i,"skill4_code");
$Askill5_code=mysql_result($result,$i,"skill5_code");
foreach( $chosenskill as $skill ) { if ( $skill == $Askill1_code or
$skill == $Askill2_code or $skill == $Askill3_code or $skill == $Askill4_code or $skill == $Askill5_code)
{$foundcount ++; } else
{
i ++;
}
}
//at end of database search call next script to display found entries
?>
This code loops through the database and executes the true result for every row even though only some of the rows contain a match to an element in the array.
What am I missing??
Frodocyber
|
|
|
|
05-26-2006, 06:44 AM
|
Re: $_POST function operation
|
Posts: 40
Location: Denmark
|
Hi,
I'm not sure i understood all of your post, so please bear with me.
Quote:
|
Originally Posted by Frodocyber
However, I'm trying to see if ANY of five fields in each database row contain ANY of the values in the passed array. If yes, record row id somewhere (and NOT in an array!) for later use and continue searching to end of database. If no, get the next database row and check that one and continue to end of DB rows.
|
Why not an array? If you end up with multiple IDs to track, arrays would probably be best suited.
Quote:
|
Originally Posted by Frodocyber
Code:
if(in_array($a or $b or $c or $d or $e, $chosenskill)
|
That is possible, but not with in_array() - look at array intersect. It would probably be something like
Code:
if( count(array_intersect( array($a,$b,$c,$d,$e), $chosenskill)) > 0 ) { ..
Quote:
|
Originally Posted by Frodocyber
Code:
$foundcount=0;
$i=0;
while ($i < $num) {
$Afname=mysql_result($result,$i,"fname");
$Alname=mysql_result($result,$i,"lname");
$Aco_name=mysql_result($result,$i,"co_name");
$Aid=mysql_result($result,$i,"id");
$Acity=mysql_result($result,$i,"city");
$Astate=mysql_result($result,$i,"state");
$Acountry=mysql_result($result,$i,"country");
$Askill1_code=mysql_result($result,$i,"skill1_code");
$Askill2_code=mysql_result($result,$i,"skill2_code");
$Askill3_code=mysql_result($result,$i,"skill3_code");
$Askill4_code=mysql_result($result,$i,"skill4_code");
$Askill5_code=mysql_result($result,$i,"skill5_code");
foreach( $chosenskill as $skill ) { if ( $skill == $Askill1_code or
$skill == $Askill2_code or $skill == $Askill3_code or $skill == $Askill4_code or $skill == $Askill5_code)
{$foundcount ++; } else
{
i ++;
}
}
//at end of database search call next script to display found entries
?>
|
Let me try to rewrite:
Code:
$result = mysql("SELECT ... ") or die(mysql_error());
while( $row = mysql_fetch_assoc($result) ) {
// Now you can use $row[columnname'] e.g: $row['fname']
if( array_intersect(array($row['Askill1_code'], $row['Askill2_code'],$row['Askill3_code'],$row['Askill4_code'],$row['Askill5_code']), $chosenskill ) ) {
$foundcount++;
}
}
|
|
|
|
05-26-2006, 08:12 AM
|
Re: $_POST function operation
|
Posts: 47
|
I tried your code using array_intersect and got the following error warning.
Code:
$result=mysql_query($query);
while( $row = mysql_fetch_assoc($result) ) {
// Now you can use $row[columnname'] e.g: $row['fname']
if( array_intersect(array($row['Askill1_code'],
$row['Askill2_code'],$row['Askill3_code'],$row['Askill4_code'],$row['Askill5_co
de']), $chosenskill ) ) {
$foundcount++;
}
}
//at end of database search call next script to display found entries
Warning: array_intersect() [ function.array-intersect]: Argument #2 is not an array in F:\Program Files\xampplite\htdocs\skillsearchtest1.php on line 15
Other ideas??
Frodocyber
|
|
|
|
05-26-2006, 08:17 AM
|
Re: $_POST function operation
|
Posts: 40
Location: Denmark
|
Isnt $chosenskill a array? (can you print_r($chosenskill) and show the result)
|
|
|
|
05-26-2006, 06:34 PM
|
Re: $_POST function operation
|
Posts: 47
|
The warning message was due to my error in not backing up enough to start with the script that set the array. Doh!
Your suggested code does not find a match between the array values and the database row fields. Row 0 has a value of 1 in skill1_code, row 1 has a value of 2 in skill2_code, row 3 has a value of 3 in skill3_code. The array has values of 1, 2, 3 in this example. There should be three matches found...row 1, row 2, and row 3. $foundcount should be 3.
I do not understand why the echo of $row shows "Array" instead of some value??
Result of correct script execution:
Code:
print_r = Array ( [0] => 1 [1] => 2 [2] => 3 ) ...row =... Array...row =... Array...row =... Array...row =... Array...row =... Array...row =... Array...row =... Array...foundcount =
Current code:
Code:
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$chosenskill=$_POST['chosenSkills'];
echo "print_r chosenskills = ";
print_r($chosenskill);
$counter=count($chosenskill);
$query="SELECT * FROM current_members";
$result=mysql_query($query);
while( $row = mysql_fetch_assoc($result) ) {
echo "...row =... ";
echo $row;
// Now you can use $row[columnname'] e.g: $row['fname']
if( array_intersect(array($row['Askill1_code'],
$row['Askill2_code'],$row['Askill3_code'],$row['Askill4_code'],$row['Askill5_co
de']), $chosenskill ) ) {
$foundcount++;
}
}
//at end of database search call next script to display found entries
echo "...foundcount = ";
echo $foundcount;
exit();
?>
|
|
|
|
05-27-2006, 02:56 PM
|
Re: $_POST function operation
|
Posts: 84
|
You cant echo an array, but you can echo its fields.
This is the correct way as seen from your code $row['Askill2_code']
|
|
|
|
05-27-2006, 03:01 PM
|
Re: $_POST function operation
|
Posts: 84
|
Looking at your original coding, you are doing a trial and error here, i would suggest a thourough study of php, learn from tutorial online, this will end up us doing this script of yours.
PEACE
|
|
|
|
05-27-2006, 11:27 PM
|
Re: $_POST function operation
|
Posts: 47
|
If you reviewed this thread fully you would have seen the only questions revolved around how to make reference to an array element in php code. Other languages I have used made the syntax simple, i.e., Array(1), Array(2), etc.
So far none of the samples provided have answered this question. A close look at the thread will reveal the "trials and errors" were all the result of testing the offered array samples, none of which have worked...so far.
I do not ask anyone to write my scripts and rudeness is not adult behaviour. I have the manual, have reviewed the tutorials, developed and fully tested over 25 scripts for this web site, and enjoyed the assistance of many fine people here. Should it be below your station to assist a new php programer in understanding php unique syntax then please don't trouble yourself.
To all others that have contributed to my better understanding of php a BIG thanks!!!
Frodocyber
|
|
|
|
|
« Reply to $_POST function operation
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|