Jump to content

[SOLVED] selects and input texts together


steveh62

Recommended Posts

I have the following in a form...a select box and a text input box per line.

 

each checkbox is really associated to a textbox - thats what I need really...anyway what I do need is to pull the data from both the checkbox array and each text input, so each matches up to its partner...basically its a size price relationship whereby I can apply a unique price to a size.

regards

steve

 

<label>200mm x 200mm</label><input name="sizes[]" type="checkbox" value="200 x 200" checked="true"><input name="" type="text" class='dropdown'><br/>

<label>200mm x 300mm</label><input name="sizes[]" type="checkbox" value="200 x 300" ><input name="sprice[]" type="text" class='dropdown'><br/>

<label>200mm x 400mm</label><input name="sizes[]" type="checkbox" value="200 x 400" ><input name="sprice[]" type="text" class='dropdown'><br/>

<label>200mm x 500mm</label><input name="sizes[]" type="checkbox" value="200 x 500" ><input name="sprice[]" type="text" class='dropdown'><br/>

<label>200mm x 600mm</label><input name="sizes[]" type="checkbox" value="200 x 600" ><input name="sprice[]" type="text" class='dropdown'>

 

Link to comment
Share on other sites

I'm not sure about this, but can you not use:

 

<label>200mm x 200mm</label><input name="sizes['size']" type="checkbox" value="200 x 200" checked="true"><input name="sizes['sprice']" type="text" class='dropdown'><br/>

 

Never tried so..

 

Adam

Link to comment
Share on other sites

cheers for an answer at least...but I need it as is as it is going to poulate to fields in mysql size and price (price can be variable thats why its empty)...I am using them to apply same size ranges but a price can be different (thats based on an image I am applying later)

rgds

steve

<label>200mm x 200mm</label><input name="sizes[]" type="checkbox" value="200 x 200" checked="true"><input name="" type="text" class='dropdown'><br/>
<label>200mm x 300mm</label><input name="sizes[]" type="checkbox" value="200 x 300" ><input name="sprice[]" type="text" class='dropdown'><br/>
<label>200mm x 400mm</label><input name="sizes[]" type="checkbox" value="200 x 400" ><input name="sprice[]" type="text" class='dropdown'><br/>
<label>200mm x 500mm</label><input name="sizes[]" type="checkbox" value="200 x 500" ><input name="sprice[]" type="text" class='dropdown'><br/>
<label>200mm x 600mm</label><input name="sizes[]" type="checkbox" value="200 x 600" ><input name="sprice[]" type="text" class='dropdown'>

Link to comment
Share on other sites

sorry adam, only just got you there...your making the relationship in the name tags yes?

 

How would I pull the data out using a for loop...see the code

 

<?php
	$foo = $_POST['sizes'];
	$sprice = $_POST['sprice'];
	echo £sprice;
	if (count($foo) > 0) {
	// loop through the array
	for ($i=0;$i<count($foo,);$i++) {//now insert sizes in table spcanvas_sizes
	echo $sprice;
mysql_query( "INSERT INTO spcanvas_sizes(`sizes`, `pid`) VALUES('".$foo[$i]."', '".$id."')" );
	// do something - this can be a SQL query,
	// echoing data to the browser, or whatever
	//echo "<li>$foo[$i] \n";

	}  //end for loop

	}  //end if
?>

Link to comment
Share on other sites

Checkboxes that are not checked are not included in the post data. So using a numerically indexed array as you have will not work because if the user does not check the 1st checkbox, then the 2nd checkbox (if checked) would be associated with the 1st text field. Can you not create a loop when creatingyour fields so you can specify an index:

 

for ($i=0; $i<$max_fields; $i++)
{
    echo "<label>200mm x 200mm</label>";
    echo "<input name=\"sizes[$i]\" type=\"checkbox\" value=\"200 x 200\" checked=\"true\">";
    echo "<input name=\"sprice[$i]\" type=\"text\" class=\"dropdown\"><br/>";
}

Link to comment
Share on other sites

Basically he's saying when a checkbox isn't checked, it doesn't return a value. So if you were to do that you may end up with two arrays like:

 

true | some text value

true | some other text value

true | another text value

      | yet another text value

      | maybe another text value

 

Where say the first, third and fifth checkboxes were checked, if you follow?

 

He's saying if you loop through the checkboxes and have a counter increment on every loop, you can then set the index for both arrays, for each input. You could do it manually if it weren't so easy to loop through the inputs. but it would mean you'd end up with an array like:

 

(0) true | (0) some text value

(1)      | (1) some other text value

(2) true | (2) another text value

(3)      | (3) yet another text value

(4) true | (4) maybe another text value

 

If that makes more sense? Had to do it with brackets btw cause it went funny with [] ..

 

Adam

 

 

Link to comment
Share on other sites

$labels = array(
            0 => '200mm x 200mm',
            1 => '200mm x 300mm',
            2 => '200mm x 400mm',
            3 => '200mm x 500mm',
            4 => '200mm x 600mm',
        );

foreach ($labels as $key => $label) {
    print '<label>' .$label. '</label><input name="sizes[' .$key. ']" type="checkbox" value="' . $label. '"';
    $key == 0 ? print ' checked';
    print ' /><input name="sprice[' .$key. ']" type="text" class='dropdown' /><br/>';
}

 

Might need a few adjustments..

Link to comment
Share on other sites

got it thanks...the error was to do with $key == 0 ? bit and also amended the class to class="dropdown"

 

so I changed it a tad to the following:

 

BUT thanks a million works now :D :D

 

<?php

$labels = array(
            0 => '200mm x 200mm',
            1 => '200mm x 300mm',
            2 => '200mm x 400mm',
            3 => '200mm x 500mm',
            4 => '200mm x 600mm',
        );

foreach ($labels as $key => $label) {
    print '<label>' .$label. '</label><input name="sizes[' .$key. ']" type="checkbox" value="' . $label. '"';
    if ($key == 0){
  print "checked";
 }
    print ' /><input name="sprice[' .$key. ']" type="text" class="dropdown" /><br/>';

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.