简体   繁体   中英

Echo PHP variable in foreach loop

I have my main page set as follows. It has 2 text boxes and a list of checkboxes which is populated from a text file (I'm not using a db as its a simple form). My issue is that I cannot POST the values in the checkboxes and echo them

    <!DOCTYPE HTML>
<html>
<body>
    <?php
        $CN = $Assignee ="";
    ?>

<form action="GPParser1.php" method="post">
<table>
  <tr>
    <td>CName:</td>
    <td><input type="text" name="CN"></td>
  </tr>

  <tr>
    <td>Assignee:</td>
    <td><input type="text" name="Assignee" ></td>
  <tr>

  <tr>
    <td>Select Vendors:</td>
</tr>
    <td>
      <?php

      $gp = file('GP.txt');

      foreach ($gp as $line_num => $gp)
        {
          //echo '<input type="checkbox" name= $line value=$line>($line)."<br />\n";
        print "
                        <br/>
                        <input type='checkbox' name='" . $gp . "' value='" . $gp . "'>$gp";
        };

      ?>
    </td>
  </tr>

  <tr>
    <td></td>
    <td>
      <input type="submit">
    </td>


 <?php

 echo $CasinoName;
 echo $Assignee;

 //foreach ($gp as $ln => $gp) {
//  echo $gp;
 //};
?>
</body>
<html>

And:

<!DOCTYPE HTML>
<html>
<body>
  <?php

    $CN = $_POST["CN"];
    $Assignee = $_POST["Assignee"];
    $GPS = $_POST ["$gp"];

 echo $CN;
 echo $Assignee;
 foreach ($GPS as $GPss => $GPS)
 {
   echo "$GPS";
 }

 //foreach ($gp as $ln => $gp) {
//  echo $gp;
 //};
?>
</body>
<html>

As it is right now I'm getting several errors based around $GPS = $_POST ["$gp"];

What should I do?

In both these scripts you are overwriting variables in the foreach loops

foreach ($gp as $line_num => $gp)
//                     this  ^^^
//       ^^^ overwrites this

and

foreach ($GPS as $GPss => $GPS)
//                  this  ^^^
//       ^^^ overwrites this

because you are using the same variable name on both sides of the as

Instead use another varibale name like this

foreach ($gp as $line_num => $gpx)

The normal approach is to name the array variable containing the array using a pluralised name like $items and the singluar variable witout, like

foreach ($gps as $line_num => $gp)

I'm not certain if this is the kind of solution you're looking for if I'm wrong, please let me know so I can give a better answer.

My solution is that your form will have a checkbox list that will later become an array of values when the form gets submitted.

 <!DOCTYPE HTML> <html> <body> <?php $CN = $Assignee =""; ?> <form action="GPParser1.php" method="post"> <table> <tr> <td>CName:</td> <td><input type="text" name="CN"></td> </tr> <tr> <td>Assignee:</td> <td><input type="text" name="Assignee"></td> <tr> <tr> <td>Select Vendors:</td> </tr> <td> <?php $gp = file('GP.txt'); foreach ($gp as $line_num => $gp) { ?> <p><input type="checkbox" name="gp[]" value="<?php echo $gp; ?>"> <?php echo $gp; ?> </p> <?php }; ?> </td> </tr> <tr> <td></td> <td> <input type="submit"> </td> </tr> </body> </html> 

 <!DOCTYPE HTML> <html> <body> <?php $CN = $_POST["CN"]; $Assignee = $_POST["Assignee"]; $GPS = $_POST["gp"]; echo $CN; echo $Assignee; foreach ($GPS as $GPss => $GPS) { echo "$GPS"; } //foreach ($gp as $ln => $gp) { // echo $gp; //}; ?> </body> <html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM