• I am trying to pass a value from a select dropdown on an html form to another page after clicking a submit button. This value doesn’t need to be stored in the database, but instead used temporary to display the users choice of product data from the database on the next page. The html output of the form works as expected with the value appearing in the option value, I just can’t get it to post that value and “fetch” it from page two using POST. But If I change POST to GET. It works as expected. I’ve been told using POST is preferred, but I cant seem to get that to work?:

    Form showing on page 1:

    <?php
    
    echo '<form method="post" action="/page2">';
    echo '<select name="selectProduct">';
    echo '<option value="" disabled selected>--select--</option>';
    foreach ( $getInfo as $product ) {
        echo '<option value="' . esc_html( $product->id ) . '">'.esc_html($product->product).'</option>';
    }
    echo '</select>';
    echo '<div class="btnWrapper">';
        echo '<input class="btn border-width-0 btn-text-skin btn-color-jevc btn-square btn-icon-left adduserBtn" type="submit" name="btnSubmit" value="View specs">';
    echo '</div>';
    echo '</form>';

    Output on page 2:

    <?php
    // ...
    $selectedProduct = $_POST['selectProduct'];
    echo '<div><b>YOU SELECTED:</b>' . esc_html( $selectedProduct ) .'</div>';

    When doing var_dump($_POST); on page2, I get an empty array(0){}

    And a php error when posting:
    error when it is posting:

    WordPress database error:

    Unknown column ‘selectProduct’ in ‘field list’ for query UPDATE myDBtable SET selectProduct= ’39’ WHERE id= 39

    Where am I going wrong?

Viewing 7 replies - 1 through 7 (of 7 total)
  • This link shows the functions affecting the query variables. WordPress strips all the variables that it doesn’t know about.

    Moderator bcworkz

    (@bcworkz)

    Indeed, unknown query_vars get stripped, but WP would not clear values in $_POST.

    I tested your code snippets as best I could considering I don’t have a products list. I hard coded arbitrary values as needed. The second page outputs YOU SELECTED: 4444 as I expected since my selected option value was 4444. Var_dumping $_POST on the destination page outputs the form element values as expected.

    Is there a redirect in between form and page 2? They sometimes drop passed values. The DB error message shows that a value of “39” was passed, so something is working despite your var_dump() not showing anything. If your table does not contain a ‘selectProduct’ column, there’s an issue with how your SQL query is constructed.

    Thread Starter creativ3y3

    (@creativ3y3)

    I’m not aware of any redirection happening but may need to look into that to see if anything else may be causing it to do that. If I remove the action on the form and post it echo $selectedProduct on the same page it works. It fails when the action is added to the form to go to the second page. When I land on that page I get: Undefined variable: selectedProduct

    Thread Starter creativ3y3

    (@creativ3y3)

    I’ve just noticed in the second page that it lands on there is a form with a post action and an if post function, that seems to be clashing. If I remove that “if post” code, it seems to work.

    So I am guessing that means it’s not possible to have another “if post” on the secondary page?

    • This reply was modified 5 years, 1 month ago by creativ3y3.
    Thread Starter creativ3y3

    (@creativ3y3)

    Thanks for your time @bcworkz you got me on the right track for debugging it. It looks like it is related to another bit of code that is conflicting with it that was stopping the variable from working properly. The POST action on page 1 that goes to page 2 lands on a page that lets the user update data and submit it to the database. So to get the product id from page 1 I pass the variable using post and on page 2 use:
    $selectedProduct = $_POST['selectProduct'];
    I use this information to query the data by id which all works. Then if I include the below code to allow the user to update the data (on the second page) it breaks due to the line commented out below refreshing it, if I comment it out and submit it breaks on the refresh after submission due to the above $selectedProduct variable now being undefined. Below is the updating code:

    if ($_POST) {
    	$skipKeys = array(
                        'btnSubmit',
                        'id',
                        'Product',
                        'Series',
                        'selectProduct',
                     );
    
    foreach($_POST as $key =>$value){
            if(!in_array($key,$skipKeys)){
                $myDB->query(
                    $myDB->prepare(
                    "UPDATE <code>my-table</code> SET ".$key."= %s WHERE id= %d", $value, $selectedProduct
                    )
                );
            }    
    }
    //this line causes it not to work at all
    //header ("location: " . $_SERVER['REQUEST_URI']);
    }

    Any ideas how to get around this? Maybe trying a session variable instead?

    • This reply was modified 5 years, 1 month ago by creativ3y3.

    You must use isset to check the isset example isset($_POST[‘key’]); obviously $_POST is always empty if you don’t use a key and not have value for example:

    $_POST['product'] = '';
    if( $_POST['product'] ) {
    //this false never true
    }
    if( isset($_POST['product'] ) ) {
    //true
    }

    , also in foreach you can create a container variable and then use echo when you have all the data.
    POST data are visible only on the landing page, you cannot redirect.
    Use SESSION or maybe global $selectedProduct; for function.

    Thread Starter creativ3y3

    (@creativ3y3)

    @autotutorial Thanks for your advice, I will look into all these things. I tried getting a session variable to work, it works when I don’t leave the page (with no form action) and post the form, but as soon as I put an action on the form to take it to the second page it doesn’t set the variable. On page 2 the session variable does show, but it hasn’t updated from the post on page 1 (so it displays the wrong data).

    On page 1 I do the following:

    if(!isset($_SESSION)) 
        { 
            session_start();
        } 

    and:

    if( isset($_POST['selectProduct'] ) ) {
    $selectedProduct = $_POST['selectProduct'];
    $_SESSION['selectedModel'] = $selectedProduct;
    	echo $_SESSION['selectedModel'];
    }

    Then Page 2:

    if(!isset($_SESSION)) 
        { 
            session_start();
        } 
    echo $_SESSION['selectedModel'];

    Am I setting the session variable wrong?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Posting variable from one page to another’ is closed to new replies.