SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    issue passing values with hidden fields

    I've been having an issue passing hidden values over the past few days and am just displaying an update if anyone might be able to help me find a solution. I realise it's probably simple but I can't seem to get it working so any help would be great.

    Can someone have a look at how I'm passing the values to see if there's something I should be doing to get them outputting on the process.php page.

    list.php

    Code:
    echo '<form action ="cart.php" method="post">';
    echo'<p><table border = "1" cellspacing="1">';
    echo'<tr><td>Title</td><td>CD</td><td>Quantity</td></tr>';
    while($row = mysql_fetch_array($result)){
    	echo '<tr>';
    	echo '<td>'. $row['title'] .'</td>';
    	echo '<td>'. $row['cd'] .'</td>';
    	echo '<td><input name ="'.$row['productid'].'" type="text" /></td>';
    	echo '</tr>';
    }
    echo'</table></p>';
    echo'<input name="submit" type="submit" />';
    echo'</form>';
    cart.php

    Code:
    <?php
            foreach($_POST as $key => $value){
            	echo $key.'';
            	echo 'The quantity is:'.$value;
            	echo '<br/>';
            }
            ?>
    
            <form action="process.php" method="post">
            <table>
    	<td><input type="hidden" name="productid" id="productid" value="<?php echo $key; ?>" /></td>
    <td><input type="hidden" name="quantity" id="quantity" value="<?php echo $value; ?>" /></td>
            <tr>
                <td>First name:</td>
                <td><input name="firstname" type="text" value="<?php if(isset($_POST) && isset($_POST['firstname'])){
            		echo $row['firstname'];
            	}
            	?>"/></td>
                <td></td>
              </tr>
              <tr>
                <td>Last name:</td>
                <td><input name="lastname" type="text" value="<?php if(isset($_POST)&& isset($_POST['lastname'])){
            		echo $_POST['lastname'];
            	}
            	?>" /></td>
                <td></td>
              </tr>
            <tr>
                <td></td>
                <td><input name="submit" type="submit" value="submit" /></td>
                <td>&nbsp;</td>
              </tr>
            </table>
            </form>
    process.php

    I've inserted the following to retrieve the posted variables sent in the hidden fields of the form on cart.php page:

    Code:
    echo $_POST['productid'];
    echo $_POST['quantity'];

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In the first block of code the titles don't match the data:
    Code:
    Title, CD, Quantity
    title, cd, productid
    To debug code you have to use the Sherlock Holmes method: find the evidence. Look at the html your code produces ('View source' in browser) and see if that is what you thought you wanted. Do this methodically from start to finish. There is a strong likelihood that you find little bugs like the one above.
    Denny Schlesinger
    web services

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's because I'm storing the productid as the $key value of the foreach loop in cart.php, and the $value variable takes the quantity amount which was entered:

    <input name ="'.$row['productid'].'" type="text" />

    These are being echoed fine in the cart.php page. It's getting them to display in the process.php page where I'm having difficulty.

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There is no quantity input field in the code you are showing and therefore it looks like a bug to people trying to help you. It's a show stopper.

    If that is not the place where the bug is then there is no need for you to post it. If the bug is only in the process page, then show only the code for that. Make life as simple as possible for the people trying to help.

    It's getting them to display in the process.php page where I'm having difficulty.
    Have you looked at the html output? What's wrong with it?
    Denny Schlesinger
    web services

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The output shows the folowing where the echoed productid and quantity should be:

    submitSubmit Query

    Thanks for any help

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do I insert the hidden fields in the foreach loop like so:

    <?php
    echo '<form action="process.php" method="post">';
    foreach($_POST as $key => $value){
    echo $key.'';
    echo 'The quantity is:'.$value;
    echo '<br/>';
    echo '<input type="hidden" name="productid" id="productid" value="'.$key.'" />';
    echo '<input type="hidden" name="quantity" id="quantity" value="'.$value.'" />';
    }
    echo '</form>';
    ?>


    I've included the form tags so the hidden fields get sent. The form is being sent to the same process.php that the other form on the same page is being sent to. If the user submits the main form, how do I ensure that the above hidden fields get sent to the process.php page aswell(that both form are sent to the same page at once, as there is only one submit button for the main form)

    I have the following on process.php to get the values from the foreach loop but nothing's showing:

    echo $_POST['productid'];
    echo $_POST['quantity'];

  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seannie View Post
    do I insert the hidden fields in the foreach loop like so:

    PHP Code:
    <?php
    echo '<form action="process.php" method="post">';
            foreach(
    $_POST as $key => $value){
                echo 
    $key.'';
                echo 
    'The quantity is:'.$value;
                echo 
    '<br/>';
                    echo 
    '<input type="hidden" name="productid" id="productid" value="'.$key.'" />';
                    echo 
    '<input type="hidden" name="quantity" id="quantity" value="'.$value.'" />';
            }
    echo 
    '</form>';
            
    ?>
    I've included the form tags so the hidden fields get sent. ....
    There is no "submit" button (input field) in that form and it cannot send anything.

    seannie, you have a serious case of "spaghetti code" which makes things difficult to follow. Good coding practice separates process (php) from display (html) as far as possible. Also, you are using tables for formatting the output, a practice that has gone out of style maybe ten years ago. Instead of tables you should be using CSS and the proper tags like <fieldset>, <legend>, and <label>. For example:
    HTML Code:
    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <fieldset style="width:450px;">
    <legend>My Data</legend>
      <label class="form" for="cd">
        CD
      </label>
      <input type="text" name="cd" id="cd" value="<?php echo $_POST['cd']; ?>" size="40" maxlength="31">
      <label class="form" for="qty">
        Quantity
      </label>
      <input type="text" name="qty" id="qty" value="<?php echo $_POST['qty']; ?>" size="40" maxlength="31">
      <input type="submit" name="action" value="Submit form">
      <input type="reset" value="Reset form">
    </fieldset>
    </form>
    Also, html tags need to be properly nested. In a piece of your code I saw <td> tags that were not inside <tr> tags. It's important to write valid html to make it likely that all browsers will render it correctly. Try testing your html code with the http://validator.w3.org/ validator.

    My suggestion would be for you to go back to square one and reorganize (clean up) your code.

    Also, to better use the sitepoint forum, use the proper tags to display your code: [ php ], [ HTML ], [ CODE ], as the case may be.

    By way of anecdote, I still remember my very first "professional" program back in 1960. It was "big" for the time, a box-full of IBM cards. It was "spaghetti code" so complicated that I could not make heads or tails of it when I tried to debug it. I decided to throw the whole thing in the trash, literally. My colleagues thought I had gone mad but it was the best decision I made, I now try to write code as clean as possible to make sure even I understand it.
    Denny Schlesinger
    web services


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •