Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

posting across my multiple checkbox names/values 1

Status
Not open for further replies.

c0deM0nK424

Programmer
Oct 28, 2007
126
GB
basicly i have a form where the user enters a price in the form of 5.99, clicks the submit button and then a php script ive written opens up a text file, a comma delimitted text file in the form of:

shadow dancer,amiga 500,1991,3.99,25,
final fight,amiga 500/500+,1991,4.59,40,
rodland,amiga 500,1992,1.50,65,
the adams family,amiga 500+,1993,2.59,55,
arabian nights, amiga 500+,1994,5.99,43,
batman:the movie,amiga 500,1989,3.99,60,
apidya,amiga 500+,1994,3.69,55,

the file holds classic computer games for the old commodore amiga 500/500+ platform. the 5 fields hold information on the game title, the amiga machine it runs on, the year of release, the price and the last field holds how many of those games are in stock.

now, when the user enters a price, say 4.59 - my script displays all the games that are below that price - onto the browser appended with a checkbox next to it. so basicly only those games that are under 4.59 are shown line by line with a checkbox next to it.

my script does that fine, but its the next bit im trying hard to figure out. what i want it to do from here is allow the user the option to select some or all of those games listed (that fell below the price user entered in the form). the user checks two boxes, which correspond to that game and then hits a submit button.

in the background , upon the user hitting the submit button, i need to somehow retrieve the checkbox that was checked alongside its value (which ive assigned as a variable , that holds a line of text read in from the text file) and using each of these pieces of data, go back to the games.txt file and ammend the last field, the stock level. i know there is going to be reading/writing back to the text file but its just GETTING the details of games chosen back out so i can do the reading/writing and changing of stock for that game.
when you see my scripts, you'll see why i cant use name='something[]' value="" , as ive tried to assign the counter variables value to the value of the checkboxes themselves, i.e $boxname = $count.

are you with me on this ?

its extremely hard to figure out how to do this, i heard you can pass the checkbox name and values as hidden fields - how one retrieves hidden fields is beyond me. some tips or advice would be welcomed, just to point me in teh right direction.

i will paste my scripts/code on here now.

============================================================
<html><head><title> checkbox fields </title> </head> </body>
<FORM METHOD="POST" ACTION="bought2.php">

<?
$price = $_POST['price'];
$filename = "games.txt";
$filepointer = fopen($filename,"r");
$myarray = file($filename);



for ($mycount=0; $mycount < count($myarray);$mycount++)

{


$aline = $myarray[$mycount];

$HF = $aline;
$boxname = $mycount;
pricefind($aline,$price,$boxname,$HF);


}

function pricefind($a,$p,$bn,$HF)
{

$fields = explode(",",$a);

if ($fields[3] < $p)

{

$game = implode(",",$fields);

print "$game <INPUT TYPE='checkbox' NAME='$bn' value ='$game'>"."<br>";
print " <input type = 'hidden' name = '$bn' value = '$HF' >";

}



}



?>
<p>
<input type="submit" name="button" value="click to buy Games's">
</form></body></html>
=========================================================

the mytest.htm script is a form with a text field , that is named 'price'.
 
After POST, all fields, including hidden, are sent to bought2.php. If you populate the values of the hidden fields on load, then that's what will be passed. You'll need javascript to change the hidden fields when clicked (onClick event) on the client. If you do it like this, you'll need to capture all fields in the $_POST array. If that checkbox = on, then it's checked. If that field does not exist, it's not checked. In this case, I don't think you'll need hidden fields.

In bought2.php, use
<?php echo '<pre>';print_r($_POST);echo "</pre>";die;?>
To see what is being passed.

I would recommend avoiding a text file. If you have access to a database like MySQL, it will certainly lessen the frustration you'll find in the future with this project.

Mark
 
thanks for getting back to me Mark, but with all due respect, what did print_r achieve that foreach($_REQUEST as $key => $value)...print key, print value didnt ?

with the foreach $ request... i just see all the names value pairs in the array lol.

maybe you misunderstood what im stuck on.

you know when you reach the mytest.php , click to buy games page ? those games are those which fell under the max price the user entered in mytest.htm


so i provide the user with the results of all games that fall under that price, and they know are presented with checkboxes beside those games.

From here, this is whats important, from here the user will click 1 or many checkboxes and then clicks the button.

i was just wondering how to pass JUST those games that were checked.

but yes, the print_r function is a powerful little bugger, and its shed some light into how to tackle this.

javascript is client side validation stuff, i steer clear from mixing and moshing php with javascript as theres really no need, php is more than adept at meeting the needs of dynamic web design i would have thought heh :)

do please get back to me if you figure out a way to get round this, and yes a database would suit this kind of project more - however, we actualy have to read in from a text file for now.

all the best


lazy coder
 
Just realised it could have helped if i had left comments in my code..sorry about that.
 
call your checkboxes as follows
Code:
<input type="checkbox" name="gameID[gameID]" />

where $gameID is the id (or unique key) of the relevant game.

the browser will ONLY submit those checkboxes that have actually been clicked. so you then just perform a loop on $_POST['gameID'].

Code:
foreach ($_POST['gameID'] as $gameID=>$value){
  // do something with $gameID
  // ignore $value
}
 
thanks a million, i really appreciate that jpadie ! :)

i shall try that out and get back to you, it may just be the correct approach to my problem.


best wishes

~CodeMonk~
 
by the way jpadie, i have yet to try your method but im sure its gonna work ...there ARE numerous ways round my problem no doubt about it, i just realised that if i comment out this line:

$boxname = $mycount

and rename my checkbox name as '$bn[$mycount]


then when i select how many number of games i want, by checking the respect checkboxes

when i hit the submit button


a script with the code


foreach($_REQUEST as $key => $value)
{

print $key . "=" . $value ."<br>";

}


seems to show me WHAT i selected. heh.


remember, i now gotta go back to the text file and subtract '1' from the corresponding (matching) line of text's 'stock' , so that a game was purchased.

im thinking about simply reading thru the file line by line and doing a string comparison, if its the same - then take that line of text , explode it , grab value[4] , copy it into a variable or just do $variable = $fields[4], followed by $variable = $variable - 1.

having done that, i guess i gotta write that back into the file somehow. Tedious. i could just replace the entire line itself heh.
 
Big thank you to japadie..invaluable help, i had actualy done exactly what you pointed out earlier but i rejected the script thinking it was incorrect, could you believe it ? all that time all that was WRONG with the script was a missing closing '}' ...

comes to show, ALWAYS put them in first before you begin writing ANY code. sure taught me a lesson. :)
 
some decent code editors (like Aptana IDE) will flag unterminated bracketed expressions to you, making it dead easy to debug this kind of thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top