Jump to content

Recommended Posts

why dosent this work with form array please

 

<?php

if(isset($_POST['submit'])){

for($i=0; $i<count($a[]); $i++){

	echo $a[$i];
}

}

?>



<form method="POST" action="form.php">

<br>
<input type="text" name="a[]">  
<br>

<br>
<input type="text" name="a[]" > 
<br>


<br>
<input type="text" name="a[]" >
<br>

<br>

<input type="submit" name="submit" value="SEND!">

</form>

Link to comment
https://forums.phpfreaks.com/topic/126716-solved-form-help-cheers-array-probs/
Share on other sites

when using dealing with arrays you want to use foreach loops as they are more effective at moving through arrays but your code should be like this

<?php
if(isset($_POST['submit'])){
foreach($_POST['a'] as $b){
	echo $b;
}
}
?>

 

Scott.

only the ecohed session should be echoed but i can echo them all....

 

im try to set sessions from a arrayed form....

 

 

the form is at the first post set to name a[]

 

<?php session_start();

if(isset($_POST['submit'])){


foreach($_POST['a'] as $b){

       $c=explode(' ',$b);

	$_SESSION['a']="$c[0]";

	$_SESSION['b']="$c[1]";

	$_SESSION['c']="$c[2]";

	echo $_SESSION['a'];

}

}

?>

here the full code how do you brake the array down to get the sessions to

be indevgally used.............

 

 

when i echo the session out from the posted form the one ecohed session

dispalys all the ordered array from the form

 

<?php session_start();

if(isset($_POST['submit'])){


foreach($_POST['a'] as $b){

       $c=explode(' ',$b);

	$_SESSION['a']="$c[0]";

	$_SESSION['b']="$c[1]";

	$_SESSION['c']="$c[2]";

	echo $_SESSION['a']; //this should be set to this >>$_SESSION['a']="$c[0]"; only but echo them all......

}

}

?>



<form method="POST" action="form.php">

<br>
<input type="text" name="a[]">  
<br>

<br>
<input type="text" name="a[]" > 
<br>


<br>
<input type="text" name="a[]" >
<br>

<br>

<input type="submit" name="submit" value="SEND!">

</form>

 

why can i not set the session via posting from a form...................

 

simple example.................

 

<?php session_start();

if(isset($_POST['submit'])){

   
    $_SESSION['a']=$_POST['cat'];

	$_SESSION['b']=$_POST['dog'];

	$_SESSION['c']=$_POST['pig'];

	echo $_SESSION['a'];

}


?>



<form method="POST" action="form.php">

<br>
<input type="text" name="pig">  
<br>

<br>
<input type="text" name="dog" > 
<br>


<br>
<input type="text" name="cat" >
<br>

<br>

<input type="submit" name="submit" value="SEND!">

</form>

non array way working need the same way with arrays.......

<?php session_start();

if(isset($_POST['submit'])){

$cat=$_POST['cat'];

$dog=$_POST['dog'];

$pig=$_POST['pig'];
   
    $_SESSION['a']=$cat;

	$_SESSION['b']=$dog;

	$_SESSION['c']=$pig;

	echo"  <br> {$_SESSION['a']} <br> {$_SESSION['b']} <br> {$_SESSION['c']} ";

}


?>



<form method="POST" action="form.php">

<br>
<input type="text" name="pig">  
<br>

<br>
<input type="text" name="dog" > 
<br>


<br>
<input type="text" name="cat" >
<br>

<br>

<input type="submit" name="submit" value="SEND!">

</form>

look above wrong lol

 

the array way..........

<?php session_start();

if(isset($_POST['submit'])){

foreach($_POST['a'] as $b){
   
    $_SESSION['a']=$b[0];

	$_SESSION['b']=$b[1];

	$_SESSION['c']=$b[2];

	echo"  <br> {$_SESSION['a']} <br> {$_SESSION['b']} <br> {$_SESSION['c']} ";

}
}

?>



<form method="POST" action="form.php">

<br>
<input type="text" name="a[]">  
<br>

<br>
<input type="text" name="a[]" > 
<br>


<br>
<input type="text" name="a[]" >
<br>

<br>

<input type="submit" name="submit" value="SEND!">

</form>

corrected forgot explode........

 

 

array way to set sessions via a form......

<?php session_start();

if(isset($_POST['submit'])){

        foreach($_POST['a'] as $b){

	$b=explode( ' ', $b);
   
        $_SESSION['a']="$b[0]";

	$_SESSION['b']="$b[1]";

	$_SESSION['c']="$b[2]";

	echo"  <br> {$_SESSION['a']} <br> {$_SESSION['b']} <br> {$_SESSION['c']} ";

}
}

?>



<form method="POST" action="form.php">

<br>
<input type="text" name="a[]">  
<br>

<br>
<input type="text" name="a[]" > 
<br>


<br>
<input type="text" name="a[]" >
<br>

<br>

<input type="submit" name="submit" value="SEND!">

</form>

 

 

non array way to set sessions via a form......

 

<?php session_start();

if(isset($_POST['submit'])){

$cat=$_POST['cat'];

$dog=$_POST['dog'];

$pig=$_POST['pig'];
   
    $_SESSION['a']=$cat;

	$_SESSION['b']=$dog;

	$_SESSION['c']=$pig;

	echo"  <br> {$_SESSION['a']} <br> {$_SESSION['b']} <br> {$_SESSION['c']} ";

}


?>



<form method="POST" action="form.php">

<br>
<input type="text" name="pig">  
<br>

<br>
<input type="text" name="dog" > 
<br>


<br>
<input type="text" name="cat" >
<br>

<br>

<input type="submit" name="submit" value="SEND!">

</form>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.