Jump to content

i cannot post a table in a form (only last td)


faure

Recommended Posts

 

like original message here 2 files php

post is not working well, only last tr

thank you for your help

 

<?php

// //////////////////////////////
// first file bz91.php
// /////////////////////////////

session_start(); 

?>
 
 

<!DOCTYPE HTML>


<html>


<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<title>bz91</title>

<style>

.zone1{

width : 800px;
height : 400px;
margin: 0 auto;

}

    
</style>
          
</head>

<body>

<div class="zone1">
<center>
<div class="w3-display-container w3-lime w3-section w3-round-xlarge" style="height:400px;" style="width:50%" ;>

<form action="bz92.php" id="ID1" method="POST" accept-charset="utf-8">


<div class="w3-display-position w3-white" style="top:20px;" >    
<table  id="t01" name="t01" BGCOLOR="#98FB98"  align= center  >


<?php    

$_SESSION[ 'lib'."1"]="Npm";

$_SESSION[ 'lib'."2"]="Prénom";
    
$_SESSION[ 'lib'."3"]="Début";
$_SESSION[ 'lib'."4"]="Fin";


$_SESSION[ "1"]="A";

$_SESSION[ "2"]="B";
    
$_SESSION[ "3"]="C";
$_SESSION[ "4"]="D";


// ////////////////////
// loop
// ///////////////////

for ($i = 1; $i <= 4; $i++)

{

?>
    
    
<tr height=30>

<td> <input type= "label"   name = 'one'.$i  value= <?php echo $_SESSION[ "lib".$i]  ?>  > </td>

<td> <input type= "text"   name = 'two'.$i  value= <?php echo $_SESSION[ $i]  ?>  > </td>

<?php

}

?>    

</table>
</div>

<div class="w3-display-position w3-white" style="top:330px;left:20px">    
<input  type="submit"  name = "valider" value="OK">
</div>


</form>

</div>
</center>    
</div>        

<p> Click on OK for validation</p>    

        
</body>
        
</html>

Link to comment
Share on other sites

 

 

 

<?php
// ///////////////////////
// second file bz92.php
// //////////////////////


session_start(); 


 ?>


 

<!DOCTYPE HTML>


<html>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


<title>bz92</title>


<style>
</style>
</head>

<body>

        
</body>

<?php
 
echo "POST ";
echo"<br>";    

print_r($_POST); 

echo"<br>";    


if(isset($_POST['valider']))


{

echo "<br>";

// boucle


$tableau = array();
// init

    

$n=0;

foreach($_POST as $index=>$valeur)
    
    {
    
    $n= $n+1;    
    
    echo "valeur ".$valeur." // ";
    echo "<br>";

    
    }
    

}

echo "<br>";

echo "nombre ".$n;


// header('Location: bz5.php');

exit();

 ?>

        
        
        
        
</html>

Link to comment
Share on other sites

Each of the inputs returned from server as html are as

<td> <input type= "label"   name = 'one'.$i  value= Npm  > </td>
<td> <input type= "text"   name = 'two'.$i  value= A  > </td>


<tr height=30>
<td> <input type= "label"   name = 'one'.$i  value= Prénom  > </td>
<td> <input type= "text"   name = 'two'.$i  value= B  > </td>

	
<tr height=30>
<td> <input type= "label"   name = 'one'.$i  value= Début  > </td>
<td> <input type= "text"   name = 'two'.$i  value= C  > </td>
	
	
<tr height=30>
<td> <input type= "label"   name = 'one'.$i  value= Fin  > </td>
<td> <input type= "text"   name = 'two'.$i  value= D  > </td>

As you can see the name attribute value although wrong is identical, so each will overwrite the previous till you are left with the last.

Note: missing closing 'tr' tags, and there is no such input type as 'label'

Link to comment
Share on other sites

You could do it like this and instead giving each input a different name attribute value, make it an array of name attribute values

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
        <title>bz91</title>
        <style>
            .zone1{
                width : 800px;
                height : 400px;
                margin: 0 auto;
            }
            #t01 > tr {height: 30px;}
        </style>
    </head>
    <body>
        <div class="zone1">
            <div class="w3-display-container w3-lime w3-section w3-round-xlarge" style="height:400px; /*width:50%*/">
                <form action="bz92.php" id="ID1" method="POST" accept-charset="utf-8">
                    <div class="w3-display-position w3-white" style="top:20px;">
                        <table  id="t01" BGCOLOR="#98FB98"  align="center">
                            <?php
                            $_SESSION['lib' . "1"] = "Npm";
                            $_SESSION['lib' . "2"] = "Prénom";
                            $_SESSION['lib' . "3"] = "Début";
                            $_SESSION['lib' . "4"] = "Fin";

                            $_SESSION["1"] = "A";
                            $_SESSION["2"] = "B";
                            $_SESSION["3"] = "C";
                            $_SESSION["4"] = "D";

                            for ($i = 1; $i <= 4; $i++) {
                                ?>
                                <tr>
                                    <td> <input type= "text"   name="<?php echo "one[]"; ?>"  value="<?php echo $_SESSION["lib" . $i]; ?>"> </td>
                                    <td> <input type= "text"   name="<?php echo "two[]"; ?>"  value="<?php echo $_SESSION[$i]; ?>"> </td>
                                </tr>
                                <?php
                            }
                            ?>
                        </table>
                    </div>
                    <div class="w3-display-position w3-white" style="top:330px;left:20px">
                        <input  type="submit"  name = "valider" value="OK">
                    </div>
                </form>
            </div>
        </div>
        <p> Click on OK for validation</p>
    </body>
</html>

Then read them using

<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>bz92</title>
        <style>
        </style>
    </head>
    <body>
        <?php
        echo "POST ";
        echo"<br>";

        print_r($_POST);

        echo"<br>";

        if (isset($_POST['valider'])) {
            echo "<br>";
// boucle

            $tableau = array();
// init

            $n = 0;

            foreach ($_POST['one'] as $index => $valeur) {

                $n = $n + 1;

                echo "valeur_one " . $valeur . " // ";
                echo "valeur_two " . $_POST['two'][$index] . " // ";
                echo "<br>";
            }
        }
        echo "<br>";
        echo "nombre " . $n;

// header('Location: bz5.php');

        exit();
        ?>
    </body>

</html>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...