Multiple Checkboxes and multiple forms

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

weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Multiple Checkboxes and multiple forms

Post by weblearner »

Hi all, am still struggling through the holidays trying t figure out how to solve this...
I have two pages, 'result.php' which generates a dynamic table, 'rfq.php' which is a form
when user click on the checkboxes in 'result.php and submit, it should pass the checkboxes values into the text fields in 'rfq.php'
if there are two checkboxes ticked, the two checkboxes values will fill up two of the text fields in 'rfq.php'
I need to pass two values from the row results 'MPN', "MFR' into the text fields 'partsnumber', 'manufacturer' respectively.
if there are two rows, then into 'partsnumber2', manufacturer2' etc.
the codes are as below:


Results.php

Code: Select all

<form id="form1" name="form1" method="post" action="rfq.php">
            <input name="hidSubmit" type="hidden" id="hidSubmit2" value="true">
            <table width="100%" border="0" cellpadding="5" cellspacing="1">
              <tr class="maintxt">
                <th align="left" bgcolor="#CCCCCC">RFQ</th>
                <?php if ($totalRows_rsPartsSearch > 0) { // Show if recordset not empty ?>
                <th align="left" bgcolor="#CCCCCC">Parts Number</th>
                <th align="left" bgcolor="#CCCCCC">Manufacturer</th>
                <th align="right" bgcolor="#CCCCCC">Quantity</th>
                <?php } // Show if recordset not empty ?>
              </tr>
              <?php do { ?>
              <tr bgcolor="#C6ECFF" class="maintxt">
                <td width="194" bgcolor="#bdc5c9"><input name="mpnrfq[]" type="checkbox" id="mpnrfq" value="<?php echo $row_rsPartsSearch['MPN']; ?>" /></td>
                <td width="194" nowrap="nowrap" bgcolor="#bdc5c9"><?php echo $row_rsPartsSearch['MPN']; ?></td>
                <td width="152" bgcolor="#bdc5c9"><?php echo $row_rsPartsSearch['MFR']; ?></td>
                <td width="135" align="right" bgcolor="#bdc5c9"><?php echo $row_rsPartsSearch['TOTAL']; ?></td>
              </tr>
              <?php } while ($row_rsPartsSearch = mysql_fetch_assoc($rsPartsSearch)); ?>
            </table>
            <input type="submit" name="submitRFQ" id="submitRFQ" value="Submit" />
          </form>

[u]Rfq.php[/u]

<table width="100%" border="0" cellspacing="1" cellpadding="0">
                  <tr>
                    <td width="24%"><span class="maintxt">Parts Number</span></td>
                    <td width="18%"><span class="maintxt">Manufacturer</span></td>
                    <td width="11%"><span class="maintxt">Quantity</span></td>
                    <td width="16%" class="maintxt">Target Price</td>
                    <td width="31%" class="maintxt">Required Delivery Date</td>
                    </tr>
                  <tr>
                    <td><span id="sprypartnumber">
                      <input name="partsnumber" type="text" id="partsnumber" size="20" />
                      <span class="textfieldRequiredMsg">Required.</span></span></td>
                    <td><span id="sprymanufacturer">
                      <input name="manufacturer" type="text" id="manufacturer" size="15" />
                      <span class="textfieldRequiredMsg">Required.</span></span></td>
                    <td><span id="spryquantity">
                    <input name="quantity" type="text" id="quantity" size="6" />
                    <span class="textfieldRequiredMsg"> Required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
                    <td><input name="targetprice" type="text" id="targetprice" size="6" /></td>
                    <td><input name="deliverydate" type="text" id="deliverydate" size="10" /></td>
                    </tr>
                  <tr>
                    <td><input name="partsnumber2" type="text" id="partsnumber2" size="20" /></td>
                    <td><input name="manufacturer2" type="text" id="manufacturer2" size="15" /></td>
                    <td><input name="quantity2" type="text" id="quantity2" size="6" /></td>
                    <td><input name="targetprice2" type="text" id="targetprice2" size="6" /></td>
                    <td><input name="deliverydate2" type="text" id="deliverydate2" size="10" /></td>
                    </tr>
                  <tr>
                    <td><input name="partsnumber3" type="text" id="partsnumber3" size="20" /></td>
                    <td><input name="manufacturer3" type="text" id="manufacturer3" size="15" /></td>
                    <td><input name="quantity3" type="text" id="quantity3" size="6" /></td>
                    <td><input name="targetprice3" type="text" id="targetprice3" size="6" /></td>
                    <td><input name="deliverydate3" type="text" id="deliverydate3" size="10" /></td>
                    </tr>
                  </table>
I'd tried to recreate a test page using the code below but can't get the results:

Code: Select all

<form id="form1" name="form1" method="post" action="">
  <p>
    <?php
	$strPartsID=$_REQUEST['mpnrfq'];
	if(isset($hidSubmit)){
	$count=count($mpnrfq);
	for($i=0;$i<$count;$i++){$strPartsID="$strPartsID$mpnrfq[$i], ";}
	} 
	?>
    <?php
    foreach ($strPartsID as $data) 
	?>
// <?php
// $partsid = $_REQUEST['mpnrfq[]'];
// ?>
    <input name="partsid" type="text" id="partsid" value="<?php print $data; ?>" />
  </p>
  <p>
    <input name="partsid2" type="text" id="partsid2" value="<?php echo $strPartsID[$i++]; ?>" />
  </p>
</form>
Would really appreciate if you guys could point out my mistakes and how i can improve on the codes.
Many thanks and happy holidays :)
Last edited by Benjamin on Thu Dec 30, 2010 1:30 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Multiple Checkboxes and multiple forms

Post by anantha »

when you are submitting the form in results.php to the Rfq.php...which you are doing now....you will get $_POST values in the Rfq.php....so if you var_dump($_POST) in Rfq.php...you should see all the post values...so depending on the value you need you can pass that value to the text field in Rfq.php....
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

hi anantha
thanks for replying, let me read up and try the var_dump() thingy cos i'm clueless about it still...
pardon me and hope you wouldn't mind should i need your help again ya..:)
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

Hi anantha
had tried the var_dump and this is the results:
array(2) { ["mpnrfq"]=> array(3) { [0]=> string(27) "MX29LV040CQI-70G, Macronix " [1]=> string(28) "MX29LV040CTC-90G , Macronix " [2]=> string(23) "JS28F640J3D75A, Numonyx" } ["submitRFQ"]=> string(6) "Submit" }

would appreciate if you could advise me on how could i pass this results into the respective form fields? thanks.
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Multiple Checkboxes and multiple forms

Post by anantha »

i have no idea what you are trying to accomplish...but i can see from your var_dump...that you have two array mpnrfq and submitRFQ....but i think submitRFQ will not be that much useful for you....if you see in the mpnrfq array...it has 3 index mpnrfq[0],mpnrfq[1],mpnrfq[2]...so you can pass these three array values to the respective input value field.....
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

hi anantha

thanks for replying...i'm trying to post the values from mpnrfq[0] 'MX29LV040CQI-70G', 'Macronix' into two form fields 'partsnumber' and 'manufacturer', followed by the values of mpnrfq[1] into form fields 'partsnumber2' and 'manufacturer2', lastly the values of mpnrfq[2] into fields 'partsnumber3' and 'manufacturer3'...would appreciate if you could give me some pointers on the codes..many thanks.
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Multiple Checkboxes and multiple forms

Post by spedula »

I'm not expert so I'm not 100% sure about this, but it's worked for me plenty of times in the past.

If you pass a value from one page to another with the name identified as an array, mpnrfq[], then the page that gets this data posted to it will auto create "mpnrfq" as an array. So if you do:

print mpnrfq[0];

then you should get the value stored there.

If thats not the case, then you can:

print $_POST[mpnrfq[0]];

Hope this helps somewhat.
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

hi spedula

many thanks for helping...i had wrote the following code in the rfq.php file which is the page that is receiving the posted data:

Code: Select all

<?php 
print mpnrfq[0];
?>
however, i'd got the following error:
Parse error: parse error in C:\wamp\www\testsite\rfq.php on line 2

had tried the 'print $_POST[mpnrfq[0]];', still got the same error...would you mind point out my mistake? thanks.
Last edited by Benjamin on Fri Jan 07, 2011 2:37 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

hi spedula

really appreciate your reply and help...
here's the code i'd amended:

Code: Select all

<?php 
$x = $_POST['mpnrfq'];
?>

<input type="text" name="partsnumber" id="partsnumber" value=<?php echo $x[0]; ?>/>
<input type="text" name="manufacturer" id="manufacturer" />
<input type="text" name="partsnumber2" id="partsnumber2" value=<?php echo $x[1]; ?>/>
<input type="text" name="manufacturer2" id="manufacturer2" />
<input type="text" name="partsnumber3" id="partsnumber3" value=<?php echo $x[2]; ?>/>
<input type="text" name="manufacturer3" id="manufacturer3" />
however the results is:

Code: Select all

    <input type="text" name="partsnumber" id="partsnumber" value=A3977SLPTR-T, Allegro/>
    <input type="text" name="manufacturer" id="manufacturer" />
    <input type="text" name="partsnumber2" id="partsnumber2" value=JS28F640J3D75A, Numonyx/>
    <input type="text" name="manufacturer2" id="manufacturer2" />
    <input type="text" name="partsnumber3" id="partsnumber3" value=<br />
<b>Notice</b>:  Undefined offset: 2 in <b>C:\wamp\www\testsite\rfq.php</b> on line <b>16</b><br />
/>
    <input type="text" name="manufacturer3" id="manufacturer3" />
how do i put the value 'Allegro' into the field 'manufacturer' and 'Numonyx' into the field 'manufacturer2' respectively?
if the user only checked on two options and submit the results, how do i not show the error of the undefined offset?
i understand that if user checked on three options and submit the results, the undefined offset error will not appear right?
please pardon my limited skill and i really appreciate your pointers...many thanks.
Last edited by Benjamin on Fri Jan 07, 2011 2:38 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Multiple Checkboxes and multiple forms

Post by spedula »

Okay. I'm not exactly sure what type of application you're building here but I can help you out here.

The way I see it, you need to have 2 inputs for each value of $x, partsnumber and manufacturer. The best way to do this is to write a loop and echo's the fields for you depending on how many you actually need, whether its 1 or 10,000.

Doing this is always a better idea, this way you won't have extra fields in your HTML that don't need to be there.

Example, you wrote 3 sets of fields but the user only submitted 2 pieces of information from the previous page. The second page will throw you an unidentified offset value error. Meaning you tried to access a value in an array that was not defined. In this case, it was $x[2].

So, getting back to the loop.

First we need to find out how many pieces of information the user submitted. We can do this by counting how many offsets are set in the array.

Code: Select all

$x = $_POST['mpnrfq'];
$xCount = count($x);
Lets say the user submitted 2 checkboxs, so $xCount will = 2.

Now lets create the loop.

Code: Select all

for ($i = 0; $i < $xCount; $i++) {
   // code to be executed
}
If you're not familiar with loops, here's what the above does. First, it sets the var $i to equal 0. Then it sets an argument to check, in this case, if $i is less than $xCount then execute the loop and the last part is to increase $i by 1 after the code executes.

Now, we can write up the actual code that will create your fields.

Code: Select all

for ($i = 0; $i < $xCount; $i++) {
   echo '<input type="text" name="partsnumber" id="partsnumber" value='".$x[$i]."'>';
   echo '<input type="text" name="manufacturer" id="manufacturer" />';
}
If you noticed the value attribute is $x[$i]. Since this is a loop where $i is set to 0 the first time it executes then the $i is replaced with its respective value, 0.So it will insert the value stored in that offset of the $x array.

Now I noticed that you append a number to each new input field. You can do this by replacing

"partsnumber" with
"partsnumber'.($i+1).'"

With the quotes intact.

As for your question about how to put allegro into the manufacturer field.

I can see that the format of the data you're passing looks like this "A3977SLPTR-T, Allegro"

So you can use the PHP function explode() since it's separated with a comma+whitespace.

Look it up in PHP.net and see if you can figure it out for yourself. :wink:
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

hi spedula

am really grateful for your pointers and codes...i'd managed to get the values into the respective text fields...but am puzzled on the below...had read through the explode(), some trial & error...had amended the code as follows:

Code: Select all

$x = $_POST['mpnrfq'];
foreach($x as $item){ 
$subarray[] = explode(" ", $item); 
} 
print_r($subarray);

$subarrayCount = count($subarray);
for ($i = 0; $i < $subarrayCount; $i++) {
echo '<input name="partsnumber'.($i+1).'" type="text" id="partsnumber" size="20" value='.$subarray[$i][0].'/>
echo '<input name="manufacturer'.($i+1).'" type="text" id="manufacturer" size="15" value='.$subarray[$i][1].'/>

however, the print_r shows this:

Array
(
[0] => Array
(
[0] => MX29LV040CTC-90G
[1] => 
[2] => Macronix <------- how come this is not at [1]
[3] => 
)

[1] => Array
(
[0] => KSZ8995MA
[1] => 
[2] => Micrel <------- how come this is not at [1]
)

[2] => Array
(
[0] => FM18L08-70-SG
[1] => Ramtron
)

)

the input source from the page that will post the data is:

Code: Select all

<input name="mpnrfq[]" type="checkbox" id="mpnrfq" value="<?php echo $row_rsPartsSearch['MPN']; ?> <?php echo $row_rsPartsSearch['MFR']; ?>" />

pulling my hair...omg...where did i go wrong again
sorry to bother again
Last edited by Benjamin on Fri Jan 07, 2011 2:38 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Multiple Checkboxes and multiple forms

Post by spedula »

First off, in your input source you don't have to open and close PHP twice.

Instead just concatenate the two statements like this:

Code: Select all

<?php echo $row_rsPartsSearch['MPN'].$row_rsPartsSearch['MFR']; ?>
A period between two variables concatenates them, it just looks nicer and is good practice.

Now to the actual issue. I can't tell what those arrays in the input are actually echoing, so take a look at the data being passed into the explode function, its in a previous post.

"A3977SLPTR-T, Allegro"

You'll notice that the two values are being separated with a comma and a space. Your explode function only accounts for the white space. Take a closer look there.

The dump of the $subarray shows that the array in $subarray[0] contains 4 offsets while the array in $subarray[1] has 3. The extra offsets are blank, or whitespace. Meaning that you need to, again, take a closer look at the data coming into the explode() function.

Make sure that the data coming in is consistently formatted. I can't stress this enough, consistency. Otherwise you'll just get garbage. :D
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

hi spedula

hope you'd a good weekend..and yes, as you'd pointed out..i'd fixed the codes and it's working properly..really appreciated your help.
i'm now finding out how to create a webpage to upload a excel file which will replace the data into a mysql table....could you advise me where i should be heading? thanks.
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Multiple Checkboxes and multiple forms

Post by spedula »

I've never really had a need to do that, but right off the top of the head...

Excel has a feature to output the data into CSV (comma separated value) files. Uploading this file will be easier to parse the data.

Now, is the mySQL database schema already setup? Meaning, Is the table already created with fields?

If not, then the script will have create the table and fields before inserting the data.

Study what the CSV files will look like and then just start working on the parser script. You'll get it eventually through trial and error. If not, ask a question here. :)
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Multiple Checkboxes and multiple forms

Post by weblearner »

hi spedula

had more or less figured out and done with the uploader...

there's one other thing which i hope if you could advise me on how to go about...
had put it in visual...
Image

how do i get the checkbox value to display the image as shown? thanks and have a good weekend :?
Post Reply