Jump to content

Some Complicated Theory...part two


phatgreenbuds

Recommended Posts

Picking up where I last left off...in the code below I have a simple form with 4 seperate selectable boxes. Each is numbered box1 thru box4 and are submitted as normal.

 

<body>
<form  action="../esv/testy.php"  method="POST">
            
        <select name="box1">
          <option value=""></option>
          <option value="a1">a1</option>
          <option value="a2">a2</option>
          <option value="a3">a3</option>
        </select>
        
        <select name="box2">
          <option value=""></option>
          <option value="b1">b1</option>
          <option value="b2">b2</option>
          <option value="b3">b3</option>
        </select>
        
        <select name="box3">
          <option value=""></option>
          <option value="c1">c1</option>
          <option value="c2">c2</option>
          <option value="c3">c3</option>
        </select>
        
        <select name="box4">
          <option value=""></option>
          <option value="d1">d1</option>
          <option value="d2">d2</option>
          <option value="d3">d3</option>
        </select>
        
<input name="Submit" type="submit" id="Submit" value="Submit">

</form>
</body>

 

Now I know that the $_POST[] is really just an array that the values being posted are stored in but when you are looking to pull them back out how would you do so with efficient means? Also how would you account for a box not be selected? I was thinking something like this:

 

<body>
<?php

if (isset($_POST[box])) {

for ($i = 1; $i <= 4; $i++) {
 echo $_POST[box][$i];
}
}

?>
</body>

 

But of course that did not work so I am back to reading and asking for help.

 

FYI I am still somewhat new to this and my course of learning is to try and reproduce parts of what I use at work and sites I may chance upon. Probably not the best way to learn but it has worked for me so far.

Link to comment
https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/
Share on other sites

it will never work because $_POST[box] is not set

 

your posts are box1, box2, box3, and box4

 

instead of labeling them box1, box2, box3, and box4

just use box[] for all for of them and it will set them automatically

 

$_POST[box] will be an array

also as a sidenote instead of using a for statement i would use a while statement

 

while($box=$_POST['box']){

echo $box;

}

ok cool...I did not know that you could do that with the labels.

 

The reason I chose to use the "for" loop is to key the eventual entry into the database.  So for instance if box 2 and 4 are passed with entries then I want the selection entered into a table with an ID of 2 and 4 repectively. I have yet to figure that part out...still taking baby steps.

 

But thats the reason for my second issue if having to account for boxes that have nothing selected.

Here is how I solved this...please feel free to critique. It does work as posted here but reading the above posts I see that I could have done this differently now.

 

testx.php

<body>
<form  action="../esv/testy.php"  method="POST">
            
        <select name="box[]">
          <option value=""></option>
          <option value="a1">a1</option>
          <option value="a2">a2</option>
          <option value="a3">a3</option>
        </select>
        
        <select name="box[]">
          <option value=""></option>
          <option value="b1">b1</option>
          <option value="b2">b2</option>
          <option value="b3">b3</option>
        </select>
        
        <select name="box[]">
          <option value=""></option>
          <option value="c1">c1</option>
          <option value="c2">c2</option>
          <option value="c3">c3</option>
        </select>
        
        <select name="box[]">
          <option value=""></option>
          <option value="d1">d1</option>
          <option value="d2">d2</option>
          <option value="d3">d3</option>
        </select>
        
<input name="Submit" type="submit" id="Submit" value="Submit">

</form>
</body>

 

 

testy.php

<body>
<?php

if (isset($_POST[box])) {

for ($i = 0; $i < 4; $i++) { 
 $out = $_POST[box][$i];
	if ($_POST[box][$i] == "") {
		$out = "nothing";
			} 
	echo $i+1 . ": " . $out; 
 	echo "<br />"; 
}
}

?>

 

Ultimately this will allow me to do the DB insert as I want it to work. I am still going to play for a bit on this and try the recommendations above.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.