Home > Back-end >  How do I retrieve echo each fam_mamber value?
How do I retrieve echo each fam_mamber value?

Time:07-28

on a previous page, the value 'numberOffamembers' is inputted by the user which creates user inputs for the names of each family member. I want to then store these names in a database. So to test, I tried to echo the fam_member[] values when the next buttons are clicked but it keeps saying that fam_member[x] is an unidentified index.

<?php 
include('db_connect.php');
if (isset($_POST["prev"])) {
    //echo "prev has been clicked";
    header("location: page4.php");
}
if(isset($_POST["next"])){
$fam_member = $_POST['fam_member[1]'];
echo $fam_member;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Add_extra_bio</title>
    <link rel="stylesheet" href="script.css">
</head>
<body>
    <h2>Welcome to the Abundant Grace Fellowship</h2>
    <p>===================================================</p>
<form action="page5.php" method = "POST">
 <?php
    for($x=1;$x<=$_GET['numberOffamembers'];$x  ){
    ?>
      <table>
        <tr>
          <td>Family Member <?=$x?>:</td>
          <td><textarea tabindex="2" id="message"  name="fam_member[<?=$x?>]" rows="1" cols="50" method="post"></textarea></td>
        </tr>
        <tr>
          <td>Mobile Number if any:</td>
          <td><textarea tabindex="2" id="message"  name="phone_no[<?=$x?>]" rows="1" cols="50" method="post"></textarea></td>
        </tr>
        <tr>
          <td colspan="2"><hr width="1150px"></td></tr>
      </table>
    <?php
    }
?>
</form>
<?php
?>
  <form action="page5.php" method="POST">
    <input type="submit" name="prev" value="Prev">
    <input type="submit" name="next" value="Next">
  </form>
</body>
</html>

I've already tried

echo $_POST['fam_member[1]'];

or changing the index

$x = 1
echo $_POST[fam_member[$x]];

The Previous page's code:

<?php 
include('db_connect.php');
//Previous button. Included on every page except 1
if (isset($_POST["prev"])) {
  header("location:page3.php");
}
if (isset($_POST["next"])){
  header("location:page5.php"); 
    $numb = mysqli_real_escape_string($conn, $_POST['numberOffamembers']);

    $sql = "INSERT INTO users(Number of Additional Family Members) VALUES($numb)";
}

 ?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Select_extra_num</title>
    <link rel="stylesheet" href="script.css">
</head>
<body>
    <h2>Welcome to the Abundant Grace Fellowship</h2>
    <p>===================================================</p>
  <p>Select number of extra family members from the dropdown list below</p>
<form action='page5.php'>
   <select name="numberOffamembers" id="famMembers">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10 </option>
   </select>
  <br>
<input type="submit" name="next" value="next">
</form>
<form action='page3.php'>
    <input type="submit" name="prev" value="prev">
</form>
</body>
</html>

I am still a novice at php and this page was created by someone else.

CodePudding user response:

for echo $fam_member value dont add [1]

$fam_member = $_POST['fam_member[1]']; **<-----this is wrong**

follow this to echo :

$fam_member = $_POST['fam_member'];  <----- this is right

CodePudding user response:

Here is a simple solution to your problem. The problem here is that you are submitting the below form but the textarea fields are present in the upper form, that's why you not getting the textarea values in your POST request.

Step 1: Move the buttons to the upper form element, just below the for loop ending bracket.

Step 2: You are using the wrong name for your textarea fields. You don't have to print index inside the name attribute. It will automatically assign the index to id. Change it from this:

name="fam_member[<?=$x?>]"

To this:

name="fam_member[]"

You are also getting the values in wrong manner through $_POST. So, change this line:

$fam_member = $_POST['fam_member[1]'];

To this:

$fam_member = $_POST['fam_member'];
echo $fam_member[0];

Below is the complete working example for page 5:

<?php 
include('db_connect.php');
if (isset($_POST["prev"])) {
    echo "prev has been clicked";
    header("location: page4.php");
}
if(isset($_POST["next"])){
    $fam_member = $_POST['fam_member'];
    echo $fam_member[0];
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Add_extra_bio</title>
    <link rel="stylesheet" href="script.css">
</head>
<body>
    <h2>Welcome to the Abundant Grace Fellowship</h2>
    <p>===================================================</p>
<form action="page5.php" method = "POST">
 <?php
    for($x=1;$x<=$_GET['numberOffamembers'];$x  ){
    ?>
      <table>
        <tr>
          <td>Family Member <?=$x?>:</td>
          <td><textarea tabindex="2" id="message"  name="fam_member[]" rows="1" cols="50" method="post"></textarea></td>
        </tr>
        <tr>
          <td>Mobile Number if any:</td>
          <td><textarea tabindex="2" id="message"  name="phone_no[]" rows="1" cols="50" method="post"></textarea></td>
        </tr>
        <tr>
          <td colspan="2"><hr width="1150px"></td></tr>
      </table>
    <?php
    }
?>
    <input type="submit" name="prev" value="Prev">
    <input type="submit" name="next" value="Next">
</form>
<?php
?>

</body>
</html>

Hope that will help!

  • Related