Problem in passing variables between PHP pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
K-Z
Forum Commoner
Posts: 29
Joined: Mon Jul 05, 2010 9:03 am

Problem in passing variables between PHP pages

Post by K-Z »

Hi all....I am new to this forum and php coding. I am designing a website for posting articles where users will be able to post anything and comment to any submitted post. But the problem I am facing is to pass the post id (or any variable) from 1 php file to another. I am using hidden variable concept to pass variabes. Here is the code:

Code: Select all

//the variable has to be passed in new.php
[b]  <form action="new.php" method="post" enctype="multipart/form-data">[/b]

// $clb[club] is the variable that needs to be passed
[b]<?php $var = $clb[club]; 
echo "<input type='hidden' id='club' name='club' value='$var' >"; ?>[/b]

// code for taking input from the user
    <input type="text" size="50" name="text" id="textfield" />
    <select name="what" id="select">
      <option selected="selected">act</option>
      <option>issue</option>
      <option>diss</option>
    </select>
    <label for="button"></label>
    <input name="post" type="submit" id="button" value="post" />
      <label for="file"><br />
    </label>
  <p>Recent posts of club1: </p>
  <p>

// I also want $clb[club] to be passed in the file old.php....but even here variables are not passed at all!!!
[b]    <?php     include("old.php"); ?>[/b]
  </p>
  </form>

// I have even tried this format...I am passing $var1 in club.php when user clicks the link Friend but same story here
[b]<form action="club.php" method="post" enctype="multipart/form-data">
<?php $var1 = $_POST['club1'];
 echo "<input type='hidden' id='club' name='club1' value='$var1' >"; ?>
<a href="club.php">Friend
[/b]  </p>
          </a>
  </form>

Can you tell me what may be the problem...I am retrieving the variables like this:

Code: Select all

// in new.php
$ret = $_POST['var'];
Please Help :(
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Problem in passing variables between PHP pages

Post by Ziq »

Hi and welcome :)

I'm not sure how you use this code. But the main idea sending variables between pages via POST is to create 2 (or 1) files like this:
file1.php

Code: Select all

<form action="file2.php" method="POST">
<input type="text" name="var_name" />
<input type="submit">
</form>
file2.php

Code: Select all

<?php
echo $_POST['var_name'];
?>
var_name in file1.php and file2.php must be same.

Also functions var_dump() and print_r() can be usefull to understand concept. Try insert this code into new.php

Code: Select all

var_dump($_POST);
print_r($_POST);
K-Z
Forum Commoner
Posts: 29
Joined: Mon Jul 05, 2010 9:03 am

Re: Problem in passing variables between PHP pages

Post by K-Z »

Ziq wrote:Hi and welcome :)

I'm not sure how you use this code. But the main idea sending variables between pages via POST is to create 2 (or 1) files like this:
file1.php

Code: Select all

<form action="file2.php" method="POST">
<input type="text" name="var_name" />
<input type="submit">
</form>
file2.php

Code: Select all

<?php
echo $_POST['var_name'];
?>
var_name in file1.php and file2.php must be same.

Also functions var_dump() and print_r() can be usefull to understand concept. Try insert this code into new.php

Code: Select all

var_dump($_POST);
print_r($_POST);
Thank you very much for the reply. But your solution is for passing user's input to a php file. I have different issue:

Suppose you code a php page, say A.php, whose function is show all posts stored in the database. Each post is accompanied with a text box for others to comment on that post (just like Facebook where you have status instead of post). So on submitting any comment over a post, that text box would call another file, say B.php, which will store the comment in the database along with the unique auto-incrementing ID of that post. The ID would act as the address of the corresponding post. Passing the ID from A.php to B.php when user submits comment is my main problem. Now I hope I am able to make myself clear.

Please suggest me anything that comes in your mind??
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Problem in passing variables between PHP pages

Post by Ziq »

I dont have a facebook account so maybe I understand something wrong. Lets say you have file a.php

Code: Select all

<?php
/* This information you get from database */
$post_title = 'Hello world';
//...
$post_id = 3; // unique auto-increment ID for a post
?>
<form action="b.php" method="POST">
<textarea name="comment">leave your comment here</textarea>
<input type="hidden" name="post_id" value="<?php echo $post_id ?>">
<input type="submit">
</form>
Then you can easily store this comment in database

Code: Select all

<?php
$post_id = $_POST['post_id'];  // Read about SQL-Injection
$comment = $_POST['comment'];  // !!!

// ... other stuff to store this information
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Problem in passing variables between PHP pages

Post by Jonah Bron »

To persistently carry data between pages, use sessions.
Post Reply