Jump to content

Question with php loop. beginner please help


patheticsam

Recommended Posts

Hi,

 

I am currently working on an html form wich loads somes values into $_SESSIONS values (here's the code for the html form)

 

<form name="change" action="../secure/" method="post">
	<div class="FBG2">
		<h2>Services de telephonie</h2>
		<span class="division"><input type="checkbox" name="T01"><p>Videotron téléphonie</p></span>
		<span class="division"><input type="checkbox" name="T02"><p>Bell Canada</p></span>
		<span class="division"><input type="checkbox" name="T03"><p>Bell Mobilité</p></span>
		<span class="division"><input type="checkbox" name="T04"><p>Fido</p></span>			
			<div class="clr"></div>
		<span class="division"><input type="checkbox" name="T05"><p>Rogers sans-fil</p></span>
		<span class="division"><input type="checkbox" name="T06"><p>Telus</p></span>
		<span class="division"><input type="checkbox" name="T07"><p>Telus mobilité</p></span>
		<span class="division"><input type="checkbox" name="T08"><p>Allstream</p></span>			
			<div class="clr"></div>
	</div>
              </form>

 

i'm passing the values into $_SESSION values:

 

<?php
session_start();

$_SESSION['T01'] = $_POST['T01'];
$_SESSION['T02'] = $_POST['T02'];
$_SESSION['T03'] = $_POST['T03'];
$_SESSION['T04'] = $_POST['T04'];
$_SESSION['T05'] = $_POST['T05'];
$_SESSION['T06'] = $_POST['T06'];
$_SESSION['T07'] = $_POST['T07'];
$_SESSION['T08'] = $_POST['T08'];

 

Now since the forms contains only checkboxes some of the values are left empty..How can I loop through the values and eliminate the ones that are left empty and output the ones that are selected?

 

I'm new to php..so if some one can point me to a tutorial or command it would be really appreciated.

 

Thanks!

 

 

Link to comment
Share on other sites

You can assign only the values that are present in a loop easily enough, if that's what you're trying to do.

 

for( $i = 1; $i < 9; $i++ ) {
       if( isset($_POST["T0{$i}"]) {
              $_SESSION["T0{$i}"] = $_POST["T0{$i}"];
       }
}

Link to comment
Share on other sites

First off your form, doesn't have a submit button, so I"m unclear how you are posting it.

 

With that said, if you have a form that has a checkbox and you submit it with no checkbox checked, there will be nothing in the $_POST.  So you need to know what the entire universe of checkboxes is, which you of course do know because you are providing the markup.

 

What I'd suggest is that you name your groups of checkboxes with the same name, and use the array syntax.

 


...

....

 

When you use this technique, your $_POST will include an array named services, so long as at least one checkbox is checked. 

 

This array will contain the VALUES for any of the checkboxes you provided.  You can then set that to be a single session variable and re-read the values from that variable depending on what you want to do.

 

See this sample program for clarity:

 

 

#testform.php
</pre>
<form name="test" method="post">

T01

T02

</form>
<br><br>var_dump($_POST);<br>?&g

 

Check both boxes and submit and the output is:

 

array(1) { ["services"]=> array(2) { [0]=> string(3) "T01" [1]=> string(3) "T01" } }

 

Link to comment
Share on other sites

You can assign only the values that are present in a loop easily enough, if that's what you're trying to do.

 

for( $i = 1; $i < 9; $i++ ) {
       if( isset($_POST["T0{$i}"]) ) {
              $_SESSION["T0{$i}"] = $_POST["T0{$i}"];
       }
}

 

This is exactly what I'm trying to do...The values are assigned correctly but how do I know which ones have been assigned? Thanks!

Link to comment
Share on other sites

I'll look into this..thanks!!

 

Did you try out the code I provided as a sample?  Unfortunately the format was a bit screwed up on some of the code tags but I fixed that.  My sample was written to reflect your code and solve your problem.  Of course if you don't know what a php array is, I guess anything in this regard is going to be confusing.  Using the technique I demonstrated, you will get one array (again as long as at least one checkbox is checked) and this array will have values for any of the checkboxes.  You can then assign this variable as is to a session variable, or use it to update a database.

 

 

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.