1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem in passing variables between PHP pages

Discussion in 'PHP' started by K-Z, Jul 5, 2010.

  1. #1
    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:

    //the variable has to be passed in new.php
    <span style="font-weight: bold">  <form action="new.php" method="post" enctype="multipart/form-data"></span>
    
    // $clb[club] is the variable that needs to be passed
    <span style="font-weight: bold"><?php $var = $clb[club]; 
    echo "<input type='hidden' id='club' name='club' value='$var' >"; ?></span>
    
    // 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!!!
    <span style="font-weight: bold">    <?php     include("old.php"); ?></span>
      </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
    <span style="font-weight: bold"><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
    </span>  </p>
              </a>
      </form>
    
    PHP:
    Can you tell me what may be the problem...I am retrieving the variables like this:

    // in new.php
    $ret = $_POST['var'];
    PHP:
    Please Help me....:(
    SEMrush
     
    K-Z, Jul 5, 2010 IP
    SEMrush
  2. Logie

    Logie Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I believe it should be

    $ret = $_GET['var'];
    Code (markup):
    Edit

    What is it you are trying to do exactly o.0
     
    Logie, Jul 5, 2010 IP
  3. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #3
    You need to use PHP sessions - look it up. You start a PHP session by calling the following:
    session_start( );
    PHP:
    You can store a value inside it and it should be available on any PHP pages:
    $_SESSION['whatever'] = "blablabla";
    PHP:
     
    Rainulf, Jul 5, 2010 IP
  4. K-Z

    K-Z Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank u for the reply. I will explain you the problem

    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.

    The problem sounds silly but I have tried all measures including your suggestion $_GET['var']. I am really stuck....you got another approach??
     
    K-Z, Jul 5, 2010 IP
  5. K-Z

    K-Z Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the reply...I have tried the concept. I used a session variable to store unique id of posts.Actually the problem is that when A.php gets executed (please see my reply to Peon), all the posts are loaded and session variable stores the id of first post which is then replaced by second post id and so on. This leads to the storage of id of last post in it. Am i using in the wrong manner?? Please suggest me..
     
    K-Z, Jul 5, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    When you have a form action="new.php" method="post" this means you are posting the required variables into the file new.php

    So from your form
    
    <input type='hidden' id='club' name='club' value='$var' >
    
     // 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>
        
    Code (markup):
    You are posting
    
    name='club'
    name="text"
    name="what"
    
    Code (markup):
    So inside the file new.php you collect the variables from the post as follows.

    
    $club = $_POST['club'];
    $text = $_POST['text'];
    $what = $_POST['what'];
    
    Code (markup):
    So now

    $club, $text and $what have values and your free to do as you wish ie: enter those into a database or use them for something else.
     
    Last edited: Jul 5, 2010
    MyVodaFone, Jul 5, 2010 IP
    K-Z likes this.
  7. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #7
    haha I see, that is quite troubling. I think I know where you went wrong. You need to put relation between user ID and post ID.. Here's my approach, you will need three tables:
    So when you submit a post, it'll submit the post id along with the user id. When displaying these, you display post corresponding to the user id by calling the viewId.

    You'll need to use foreach by the way.. This isn't the most right way to use session because as you know, it replaces it.
     
    Last edited: Jul 5, 2010
    Rainulf, Jul 5, 2010 IP
    K-Z likes this.
  8. K-Z

    K-Z Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you very much.....:) Problem solved....I know this concept but I missed...Jesus.....what a jerk I am.....Thnx...
     
    K-Z, Jul 5, 2010 IP
  9. K-Z

    K-Z Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you very much for your suggestion. Though my problem has been solved, I really appreciate your solution and will keep in mind for future....thank you...:)
     
    K-Z, Jul 5, 2010 IP