1

I have a php variable "echo $id". Now I want to use the $_POST method to post the variable. I just want to know how to do this for a variable because $_POST[$id] does not work? I want the $id to be posted from the form into another page.

Below is my function where the $id is in:

function id_generator(){ 

          $id = ""; 
          $a = array( "A" , "B"); 

      for( $i = 0 ; $i < 3 ; $i++ ){ $r = rand( 0 , 25 );
       $id .= $a[ $r ]; 

   }

   return $id;
}

Below is the form:

         <form action="QandATable.php" method="post" id="sessionForm">
         <p><strong>1: Your Session ID: </strong><?php echo $id; ?></p>
</form>

Thanks

1

2 Answers 2

3

You need to put the id into a form input field. For example, to put it in a hidden input:

<form action="QandATable.php" method="post" id="sessionForm">
     <p><strong>1: Your Session ID: </strong><?php echo $id; ?></p>
     <input type='hidden' name='id' value='<?php echo $id; ?>' />
</form>

Then after the user clicks a submit button (which you will also need to add to the form), in QandATable.php you can just go

$id = $_POST["id"]

to get the id.

Sign up to request clarification or add additional context in comments.

Comments

1

$_POST variable keeps values passed via POST method. But in your code I don't see any fields in form that can handle this value. You should create input, select or textarea inside <form> tag. Then after submit $_POST will be filled with values of this fields. Key in $_POST array will come from name attribute and value will be value of field. You probably want to use $_SESSION variable in this case. With $_SESSION variable is remembered on server.

$_SESSION['id'] = id_generator(); // to set
echo $_SESSION['id']; // to use

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.