Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts

    passing variables

    Hey guys, so right now i have a page that when opened it has a text box where the user can enter in data and then hit a submit button which will post the data back to the same page, then bring up another text box so the user can enter in data again. (the user is entering in multiple steps for recipes)

    When the page opens for testing purposes i am displaying the 'id' for that record the user wants to add "steps" into. I echo out the 'id' just to make sure it is being passed to this page (AND IT IS)

    But after the user enters in step 1 and clicks submit, i am not quite sure how to pass the id again in the form action where i am creating multiple text boxes for the steps procedure

    Can anyone tell me how to pass the 'id' so i am able to echo it out inside of the FOR STATEMENT
    here is my code:

    Code:
    <?php
    
    $db = mysql_connect( "localhost","root", "temp1234");
    mysql_select_db( "Peters_restaurantDB");
    
    	$id = $_GET['id'];
    	
    	echo "DEBUG: value of id is " . $id . "<hr/>\n";
    
        if(isset($_GET['steps'])&&is_numeric($_GET['steps']))
        {
            $steps=$_GET['steps'];
    
    		
       for ($i = 1; $i <= $steps; $i++) {
           echo "DEBUG: step number: " .$i . "<br />";
           echo "DEBUG: step desc: " . $_POST["step$i"] . "<br /><br />";
       }
    		
    //	$sql = "
    //	INSERT INTO steps (recipe_id, stepno, text)
    //	VALUES ($id, $steps, 'step$step');	
    //	";
    		
    //    $result = mysql_query( $sql )
    //     or die( "Insert failed ($sql): " . mysql_error() ); 		
    		
        }
        else
            $steps=1;
    
    ?>
    
    <html>
    <head></head>
    <body>
    
    <h1>Add Recipe Steps</h1><hr /> 
    
    <form action="<?php echo $_SERVER['PHP_SELF'] . "?steps=" . ($steps+1);?>" method="POST">
    <table>
    
    <br />
    <?php
        $step=1;
        do
        {
            echo <<<HTML
            <tr>
                <td align="right">
                    Step $step:
                </td>
                <td>
                    <input type="text" name="step$step" value="
    HTML;
        if(isset($_POST["step$step"])) echo $_POST["step$step"];
        echo <<<HTML
    ">
                </td>
            </tr>
    HTML;
        $step++;
        }
        while($step<=$steps);
    
    ?>
    </table>
    <input type="submit" value="Add Step">
    
    </form>
    
      <!--Now this following line of code will be used to return to previous page-->
    <p> </p>
    <form method="get" ACTION="PetersRecipeDB.php">
    <input type="submit" value="Back"> 
    </form>
    </body>
    </html>

  • #2
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 2 Times in 2 Posts
    If I understand correctly what you need is post id after submit button is pressed;
    To do this you can use an hidden parameter and than modify your code this way:

    PHP Code:
    <?php

    $db 
    mysql_connect"localhost","root""temp1234");
    mysql_select_db"Peters_restaurantDB");

        if(isset( 
    $_GET['id'])) $id $_GET['id'];//MODIFIED
        
    if(isset( $_POST['id'])) $id $_POST['id'];//ADDED
        
    echo "DEBUG: value of id is " $id "<hr/>\n";

        if(isset(
    $_GET['steps'])&&is_numeric($_GET['steps']))
        {
            
    $steps=$_GET['steps'];

            
       for (
    $i 1$i <= $steps$i++) {
           echo 
    "DEBUG: step number: " .$i "<br />";
           echo 
    "DEBUG: step desc: " $_POST["step$i"] . "<br /><br />";
       }
            
    //    $sql = "
    //    INSERT INTO steps (recipe_id, stepno, text)
    //    VALUES ($id, $steps, 'step$step');    
    //    ";
            
    //    $result = mysql_query( $sql )
    //     or die( "Insert failed ($sql): " . mysql_error() );         
            
        
    }
        else
            
    $steps=1;

    ?>

    <html>
    <head></head>
    <body>

    <h1>Add Recipe Steps</h1><hr /> 

    <form action="<?php echo $_SERVER['PHP_SELF'] . "?steps=" . ($steps+1);?>" method="POST">
    <table>

    <br />
    <?php
        $step
    =1;
        do
        {
            echo <<<HTML
            <tr>
                <td align="right">
                    Step $step:
                </td>
                <td>
                    <input type="text" name="step$step" value="
    HTML;
        if(isset(
    $_POST["step$step"])) echo $_POST["step$step"];
        echo <<<HTML
    ">
                </td>
            </tr>
    HTML;
        
    $step++;
        }
        while(
    $step<=$steps);

    ?>
    </table>
    <input type="hidden" name="id" value="<?php echo $id ?>"> //ADDED
    <input type="submit" value="Add Step">

    </form>

      <!--Now this following line of code will be used to return to previous page-->
    <p> </p>
    <form method="get" ACTION="PetersRecipeDB.php">
    <input type="hidden" name="id" value="<?php echo $id ?>"> //ADDED
    <input type="submit" value="Back"> 
    </form>
    </body>
    </html>
    In this code I marked with //ADDED and //MODIFIED the few lines of code I added or modified.
    Hope this helps,
    Antonio


  •  

    Posting Permissions

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